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 | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 14 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 15 | #include <utility> |
| 16 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/jsepicecandidate.h" |
| 19 | #include "api/jsepsessiondescription.h" |
| 20 | #include "api/mediaconstraintsinterface.h" |
| 21 | #include "api/mediastreamproxy.h" |
| 22 | #include "api/mediastreamtrackproxy.h" |
| 23 | #include "call/call.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 24 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 26 | #include "media/sctp/sctptransport.h" |
| 27 | #include "pc/audiotrack.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 28 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "pc/channelmanager.h" |
| 30 | #include "pc/dtmfsender.h" |
| 31 | #include "pc/mediastream.h" |
| 32 | #include "pc/mediastreamobserver.h" |
| 33 | #include "pc/remoteaudiosource.h" |
| 34 | #include "pc/rtpreceiver.h" |
| 35 | #include "pc/rtpsender.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 36 | #include "pc/sctputils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "pc/streamcollection.h" |
| 38 | #include "pc/videocapturertracksource.h" |
| 39 | #include "pc/videotrack.h" |
| 40 | #include "rtc_base/bind.h" |
| 41 | #include "rtc_base/checks.h" |
| 42 | #include "rtc_base/logging.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 43 | #include "rtc_base/ptr_util.h" |
| 44 | #include "rtc_base/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 45 | #include "rtc_base/stringencode.h" |
| 46 | #include "rtc_base/stringutils.h" |
| 47 | #include "rtc_base/trace_event.h" |
| 48 | #include "system_wrappers/include/clock.h" |
| 49 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 51 | using cricket::ContentInfo; |
| 52 | using cricket::ContentInfos; |
| 53 | using cricket::MediaContentDescription; |
| 54 | using cricket::SessionDescription; |
| 55 | using cricket::TransportInfo; |
| 56 | |
| 57 | using cricket::LOCAL_PORT_TYPE; |
| 58 | using cricket::STUN_PORT_TYPE; |
| 59 | using cricket::RELAY_PORT_TYPE; |
| 60 | using cricket::PRFLX_PORT_TYPE; |
| 61 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 62 | namespace webrtc { |
| 63 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 64 | // Error messages |
| 65 | const char kBundleWithoutRtcpMux[] = |
| 66 | "rtcp-mux must be enabled when BUNDLE " |
| 67 | "is enabled."; |
| 68 | const char kCreateChannelFailed[] = "Failed to create channels."; |
| 69 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 70 | const char kInvalidSdp[] = "Invalid session description."; |
| 71 | const char kMlineMismatchInAnswer[] = |
| 72 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 73 | "answer."; |
| 74 | const char kMlineMismatchInSubsequentOffer[] = |
| 75 | "The order of m-lines in subsequent offer doesn't match order from " |
| 76 | "previous offer/answer."; |
| 77 | const char kPushDownTDFailed[] = "Failed to push down transport description:"; |
| 78 | const char kSdpWithoutDtlsFingerprint[] = |
| 79 | "Called with SDP without DTLS fingerprint."; |
| 80 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 81 | const char kSdpWithoutIceUfragPwd[] = |
| 82 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 83 | const char kSessionError[] = "Session error code: "; |
| 84 | const char kSessionErrorDesc[] = "Session error description: "; |
| 85 | const char kDtlsSrtpSetupFailureRtp[] = |
| 86 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 87 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 88 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
| 89 | const char kEnableBundleFailed[] = "Failed to enable BUNDLE."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 91 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 93 | static const char kDefaultStreamLabel[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 94 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 95 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 96 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 97 | // The length of RTCP CNAMEs. |
| 98 | static const int kRtcpCnameLength = 16; |
| 99 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 101 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 103 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 105 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 108 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | explicit SetSessionDescriptionMsg( |
| 110 | webrtc::SetSessionDescriptionObserver* observer) |
| 111 | : observer(observer) { |
| 112 | } |
| 113 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 114 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | std::string error; |
| 116 | }; |
| 117 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 118 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 119 | explicit CreateSessionDescriptionMsg( |
| 120 | webrtc::CreateSessionDescriptionObserver* observer) |
| 121 | : observer(observer) {} |
| 122 | |
| 123 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 124 | std::string error; |
| 125 | }; |
| 126 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 127 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 128 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 129 | webrtc::MediaStreamTrackInterface* track) |
| 130 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 132 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 133 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 136 | // Check if we can send |new_stream| on a PeerConnection. |
| 137 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 138 | webrtc::MediaStreamInterface* new_stream) { |
| 139 | if (!new_stream || !current_streams) { |
| 140 | return false; |
| 141 | } |
| 142 | if (current_streams->find(new_stream->label()) != nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 143 | RTC_LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 144 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool MediaContentDirectionHasSend(cricket::MediaContentDirection dir) { |
| 151 | return dir == cricket::MD_SENDONLY || dir == cricket::MD_SENDRECV; |
| 152 | } |
| 153 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 154 | // If the direction is "recvonly" or "inactive", treat the description |
| 155 | // as containing no streams. |
| 156 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 157 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 158 | const cricket::MediaContentDescription* desc) { |
| 159 | return MediaContentDirectionHasSend(desc->direction()) |
| 160 | ? desc->streams() |
| 161 | : std::vector<cricket::StreamParams>(); |
| 162 | } |
| 163 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 164 | bool IsValidOfferToReceiveMedia(int value) { |
| 165 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 166 | return (value >= Options::kUndefined) && |
| 167 | (value <= Options::kMaxOfferToReceiveMedia); |
| 168 | } |
| 169 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 170 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 171 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 172 | const std::vector<rtc::scoped_refptr< |
| 173 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 174 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 175 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 176 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 177 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 178 | if (audio_media_description_options) { |
| 179 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 180 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 181 | } |
| 182 | } else { |
| 183 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 184 | if (video_media_description_options) { |
| 185 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 186 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 189 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 190 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 191 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 192 | // Add options to |session_options| from |rtp_data_channels|. |
| 193 | void AddRtpDataChannelOptions( |
| 194 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 195 | rtp_data_channels, |
| 196 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 197 | if (!data_media_description_options) { |
| 198 | return; |
| 199 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 200 | // Check for data channels. |
| 201 | for (const auto& kv : rtp_data_channels) { |
| 202 | const DataChannel* channel = kv.second; |
| 203 | if (channel->state() == DataChannel::kConnecting || |
| 204 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 205 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 206 | // the data channel's label. |
| 207 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 208 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 213 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 214 | PeerConnectionInterface::IceTransportsType type) { |
| 215 | switch (type) { |
| 216 | case PeerConnectionInterface::kNone: |
| 217 | return cricket::CF_NONE; |
| 218 | case PeerConnectionInterface::kRelay: |
| 219 | return cricket::CF_RELAY; |
| 220 | case PeerConnectionInterface::kNoHost: |
| 221 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 222 | case PeerConnectionInterface::kAll: |
| 223 | return cricket::CF_ALL; |
| 224 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 225 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 226 | } |
| 227 | return cricket::CF_NONE; |
| 228 | } |
| 229 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 230 | // Helper to set an error and return from a method. |
| 231 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 232 | if (error) { |
| 233 | error->set_type(type); |
| 234 | } |
| 235 | return type == webrtc::RTCErrorType::NONE; |
| 236 | } |
| 237 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 238 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 239 | if (error_out) { |
| 240 | *error_out = std::move(error); |
| 241 | } |
| 242 | return error.ok(); |
| 243 | } |
| 244 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 245 | std::string GetSignalingStateString( |
| 246 | PeerConnectionInterface::SignalingState state) { |
| 247 | switch (state) { |
| 248 | case PeerConnectionInterface::kStable: |
| 249 | return "kStable"; |
| 250 | case PeerConnectionInterface::kHaveLocalOffer: |
| 251 | return "kHaveLocalOffer"; |
| 252 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 253 | return "kHavePrAnswer"; |
| 254 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 255 | return "kHaveRemoteOffer"; |
| 256 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 257 | return "kHaveRemotePrAnswer"; |
| 258 | case PeerConnectionInterface::kClosed: |
| 259 | return "kClosed"; |
| 260 | } |
| 261 | RTC_NOTREACHED(); |
| 262 | return ""; |
| 263 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 264 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 265 | IceCandidatePairType GetIceCandidatePairCounter( |
| 266 | const cricket::Candidate& local, |
| 267 | const cricket::Candidate& remote) { |
| 268 | const auto& l = local.type(); |
| 269 | const auto& r = remote.type(); |
| 270 | const auto& host = LOCAL_PORT_TYPE; |
| 271 | const auto& srflx = STUN_PORT_TYPE; |
| 272 | const auto& relay = RELAY_PORT_TYPE; |
| 273 | const auto& prflx = PRFLX_PORT_TYPE; |
| 274 | if (l == host && r == host) { |
| 275 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 276 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 277 | if (local_private) { |
| 278 | if (remote_private) { |
| 279 | return kIceCandidatePairHostPrivateHostPrivate; |
| 280 | } else { |
| 281 | return kIceCandidatePairHostPrivateHostPublic; |
| 282 | } |
| 283 | } else { |
| 284 | if (remote_private) { |
| 285 | return kIceCandidatePairHostPublicHostPrivate; |
| 286 | } else { |
| 287 | return kIceCandidatePairHostPublicHostPublic; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | if (l == host && r == srflx) |
| 292 | return kIceCandidatePairHostSrflx; |
| 293 | if (l == host && r == relay) |
| 294 | return kIceCandidatePairHostRelay; |
| 295 | if (l == host && r == prflx) |
| 296 | return kIceCandidatePairHostPrflx; |
| 297 | if (l == srflx && r == host) |
| 298 | return kIceCandidatePairSrflxHost; |
| 299 | if (l == srflx && r == srflx) |
| 300 | return kIceCandidatePairSrflxSrflx; |
| 301 | if (l == srflx && r == relay) |
| 302 | return kIceCandidatePairSrflxRelay; |
| 303 | if (l == srflx && r == prflx) |
| 304 | return kIceCandidatePairSrflxPrflx; |
| 305 | if (l == relay && r == host) |
| 306 | return kIceCandidatePairRelayHost; |
| 307 | if (l == relay && r == srflx) |
| 308 | return kIceCandidatePairRelaySrflx; |
| 309 | if (l == relay && r == relay) |
| 310 | return kIceCandidatePairRelayRelay; |
| 311 | if (l == relay && r == prflx) |
| 312 | return kIceCandidatePairRelayPrflx; |
| 313 | if (l == prflx && r == host) |
| 314 | return kIceCandidatePairPrflxHost; |
| 315 | if (l == prflx && r == srflx) |
| 316 | return kIceCandidatePairPrflxSrflx; |
| 317 | if (l == prflx && r == relay) |
| 318 | return kIceCandidatePairPrflxRelay; |
| 319 | return kIceCandidatePairMax; |
| 320 | } |
| 321 | |
| 322 | // Verify that the order of media sections in |new_desc| matches |
| 323 | // |existing_desc|. The number of m= sections in |new_desc| should be no less |
| 324 | // than |existing_desc|. |
| 325 | bool MediaSectionsInSameOrder(const SessionDescription* existing_desc, |
| 326 | const SessionDescription* new_desc) { |
| 327 | if (!existing_desc || !new_desc) { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | if (existing_desc->contents().size() > new_desc->contents().size()) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | for (size_t i = 0; i < existing_desc->contents().size(); ++i) { |
| 336 | if (new_desc->contents()[i].name != existing_desc->contents()[i].name) { |
| 337 | return false; |
| 338 | } |
| 339 | const MediaContentDescription* new_desc_mdesc = |
| 340 | static_cast<const MediaContentDescription*>( |
| 341 | new_desc->contents()[i].description); |
| 342 | const MediaContentDescription* existing_desc_mdesc = |
| 343 | static_cast<const MediaContentDescription*>( |
| 344 | existing_desc->contents()[i].description); |
| 345 | if (new_desc_mdesc->type() != existing_desc_mdesc->type()) { |
| 346 | return false; |
| 347 | } |
| 348 | } |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | bool MediaSectionsHaveSameCount(const SessionDescription* desc1, |
| 353 | const SessionDescription* desc2) { |
| 354 | if (!desc1 || !desc2) { |
| 355 | return false; |
| 356 | } |
| 357 | return desc1->contents().size() == desc2->contents().size(); |
| 358 | } |
| 359 | |
| 360 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 361 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 362 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 363 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 364 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 365 | // by Channel's |srtp_required| check. |
| 366 | bool VerifyCrypto(const SessionDescription* desc, |
| 367 | bool dtls_enabled, |
| 368 | std::string* error) { |
| 369 | const cricket::ContentGroup* bundle = |
| 370 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 371 | const ContentInfos& contents = desc->contents(); |
| 372 | for (size_t index = 0; index < contents.size(); ++index) { |
| 373 | const ContentInfo* cinfo = &contents[index]; |
| 374 | if (cinfo->rejected) { |
| 375 | continue; |
| 376 | } |
| 377 | if (bundle && bundle->HasContentName(cinfo->name) && |
| 378 | cinfo->name != *(bundle->FirstContentName())) { |
| 379 | // This isn't the first media section in the BUNDLE group, so it's not |
| 380 | // required to have crypto attributes, since only the crypto attributes |
| 381 | // from the first section actually get used. |
| 382 | continue; |
| 383 | } |
| 384 | |
| 385 | // If the content isn't rejected or bundled into another m= section, crypto |
| 386 | // must be present. |
| 387 | const MediaContentDescription* media = |
| 388 | static_cast<const MediaContentDescription*>(cinfo->description); |
| 389 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 390 | if (!media || !tinfo) { |
| 391 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 392 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 393 | *error = kInvalidSdp; |
| 394 | return false; |
| 395 | } |
| 396 | if (dtls_enabled) { |
| 397 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 398 | RTC_LOG(LS_WARNING) |
| 399 | << "Session description must have DTLS fingerprint if " |
| 400 | "DTLS enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 401 | *error = kSdpWithoutDtlsFingerprint; |
| 402 | return false; |
| 403 | } |
| 404 | } else { |
| 405 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 406 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 407 | << "Session description must have SDES when DTLS disabled."; |
| 408 | *error = kSdpWithoutSdesCrypto; |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 418 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 419 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 420 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 421 | const cricket::ContentGroup* bundle = |
| 422 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 423 | const ContentInfos& contents = desc->contents(); |
| 424 | for (size_t index = 0; index < contents.size(); ++index) { |
| 425 | const ContentInfo* cinfo = &contents[index]; |
| 426 | if (cinfo->rejected) { |
| 427 | continue; |
| 428 | } |
| 429 | if (bundle && bundle->HasContentName(cinfo->name) && |
| 430 | cinfo->name != *(bundle->FirstContentName())) { |
| 431 | // This isn't the first media section in the BUNDLE group, so it's not |
| 432 | // required to have ufrag/password, since only the ufrag/password from |
| 433 | // the first section actually get used. |
| 434 | continue; |
| 435 | } |
| 436 | |
| 437 | // If the content isn't rejected or bundled into another m= section, |
| 438 | // ice-ufrag and ice-pwd must be present. |
| 439 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 440 | if (!tinfo) { |
| 441 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 442 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 443 | return false; |
| 444 | } |
| 445 | if (tinfo->description.ice_ufrag.empty() || |
| 446 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 447 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 448 | return false; |
| 449 | } |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 455 | uint32_t ssrc, |
| 456 | std::string* track_id) { |
| 457 | RTC_DCHECK(track_id != NULL); |
| 458 | |
| 459 | const cricket::ContentInfo* audio_info = |
| 460 | cricket::GetFirstAudioContent(session_description); |
| 461 | if (audio_info) { |
| 462 | const cricket::MediaContentDescription* audio_content = |
| 463 | static_cast<const cricket::MediaContentDescription*>( |
| 464 | audio_info->description); |
| 465 | |
| 466 | const auto* found = |
| 467 | cricket::GetStreamBySsrc(audio_content->streams(), ssrc); |
| 468 | if (found) { |
| 469 | *track_id = found->id; |
| 470 | return true; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | const cricket::ContentInfo* video_info = |
| 475 | cricket::GetFirstVideoContent(session_description); |
| 476 | if (video_info) { |
| 477 | const cricket::MediaContentDescription* video_content = |
| 478 | static_cast<const cricket::MediaContentDescription*>( |
| 479 | video_info->description); |
| 480 | |
| 481 | const auto* found = |
| 482 | cricket::GetStreamBySsrc(video_content->streams(), ssrc); |
| 483 | if (found) { |
| 484 | *track_id = found->id; |
| 485 | return true; |
| 486 | } |
| 487 | } |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | // Get the SCTP port out of a SessionDescription. |
| 492 | // Return -1 if not found. |
| 493 | int GetSctpPort(const SessionDescription* session_description) { |
| 494 | const ContentInfo* content_info = GetFirstDataContent(session_description); |
| 495 | RTC_DCHECK(content_info); |
| 496 | if (!content_info) { |
| 497 | return -1; |
| 498 | } |
| 499 | const cricket::DataContentDescription* data = |
| 500 | static_cast<const cricket::DataContentDescription*>( |
| 501 | (content_info->description)); |
| 502 | std::string value; |
| 503 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 504 | cricket::kGoogleSctpDataCodecName); |
| 505 | for (const cricket::DataCodec& codec : data->codecs()) { |
| 506 | if (!codec.Matches(match_pattern)) { |
| 507 | continue; |
| 508 | } |
| 509 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 510 | return rtc::FromString<int>(value); |
| 511 | } |
| 512 | } |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | bool BadSdp(const std::string& source, |
| 517 | const std::string& type, |
| 518 | const std::string& reason, |
| 519 | std::string* err_desc) { |
| 520 | std::ostringstream desc; |
| 521 | desc << "Failed to set " << source; |
| 522 | if (!type.empty()) { |
| 523 | desc << " " << type; |
| 524 | } |
| 525 | desc << " sdp: " << reason; |
| 526 | |
| 527 | if (err_desc) { |
| 528 | *err_desc = desc.str(); |
| 529 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 530 | RTC_LOG(LS_ERROR) << desc.str(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 531 | return false; |
| 532 | } |
| 533 | |
| 534 | bool BadSdp(cricket::ContentSource source, |
| 535 | const std::string& type, |
| 536 | const std::string& reason, |
| 537 | std::string* err_desc) { |
| 538 | if (source == cricket::CS_LOCAL) { |
| 539 | return BadSdp("local", type, reason, err_desc); |
| 540 | } else { |
| 541 | return BadSdp("remote", type, reason, err_desc); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | bool BadLocalSdp(const std::string& type, |
| 546 | const std::string& reason, |
| 547 | std::string* err_desc) { |
| 548 | return BadSdp(cricket::CS_LOCAL, type, reason, err_desc); |
| 549 | } |
| 550 | |
| 551 | bool BadRemoteSdp(const std::string& type, |
| 552 | const std::string& reason, |
| 553 | std::string* err_desc) { |
| 554 | return BadSdp(cricket::CS_REMOTE, type, reason, err_desc); |
| 555 | } |
| 556 | |
| 557 | bool BadOfferSdp(cricket::ContentSource source, |
| 558 | const std::string& reason, |
| 559 | std::string* err_desc) { |
| 560 | return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc); |
| 561 | } |
| 562 | |
| 563 | bool BadPranswerSdp(cricket::ContentSource source, |
| 564 | const std::string& reason, |
| 565 | std::string* err_desc) { |
| 566 | return BadSdp(source, SessionDescriptionInterface::kPrAnswer, reason, |
| 567 | err_desc); |
| 568 | } |
| 569 | |
| 570 | bool BadAnswerSdp(cricket::ContentSource source, |
| 571 | const std::string& reason, |
| 572 | std::string* err_desc) { |
| 573 | return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc); |
| 574 | } |
| 575 | |
| 576 | std::string BadStateErrMsg(PeerConnectionInterface::SignalingState state) { |
| 577 | std::ostringstream desc; |
| 578 | desc << "Called in wrong state: " << GetSignalingStateString(state); |
| 579 | return desc.str(); |
| 580 | } |
| 581 | |
| 582 | #define GET_STRING_OF_ERROR_CODE(err) \ |
| 583 | case webrtc::PeerConnection::err: \ |
| 584 | result = #err; \ |
| 585 | break; |
| 586 | |
| 587 | std::string GetErrorCodeString(webrtc::PeerConnection::Error err) { |
| 588 | std::string result; |
| 589 | switch (err) { |
| 590 | GET_STRING_OF_ERROR_CODE(ERROR_NONE) |
| 591 | GET_STRING_OF_ERROR_CODE(ERROR_CONTENT) |
| 592 | GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT) |
| 593 | default: |
| 594 | RTC_NOTREACHED(); |
| 595 | break; |
| 596 | } |
| 597 | return result; |
| 598 | } |
| 599 | |
| 600 | std::string MakeErrorString(const std::string& error, const std::string& desc) { |
| 601 | std::ostringstream ret; |
| 602 | ret << error << " " << desc; |
| 603 | return ret.str(); |
| 604 | } |
| 605 | |
| 606 | std::string MakeTdErrorString(const std::string& desc) { |
| 607 | return MakeErrorString(kPushDownTDFailed, desc); |
| 608 | } |
| 609 | |
| 610 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 611 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 612 | const SessionDescriptionInterface* new_desc, |
| 613 | const std::string& content_name) { |
| 614 | if (!old_desc) { |
| 615 | return false; |
| 616 | } |
| 617 | const SessionDescription* new_sd = new_desc->description(); |
| 618 | const SessionDescription* old_sd = old_desc->description(); |
| 619 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 620 | if (!cinfo || cinfo->rejected) { |
| 621 | return false; |
| 622 | } |
| 623 | // If the content isn't rejected, check if ufrag and password has changed. |
| 624 | const cricket::TransportDescription* new_transport_desc = |
| 625 | new_sd->GetTransportDescriptionByName(content_name); |
| 626 | const cricket::TransportDescription* old_transport_desc = |
| 627 | old_sd->GetTransportDescriptionByName(content_name); |
| 628 | if (!new_transport_desc || !old_transport_desc) { |
| 629 | // No transport description exists. This is not an ICE restart. |
| 630 | return false; |
| 631 | } |
| 632 | if (cricket::IceCredentialsChanged( |
| 633 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 634 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 635 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 636 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 637 | return true; |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | } // namespace |
| 643 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 644 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 645 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 646 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 647 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 648 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 649 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 650 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 651 | BundlePolicy bundle_policy; |
| 652 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 653 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 654 | int ice_candidate_pool_size; |
| 655 | bool disable_ipv6; |
| 656 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 657 | int max_ipv6_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 658 | bool enable_rtp_data_channel; |
| 659 | rtc::Optional<int> screencast_min_bitrate; |
| 660 | rtc::Optional<bool> combined_audio_video_bwe; |
| 661 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 662 | TcpCandidatePolicy tcp_candidate_policy; |
| 663 | CandidateNetworkPolicy candidate_network_policy; |
| 664 | int audio_jitter_buffer_max_packets; |
| 665 | bool audio_jitter_buffer_fast_accelerate; |
| 666 | int ice_connection_receiving_timeout; |
| 667 | int ice_backup_candidate_pair_ping_interval; |
| 668 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 669 | bool prioritize_most_likely_ice_candidate_pairs; |
| 670 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 671 | bool prune_turn_ports; |
| 672 | bool presume_writable_when_fully_relayed; |
| 673 | bool enable_ice_renomination; |
| 674 | bool redetermine_role_on_ice_restart; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 675 | rtc::Optional<int> ice_check_min_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 676 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 677 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame^] | 678 | SdpSemantics sdp_semantics; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 679 | }; |
| 680 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 681 | "Did you add something to RTCConfiguration and forget to " |
| 682 | "update operator==?"); |
| 683 | return type == o.type && servers == o.servers && |
| 684 | bundle_policy == o.bundle_policy && |
| 685 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 686 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 687 | candidate_network_policy == o.candidate_network_policy && |
| 688 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 689 | audio_jitter_buffer_fast_accelerate == |
| 690 | o.audio_jitter_buffer_fast_accelerate && |
| 691 | ice_connection_receiving_timeout == |
| 692 | o.ice_connection_receiving_timeout && |
| 693 | ice_backup_candidate_pair_ping_interval == |
| 694 | o.ice_backup_candidate_pair_ping_interval && |
| 695 | continual_gathering_policy == o.continual_gathering_policy && |
| 696 | certificates == o.certificates && |
| 697 | prioritize_most_likely_ice_candidate_pairs == |
| 698 | o.prioritize_most_likely_ice_candidate_pairs && |
| 699 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 700 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 701 | max_ipv6_networks == o.max_ipv6_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 702 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 703 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 704 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 705 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 706 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 707 | prune_turn_ports == o.prune_turn_ports && |
| 708 | presume_writable_when_fully_relayed == |
| 709 | o.presume_writable_when_fully_relayed && |
| 710 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 711 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 712 | ice_check_min_interval == o.ice_check_min_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 713 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame^] | 714 | turn_customizer == o.turn_customizer && |
| 715 | sdp_semantics == o.sdp_semantics; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 719 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 720 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 721 | } |
| 722 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 723 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 724 | std::string GenerateRtcpCname() { |
| 725 | std::string cname; |
| 726 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 727 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 728 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 729 | } |
| 730 | return cname; |
| 731 | } |
| 732 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 733 | bool ValidateOfferAnswerOptions( |
| 734 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 735 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 736 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 737 | } |
| 738 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 739 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 740 | // m= sections (in other words, nothing that involves a map/array). |
| 741 | void ExtractSharedMediaSessionOptions( |
| 742 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 743 | cricket::MediaSessionOptions* session_options) { |
| 744 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 745 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 746 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 747 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 748 | bool ConvertConstraintsToOfferAnswerOptions( |
| 749 | const MediaConstraintsInterface* constraints, |
| 750 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 751 | if (!constraints) { |
| 752 | return true; |
| 753 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 754 | |
| 755 | bool value = false; |
| 756 | size_t mandatory_constraints_satisfied = 0; |
| 757 | |
| 758 | if (FindConstraint(constraints, |
| 759 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 760 | &mandatory_constraints_satisfied)) { |
| 761 | offer_answer_options->offer_to_receive_audio = |
| 762 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 763 | kOfferToReceiveMediaTrue |
| 764 | : 0; |
| 765 | } |
| 766 | |
| 767 | if (FindConstraint(constraints, |
| 768 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 769 | &mandatory_constraints_satisfied)) { |
| 770 | offer_answer_options->offer_to_receive_video = |
| 771 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 772 | kOfferToReceiveMediaTrue |
| 773 | : 0; |
| 774 | } |
| 775 | if (FindConstraint(constraints, |
| 776 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 777 | &mandatory_constraints_satisfied)) { |
| 778 | offer_answer_options->voice_activity_detection = value; |
| 779 | } |
| 780 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 781 | &mandatory_constraints_satisfied)) { |
| 782 | offer_answer_options->use_rtp_mux = value; |
| 783 | } |
| 784 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 785 | &value, &mandatory_constraints_satisfied)) { |
| 786 | offer_answer_options->ice_restart = value; |
| 787 | } |
| 788 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 789 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 790 | } |
| 791 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 792 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 793 | std::unique_ptr<RtcEventLog> event_log, |
| 794 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 795 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 796 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 797 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 798 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 799 | remote_streams_(StreamCollection::Create()), |
| 800 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 801 | |
| 802 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 803 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 804 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 805 | |
| 806 | // Need to detach RTP transceivers from PeerConnection, since it's about to be |
| 807 | // destroyed. |
| 808 | for (auto transceiver : transceivers_) { |
| 809 | transceiver->Stop(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 810 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 811 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 812 | // Destroy stats_ because it depends on session_. |
| 813 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 814 | if (stats_collector_) { |
| 815 | stats_collector_->WaitForPendingRequest(); |
| 816 | stats_collector_ = nullptr; |
| 817 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 818 | |
| 819 | // Destroy video channels first since they may have a pointer to a voice |
| 820 | // channel. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 821 | for (auto transceiver : transceivers_) { |
| 822 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_VIDEO && |
| 823 | transceiver->internal()->channel()) { |
| 824 | DestroyVideoChannel(static_cast<cricket::VideoChannel*>( |
| 825 | transceiver->internal()->channel())); |
| 826 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 827 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 828 | for (auto transceiver : transceivers_) { |
| 829 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_AUDIO && |
| 830 | transceiver->internal()->channel()) { |
| 831 | DestroyVoiceChannel(static_cast<cricket::VoiceChannel*>( |
| 832 | transceiver->internal()->channel())); |
| 833 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 834 | } |
| 835 | if (rtp_data_channel_) { |
| 836 | DestroyDataChannel(); |
| 837 | } |
| 838 | |
| 839 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 840 | // grab a reference to this PeerConnection. The RefCountedObject vtable will |
| 841 | // have already been destroyed (since it is a subclass of PeerConnection) and |
| 842 | // using rtc::Bind will cause "Pure virtual function called" error to appear. |
| 843 | |
| 844 | if (sctp_transport_) { |
| 845 | OnDataChannelDestroyed(); |
| 846 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 847 | [this] { DestroySctpTransport_n(); }); |
| 848 | } |
| 849 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 850 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 851 | |
| 852 | webrtc_session_desc_factory_.reset(); |
| 853 | sctp_invoker_.reset(); |
| 854 | sctp_factory_.reset(); |
| 855 | transport_controller_.reset(); |
| 856 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 857 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 858 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 859 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 860 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 861 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 862 | call_.reset(); |
| 863 | event_log_.reset(); |
| 864 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 868 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 869 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 870 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 871 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 872 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 873 | |
| 874 | RTCError config_error = ValidateConfiguration(configuration); |
| 875 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 876 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 877 | return false; |
| 878 | } |
| 879 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 880 | if (!allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 881 | RTC_LOG(LS_ERROR) |
| 882 | << "PeerConnection initialized without a PortAllocator? " |
| 883 | << "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 884 | return false; |
| 885 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 886 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 887 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 888 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 889 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
| 890 | << "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 891 | return false; |
| 892 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 893 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 894 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 895 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 896 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 897 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 898 | if (!network_thread()->Invoke<bool>( |
| 899 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 900 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 901 | return false; |
| 902 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 903 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 904 | // RFC 3264: The numeric value of the session id and version in the |
| 905 | // o line MUST be representable with a "64 bit signed integer". |
| 906 | // Due to this constraint session id |session_id_| is max limited to |
| 907 | // LLONG_MAX. |
| 908 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
| 909 | transport_controller_.reset(factory_->CreateTransportController( |
| 910 | port_allocator_.get(), configuration.redetermine_role_on_ice_restart)); |
| 911 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 912 | transport_controller_->SignalConnectionState.connect( |
| 913 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 914 | transport_controller_->SignalGatheringState.connect( |
| 915 | this, &PeerConnection::OnTransportControllerGatheringState); |
| 916 | transport_controller_->SignalCandidatesGathered.connect( |
| 917 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
| 918 | transport_controller_->SignalCandidatesRemoved.connect( |
| 919 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 920 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 921 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 922 | |
| 923 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 924 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 925 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 926 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 927 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 928 | configuration_ = configuration; |
| 929 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 930 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 931 | |
| 932 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
| 933 | |
| 934 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 935 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 936 | if (!configuration.certificates.empty()) { |
| 937 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 938 | // just picking the first one. The decision should be made based on the DTLS |
| 939 | // handshake. The DTLS negotiations need to know about all certificates. |
| 940 | certificate = configuration.certificates[0]; |
| 941 | } |
| 942 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 943 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 944 | |
| 945 | if (options.disable_encryption) { |
| 946 | dtls_enabled_ = false; |
| 947 | } else { |
| 948 | // Enable DTLS by default if we have an identity store or a certificate. |
| 949 | dtls_enabled_ = (cert_generator || certificate); |
| 950 | // |configuration| can override the default |dtls_enabled_| value. |
| 951 | if (configuration.enable_dtls_srtp) { |
| 952 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 957 | // It takes precendence over the disable_sctp_data_channels |
| 958 | // PeerConnectionFactoryInterface::Options. |
| 959 | if (configuration.enable_rtp_data_channel) { |
| 960 | data_channel_type_ = cricket::DCT_RTP; |
| 961 | } else { |
| 962 | // DTLS has to be enabled to use SCTP. |
| 963 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 964 | data_channel_type_ = cricket::DCT_SCTP; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | video_options_.screencast_min_bitrate_kbps = |
| 969 | configuration.screencast_min_bitrate; |
| 970 | audio_options_.combined_audio_video_bwe = |
| 971 | configuration.combined_audio_video_bwe; |
| 972 | |
| 973 | audio_options_.audio_jitter_buffer_max_packets = |
| 974 | rtc::Optional<int>(configuration.audio_jitter_buffer_max_packets); |
| 975 | |
| 976 | audio_options_.audio_jitter_buffer_fast_accelerate = |
| 977 | rtc::Optional<bool>(configuration.audio_jitter_buffer_fast_accelerate); |
| 978 | |
| 979 | // Whether the certificate generator/certificate is null or not determines |
| 980 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 981 | // the right instructions by clearing the variables if needed. |
| 982 | if (!dtls_enabled_) { |
| 983 | cert_generator.reset(); |
| 984 | certificate = nullptr; |
| 985 | } else if (certificate) { |
| 986 | // Favor generated certificate over the certificate generator. |
| 987 | cert_generator.reset(); |
| 988 | } |
| 989 | |
| 990 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 991 | signaling_thread(), channel_manager(), this, session_id(), |
| 992 | std::move(cert_generator), certificate)); |
| 993 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 994 | this, &PeerConnection::OnCertificateReady); |
| 995 | |
| 996 | if (options.disable_encryption) { |
| 997 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 998 | } |
| 999 | |
| 1000 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 1001 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1002 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1003 | // Add default audio/video transceivers for Plan B SDP. |
| 1004 | if (!IsUnifiedPlan()) { |
| 1005 | transceivers_.push_back( |
| 1006 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1007 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1008 | transceivers_.push_back( |
| 1009 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1010 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1011 | } |
| 1012 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1013 | return true; |
| 1014 | } |
| 1015 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1016 | RTCError PeerConnection::ValidateConfiguration( |
| 1017 | const RTCConfiguration& config) const { |
| 1018 | if (config.ice_regather_interval_range && |
| 1019 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1020 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1021 | "ice_regather_interval_range specified but continual " |
| 1022 | "gathering policy is GATHER_ONCE"); |
| 1023 | } |
| 1024 | return RTCError::OK(); |
| 1025 | } |
| 1026 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1027 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1028 | PeerConnection::local_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1029 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1032 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1033 | PeerConnection::remote_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1034 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1037 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1038 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1039 | if (IsClosed()) { |
| 1040 | return false; |
| 1041 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1042 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1043 | return false; |
| 1044 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1045 | |
| 1046 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1047 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1048 | observer->SignalAudioTrackAdded.connect(this, |
| 1049 | &PeerConnection::OnAudioTrackAdded); |
| 1050 | observer->SignalAudioTrackRemoved.connect( |
| 1051 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1052 | observer->SignalVideoTrackAdded.connect(this, |
| 1053 | &PeerConnection::OnVideoTrackAdded); |
| 1054 | observer->SignalVideoTrackRemoved.connect( |
| 1055 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1056 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1057 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1058 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1059 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1060 | } |
| 1061 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1062 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1065 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1066 | observer_->OnRenegotiationNeeded(); |
| 1067 | return true; |
| 1068 | } |
| 1069 | |
| 1070 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1071 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1072 | if (!IsClosed()) { |
| 1073 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1074 | RemoveAudioTrack(track.get(), local_stream); |
| 1075 | } |
| 1076 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1077 | RemoveVideoTrack(track.get(), local_stream); |
| 1078 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1079 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1080 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1081 | stream_observers_.erase( |
| 1082 | std::remove_if( |
| 1083 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1084 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1085 | return observer->stream()->label().compare(local_stream->label()) == |
| 1086 | 0; |
| 1087 | }), |
| 1088 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1089 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1090 | if (IsClosed()) { |
| 1091 | return; |
| 1092 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1093 | observer_->OnRenegotiationNeeded(); |
| 1094 | } |
| 1095 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1096 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1097 | MediaStreamTrackInterface* track, |
| 1098 | std::vector<MediaStreamInterface*> streams) { |
| 1099 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
| 1100 | if (IsClosed()) { |
| 1101 | return nullptr; |
| 1102 | } |
| 1103 | if (streams.size() >= 2) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1104 | RTC_LOG(LS_ERROR) |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1105 | << "Adding a track with two streams is not currently supported."; |
| 1106 | return nullptr; |
| 1107 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1108 | if (FindSenderForTrack(track)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1109 | RTC_LOG(LS_ERROR) << "Sender for track " << track->id() |
| 1110 | << " already exists."; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1111 | return nullptr; |
| 1112 | } |
| 1113 | |
| 1114 | // TODO(deadbeef): Support adding a track to multiple streams. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1115 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1116 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1117 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1118 | signaling_thread(), |
| 1119 | new AudioRtpSender(static_cast<AudioTrackInterface*>(track), |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1120 | voice_channel(), stats_.get())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1121 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1122 | if (!streams.empty()) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1123 | new_sender->internal()->set_stream_id(streams[0]->label()); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1124 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1125 | const RtpSenderInfo* sender_info = |
| 1126 | FindSenderInfo(local_audio_sender_infos_, |
| 1127 | new_sender->internal()->stream_id(), track->id()); |
| 1128 | if (sender_info) { |
| 1129 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1130 | } |
| 1131 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1132 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1133 | signaling_thread(), |
| 1134 | new VideoRtpSender(static_cast<VideoTrackInterface*>(track), |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1135 | video_channel())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1136 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1137 | if (!streams.empty()) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1138 | new_sender->internal()->set_stream_id(streams[0]->label()); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1139 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1140 | const RtpSenderInfo* sender_info = |
| 1141 | FindSenderInfo(local_video_sender_infos_, |
| 1142 | new_sender->internal()->stream_id(), track->id()); |
| 1143 | if (sender_info) { |
| 1144 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1145 | } |
| 1146 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1147 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " |
| 1148 | << track->kind(); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1149 | return rtc::scoped_refptr<RtpSenderInterface>(); |
| 1150 | } |
| 1151 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1152 | observer_->OnRenegotiationNeeded(); |
| 1153 | return new_sender; |
| 1154 | } |
| 1155 | |
| 1156 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1157 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
| 1158 | if (IsClosed()) { |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1162 | bool removed; |
| 1163 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1164 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1165 | } else { |
| 1166 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1167 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1168 | } |
| 1169 | if (!removed) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1170 | RTC_LOG(LS_ERROR) << "Couldn't find sender " << sender->id() |
| 1171 | << " to remove."; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1174 | |
| 1175 | observer_->OnRenegotiationNeeded(); |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1179 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1180 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1181 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1182 | if (IsClosed()) { |
| 1183 | return nullptr; |
| 1184 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1185 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1186 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1187 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1188 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1189 | auto track_sender = FindSenderForTrack(track); |
| 1190 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1191 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1192 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1195 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1198 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1199 | const std::string& kind, |
| 1200 | const std::string& stream_id) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1201 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1202 | if (IsClosed()) { |
| 1203 | return nullptr; |
| 1204 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1205 | |
| 1206 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1207 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1208 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1209 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1210 | signaling_thread(), new AudioRtpSender(voice_channel(), stats_.get())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1211 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1212 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1213 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1214 | signaling_thread(), new VideoRtpSender(video_channel())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1215 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1216 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1217 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1218 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1219 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1220 | |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1221 | if (!stream_id.empty()) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1222 | new_sender->internal()->set_stream_id(stream_id); |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1223 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1224 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1225 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1226 | } |
| 1227 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1228 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1229 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1230 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1231 | for (auto sender : GetSendersInternal()) { |
| 1232 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1233 | } |
| 1234 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1237 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1238 | PeerConnection::GetSendersInternal() const { |
| 1239 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1240 | all_senders; |
| 1241 | for (auto transceiver : transceivers_) { |
| 1242 | auto senders = transceiver->internal()->senders(); |
| 1243 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1244 | } |
| 1245 | return all_senders; |
| 1246 | } |
| 1247 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1248 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1249 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1250 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1251 | for (const auto& receiver : GetReceiversInternal()) { |
| 1252 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1253 | } |
| 1254 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1257 | std::vector< |
| 1258 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1259 | PeerConnection::GetReceiversInternal() const { |
| 1260 | std::vector< |
| 1261 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1262 | all_receivers; |
| 1263 | for (auto transceiver : transceivers_) { |
| 1264 | auto receivers = transceiver->internal()->receivers(); |
| 1265 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1266 | receivers.end()); |
| 1267 | } |
| 1268 | return all_receivers; |
| 1269 | } |
| 1270 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1271 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1272 | MediaStreamTrackInterface* track, |
| 1273 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1274 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1275 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1276 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1277 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1278 | return false; |
| 1279 | } |
| 1280 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1281 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1282 | // The StatsCollector is used to tell if a track is valid because it may |
| 1283 | // remember tracks that the PeerConnection previously removed. |
| 1284 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1285 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1286 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1287 | return false; |
| 1288 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1289 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1290 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1291 | return true; |
| 1292 | } |
| 1293 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1294 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
| 1295 | RTC_DCHECK(stats_collector_); |
| 1296 | stats_collector_->GetStatsReport(callback); |
| 1297 | } |
| 1298 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1299 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1300 | return signaling_state_; |
| 1301 | } |
| 1302 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1303 | PeerConnectionInterface::IceConnectionState |
| 1304 | PeerConnection::ice_connection_state() { |
| 1305 | return ice_connection_state_; |
| 1306 | } |
| 1307 | |
| 1308 | PeerConnectionInterface::IceGatheringState |
| 1309 | PeerConnection::ice_gathering_state() { |
| 1310 | return ice_gathering_state_; |
| 1311 | } |
| 1312 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1313 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1314 | PeerConnection::CreateDataChannel( |
| 1315 | const std::string& label, |
| 1316 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1317 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1318 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1319 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1320 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1321 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1322 | if (config) { |
| 1323 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1324 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1325 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1326 | InternalCreateDataChannel(label, internal_config.get())); |
| 1327 | if (!channel.get()) { |
| 1328 | return nullptr; |
| 1329 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1330 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1331 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1332 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1333 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1334 | observer_->OnRenegotiationNeeded(); |
| 1335 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1336 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1337 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1338 | } |
| 1339 | |
| 1340 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1341 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1342 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1343 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1344 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1345 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1346 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1347 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1348 | // |constraints| and some downstream applications might create offers with |
| 1349 | // mandatory fields which would not be parsed in the helper method. For |
| 1350 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1351 | // |constraints| as a mandatory field but it is not parsed. |
| 1352 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1353 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1354 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1358 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1359 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1360 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1361 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1362 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1363 | return; |
| 1364 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1365 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1366 | if (IsClosed()) { |
| 1367 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1368 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1369 | PostCreateSessionDescriptionFailure(observer, error); |
| 1370 | return; |
| 1371 | } |
| 1372 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1373 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1374 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1375 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1376 | PostCreateSessionDescriptionFailure(observer, error); |
| 1377 | return; |
| 1378 | } |
| 1379 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1380 | cricket::MediaSessionOptions session_options; |
| 1381 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1382 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | void PeerConnection::CreateAnswer( |
| 1386 | CreateSessionDescriptionObserver* observer, |
| 1387 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1388 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1389 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1390 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1391 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1392 | return; |
| 1393 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1394 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1395 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1396 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1397 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1398 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1399 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1400 | PostCreateSessionDescriptionFailure(observer, error); |
| 1401 | return; |
| 1402 | } |
| 1403 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1404 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1408 | const RTCOfferAnswerOptions& options) { |
| 1409 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1410 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1411 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1412 | return; |
| 1413 | } |
| 1414 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1415 | if (IsClosed()) { |
| 1416 | std::string error = "CreateAnswer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1417 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1418 | PostCreateSessionDescriptionFailure(observer, error); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1422 | if (remote_description() && |
| 1423 | remote_description()->type() != SessionDescriptionInterface::kOffer) { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1424 | std::string error = "CreateAnswer called without remote offer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1425 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1426 | PostCreateSessionDescriptionFailure(observer, error); |
| 1427 | return; |
| 1428 | } |
| 1429 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1430 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1431 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1432 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1433 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | void PeerConnection::SetLocalDescription( |
| 1437 | SetSessionDescriptionObserver* observer, |
| 1438 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1439 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1440 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1441 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1442 | return; |
| 1443 | } |
| 1444 | if (!desc) { |
| 1445 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1446 | return; |
| 1447 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1448 | |
| 1449 | // Takes the ownership of |desc| regardless of the result. |
| 1450 | std::unique_ptr<SessionDescriptionInterface> desc_temp(desc); |
| 1451 | |
| 1452 | if (IsClosed()) { |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1453 | std::string error = "Failed to set local " + desc_temp->type() + |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1454 | " sdp: Called in wrong state: STATE_CLOSED"; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1455 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1456 | PostSetSessionDescriptionFailure(observer, error); |
| 1457 | return; |
| 1458 | } |
| 1459 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1460 | // Update stats here so that we have the most recent stats for tracks and |
| 1461 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1462 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1463 | std::string error; |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1464 | // Takes the ownership of |desc_temp|. On success, local_description() is |
| 1465 | // updated to reflect the description that was passed in. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1466 | if (!SetLocalDescription(std::move(desc_temp), &error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1467 | PostSetSessionDescriptionFailure(observer, error); |
| 1468 | return; |
| 1469 | } |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1470 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1471 | |
| 1472 | // If setting the description decided our SSL role, allocate any necessary |
| 1473 | // SCTP sids. |
| 1474 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1475 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1476 | AllocateSctpSids(role); |
| 1477 | } |
| 1478 | |
| 1479 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 1480 | // local session description. |
| 1481 | const cricket::ContentInfo* audio_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1482 | GetFirstAudioContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1483 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1484 | if (audio_content->rejected) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1485 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1486 | } else { |
| 1487 | const cricket::AudioContentDescription* audio_desc = |
| 1488 | static_cast<const cricket::AudioContentDescription*>( |
| 1489 | audio_content->description); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1490 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1491 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | const cricket::ContentInfo* video_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1495 | GetFirstVideoContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1496 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1497 | if (video_content->rejected) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1498 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1499 | } else { |
| 1500 | const cricket::VideoContentDescription* video_desc = |
| 1501 | static_cast<const cricket::VideoContentDescription*>( |
| 1502 | video_content->description); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1503 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1504 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1508 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1509 | if (data_content) { |
| 1510 | const cricket::DataContentDescription* data_desc = |
| 1511 | static_cast<const cricket::DataContentDescription*>( |
| 1512 | data_content->description); |
| 1513 | if (rtc::starts_with(data_desc->protocol().data(), |
| 1514 | cricket::kMediaProtocolRtpPrefix)) { |
| 1515 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1520 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 1521 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1522 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1523 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1524 | // size is not allowed, and changing the set of ICE servers will not result |
| 1525 | // in new candidates being gathered. |
| 1526 | port_allocator_->FreezeCandidatePool(); |
| 1527 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1528 | // MaybeStartGathering needs to be called after posting |
| 1529 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1530 | // before signaling that SetLocalDescription completed. |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1531 | transport_controller_->MaybeStartGathering(); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1532 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1533 | if (local_description()->type() == SessionDescriptionInterface::kAnswer) { |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1534 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1535 | // MaybeStartGathering... |
| 1536 | network_thread()->Invoke<void>( |
| 1537 | RTC_FROM_HERE, |
| 1538 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1539 | port_allocator_.get())); |
| 1540 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | void PeerConnection::SetRemoteDescription( |
| 1544 | SetSessionDescriptionObserver* observer, |
| 1545 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1546 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1547 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1548 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1549 | return; |
| 1550 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1551 | if (!desc) { |
| 1552 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1553 | return; |
| 1554 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1555 | |
| 1556 | // Takes the ownership of |desc| regardless of the result. |
| 1557 | std::unique_ptr<SessionDescriptionInterface> desc_temp(desc); |
| 1558 | |
| 1559 | if (IsClosed()) { |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1560 | std::string error = "Failed to set remote " + desc_temp->type() + |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1561 | " sdp: Called in wrong state: STATE_CLOSED"; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1562 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1563 | PostSetSessionDescriptionFailure(observer, error); |
| 1564 | return; |
| 1565 | } |
| 1566 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1567 | // Update stats here so that we have the most recent stats for tracks and |
| 1568 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1569 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1570 | std::string error; |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1571 | // Takes the ownership of |desc_temp|. On success, remote_description() is |
| 1572 | // updated to reflect the description that was passed in. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1573 | if (!SetRemoteDescription(std::move(desc_temp), &error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1574 | PostSetSessionDescriptionFailure(observer, error); |
| 1575 | return; |
| 1576 | } |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1577 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1578 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1579 | // If setting the description decided our SSL role, allocate any necessary |
| 1580 | // SCTP sids. |
| 1581 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1582 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1583 | AllocateSctpSids(role); |
| 1584 | } |
| 1585 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1586 | const cricket::ContentInfo* audio_content = |
| 1587 | GetFirstAudioContent(remote_description()->description()); |
| 1588 | const cricket::ContentInfo* video_content = |
| 1589 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1590 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1591 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1592 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1593 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1594 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1595 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1596 | |
| 1597 | // Check if the descriptions include streams, just in case the peer supports |
| 1598 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1599 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1600 | (audio_desc && !audio_desc->streams().empty()) || |
| 1601 | (video_desc && !video_desc->streams().empty())) { |
| 1602 | remote_peer_supports_msid_ = true; |
| 1603 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1604 | |
| 1605 | // We wait to signal new streams until we finish processing the description, |
| 1606 | // since only at that point will new streams have all their tracks. |
| 1607 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 1608 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1609 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 1610 | // rejected media section, there is some cleanup logic that expects the voice/ |
| 1611 | // video channel to still be set. But in this method the voice/video channel |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1612 | // would have been destroyed by the SetRemoteDescription caller above so the |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1613 | // cleanup that relies on them fails to run. The RemoveSenders calls should be |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1614 | // moved to right before the DestroyChannel calls to fix this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1615 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1616 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 1617 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1618 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1619 | if (audio_content->rejected) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1620 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1621 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1622 | bool default_audio_track_needed = |
| 1623 | !remote_peer_supports_msid_ && |
| 1624 | MediaContentDirectionHasSend(audio_desc->direction()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1625 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1626 | default_audio_track_needed, audio_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1627 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1628 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 1632 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1633 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1634 | if (video_content->rejected) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1635 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1636 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1637 | bool default_video_track_needed = |
| 1638 | !remote_peer_supports_msid_ && |
| 1639 | MediaContentDirectionHasSend(video_desc->direction()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1640 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1641 | default_video_track_needed, video_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1642 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1643 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | // Update the DataChannels with the information from the remote peer. |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1647 | if (data_desc) { |
| 1648 | if (rtc::starts_with(data_desc->protocol().data(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1649 | cricket::kMediaProtocolRtpPrefix)) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1650 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 1655 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 1656 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 1657 | stats_->AddStream(new_stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 1658 | observer_->OnAddStream( |
| 1659 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1662 | UpdateEndedRemoteMediaStreams(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1663 | |
| 1664 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1665 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 1666 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1667 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1668 | if (remote_description()->type() == SessionDescriptionInterface::kAnswer) { |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1669 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1670 | // MaybeStartGathering... |
| 1671 | network_thread()->Invoke<void>( |
| 1672 | RTC_FROM_HERE, |
| 1673 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1674 | port_allocator_.get())); |
| 1675 | } |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 1678 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 1679 | return configuration_; |
| 1680 | } |
| 1681 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1682 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 1683 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1684 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 1685 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1686 | if (local_description() && configuration.ice_candidate_pool_size != |
| 1687 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1688 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 1689 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1690 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1691 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1692 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1693 | // The simplest (and most future-compatible) way to tell if the config was |
| 1694 | // modified in an invalid way is to copy each property we do support |
| 1695 | // modifying, then use operator==. There are far more properties we don't |
| 1696 | // support modifying than those we do, and more could be added. |
| 1697 | RTCConfiguration modified_config = configuration_; |
| 1698 | modified_config.servers = configuration.servers; |
| 1699 | modified_config.type = configuration.type; |
| 1700 | modified_config.ice_candidate_pool_size = |
| 1701 | configuration.ice_candidate_pool_size; |
| 1702 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 1703 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 1704 | modified_config.turn_customizer = configuration.turn_customizer; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1705 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1706 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1707 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 1708 | } |
| 1709 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1710 | // Validate the modified configuration. |
| 1711 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 1712 | if (!validate_error.ok()) { |
| 1713 | return SafeSetError(std::move(validate_error), error); |
| 1714 | } |
| 1715 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1716 | // Note that this isn't possible through chromium, since it's an unsigned |
| 1717 | // short in WebIDL. |
| 1718 | if (configuration.ice_candidate_pool_size < 0 || |
| 1719 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 1720 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 1721 | } |
| 1722 | |
| 1723 | // Parse ICE servers before hopping to network thread. |
| 1724 | cricket::ServerAddresses stun_servers; |
| 1725 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1726 | RTCErrorType parse_error = |
| 1727 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 1728 | if (parse_error != RTCErrorType::NONE) { |
| 1729 | return SafeSetError(parse_error, error); |
| 1730 | } |
| 1731 | |
| 1732 | // In theory this shouldn't fail. |
| 1733 | if (!network_thread()->Invoke<bool>( |
| 1734 | RTC_FROM_HERE, |
| 1735 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 1736 | stun_servers, turn_servers, modified_config.type, |
| 1737 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 1738 | modified_config.prune_turn_ports, |
| 1739 | modified_config.turn_customizer))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1740 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1741 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 1742 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1743 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 1744 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 1745 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 1746 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1747 | if (modified_config.servers != configuration_.servers || |
| 1748 | modified_config.type != configuration_.type || |
| 1749 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1750 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 1751 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 1752 | |
| 1753 | if (modified_config.ice_check_min_interval != |
| 1754 | configuration_.ice_check_min_interval) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1755 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 1756 | } |
| 1757 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1758 | configuration_ = modified_config; |
| 1759 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1762 | bool PeerConnection::AddIceCandidate( |
| 1763 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1764 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1765 | if (IsClosed()) { |
| 1766 | return false; |
| 1767 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1768 | |
| 1769 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1770 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
| 1771 | << "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1772 | return false; |
| 1773 | } |
| 1774 | |
| 1775 | if (!ice_candidate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1776 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1777 | return false; |
| 1778 | } |
| 1779 | |
| 1780 | bool valid = false; |
| 1781 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 1782 | if (!valid) { |
| 1783 | return false; |
| 1784 | } |
| 1785 | |
| 1786 | // Add this candidate to the remote session description. |
| 1787 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1788 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1789 | return false; |
| 1790 | } |
| 1791 | |
| 1792 | if (ready) { |
| 1793 | return UseCandidate(ice_candidate); |
| 1794 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1795 | RTC_LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1796 | return true; |
| 1797 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1800 | bool PeerConnection::RemoveIceCandidates( |
| 1801 | const std::vector<cricket::Candidate>& candidates) { |
| 1802 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1803 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1804 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
| 1805 | << "removed without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1806 | return false; |
| 1807 | } |
| 1808 | |
| 1809 | if (candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1810 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | size_t number_removed = |
| 1815 | mutable_remote_description()->RemoveCandidates(candidates); |
| 1816 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1817 | RTC_LOG(LS_ERROR) |
| 1818 | << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
| 1819 | << "Requested " << candidates.size() << " but only " << number_removed |
| 1820 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | // Remove the candidates from the transport controller. |
| 1824 | std::string error; |
| 1825 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 1826 | if (!res && !error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1827 | RTC_LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1828 | } |
| 1829 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1832 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1833 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1834 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1835 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1836 | if (transport_controller()) { |
| 1837 | transport_controller()->SetMetricsObserver(uma_observer_); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1840 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1841 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1842 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1843 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1844 | uma_observer_->IncrementEnumCounter( |
| 1845 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 1846 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 1847 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1848 | uma_observer_->IncrementEnumCounter( |
| 1849 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 1850 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1851 | } |
| 1852 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 1855 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 1856 | if (!worker_thread()->IsCurrent()) { |
| 1857 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 1858 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 1859 | } |
| 1860 | |
| 1861 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 1862 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 1863 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 1864 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 1865 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1866 | "min_bitrate_bps <= 0"); |
| 1867 | } |
| 1868 | if (has_current) { |
| 1869 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 1870 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1871 | "current_bitrate_bps < min_bitrate_bps"); |
| 1872 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 1873 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1874 | "curent_bitrate_bps < 0"); |
| 1875 | } |
| 1876 | } |
| 1877 | if (has_max) { |
| 1878 | if (has_current && |
| 1879 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 1880 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1881 | "max_bitrate_bps < current_bitrate_bps"); |
| 1882 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 1883 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1884 | "max_bitrate_bps < min_bitrate_bps"); |
| 1885 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 1886 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1887 | "max_bitrate_bps < 0"); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | Call::Config::BitrateConfigMask mask; |
| 1892 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 1893 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 1894 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 1895 | |
| 1896 | RTC_DCHECK(call_.get()); |
| 1897 | call_->SetBitrateConfigMask(mask); |
| 1898 | |
| 1899 | return RTCError::OK(); |
| 1900 | } |
| 1901 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 1902 | void PeerConnection::SetBitrateAllocationStrategy( |
| 1903 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 1904 | bitrate_allocation_strategy) { |
| 1905 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 1906 | if (!worker_thread->IsCurrent()) { |
| 1907 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 1908 | bitrate_allocation_strategy.release(); |
| 1909 | auto functor = [this, strategy_raw]() { |
| 1910 | call_->SetBitrateAllocationStrategy( |
| 1911 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 1912 | }; |
| 1913 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 1914 | return; |
| 1915 | } |
| 1916 | RTC_DCHECK(call_.get()); |
| 1917 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 1918 | } |
| 1919 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 1920 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 1921 | if (!worker_thread()->IsCurrent()) { |
| 1922 | worker_thread()->Invoke<void>( |
| 1923 | RTC_FROM_HERE, |
| 1924 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 1925 | return; |
| 1926 | } |
| 1927 | auto audio_state = |
| 1928 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 1929 | audio_state->SetPlayout(playout); |
| 1930 | } |
| 1931 | |
| 1932 | void PeerConnection::SetAudioRecording(bool recording) { |
| 1933 | if (!worker_thread()->IsCurrent()) { |
| 1934 | worker_thread()->Invoke<void>( |
| 1935 | RTC_FROM_HERE, |
| 1936 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 1937 | return; |
| 1938 | } |
| 1939 | auto audio_state = |
| 1940 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 1941 | audio_state->SetRecording(recording); |
| 1942 | } |
| 1943 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 1944 | std::unique_ptr<rtc::SSLCertificate> |
| 1945 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1946 | if (!voice_channel()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 1947 | return nullptr; |
| 1948 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1949 | return GetRemoteSSLCertificate(voice_channel()->transport_name()); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 1952 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 1953 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 1954 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 1955 | const size_t max_size = (max_size_bytes < 0) |
| 1956 | ? RtcEventLog::kUnlimitedOutput |
| 1957 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 1958 | return StartRtcEventLog( |
| 1959 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size)); |
| 1960 | } |
| 1961 | |
| 1962 | bool PeerConnection::StartRtcEventLog( |
| 1963 | std::unique_ptr<RtcEventLogOutput> output) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 1964 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 1965 | struct Functor { |
Zhi Huang | 33c5c7f | 2017-11-17 20:59:29 +0000 | [diff] [blame] | 1966 | bool operator()() { return pc->StartRtcEventLog_w(std::move(output)); } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 1967 | PeerConnection* const pc; |
| 1968 | std::unique_ptr<RtcEventLogOutput> output; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 1969 | }; |
Zhi Huang | 33c5c7f | 2017-11-17 20:59:29 +0000 | [diff] [blame] | 1970 | return worker_thread()->Invoke<bool>(RTC_FROM_HERE, |
| 1971 | Functor{this, std::move(output)}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 1975 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 1976 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 1977 | } |
| 1978 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1979 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1980 | return pending_local_description_ ? pending_local_description_.get() |
| 1981 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1985 | return pending_remote_description_ ? pending_remote_description_.get() |
| 1986 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1989 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 1990 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1991 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 1995 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1996 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 2000 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2001 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 2005 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2006 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2007 | } |
| 2008 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2009 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2010 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2011 | // Update stats here so that we have the most recent stats for tracks and |
| 2012 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2013 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2014 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2015 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 2016 | RemoveUnusedChannels(nullptr); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2017 | RTC_DCHECK(!GetAudioTransceiver()->internal()->channel()); |
| 2018 | RTC_DCHECK(!GetVideoTransceiver()->internal()->channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2019 | RTC_DCHECK(!rtp_data_channel_); |
| 2020 | RTC_DCHECK(!sctp_transport_); |
| 2021 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 2022 | network_thread()->Invoke<void>( |
| 2023 | RTC_FROM_HERE, |
| 2024 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2025 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 2026 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2027 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 2028 | call_.reset(); |
| 2029 | // The event log must outlive call (and any other object that uses it). |
| 2030 | event_log_.reset(); |
| 2031 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2034 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2035 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2036 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 2037 | SetSessionDescriptionMsg* param = |
| 2038 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 2039 | param->observer->OnSuccess(); |
| 2040 | delete param; |
| 2041 | break; |
| 2042 | } |
| 2043 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 2044 | SetSessionDescriptionMsg* param = |
| 2045 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 2046 | param->observer->OnFailure(param->error); |
| 2047 | delete param; |
| 2048 | break; |
| 2049 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2050 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 2051 | CreateSessionDescriptionMsg* param = |
| 2052 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 2053 | param->observer->OnFailure(param->error); |
| 2054 | delete param; |
| 2055 | break; |
| 2056 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2057 | case MSG_GETSTATS: { |
| 2058 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 2059 | StatsReports reports; |
| 2060 | stats_->GetStats(param->track, &reports); |
| 2061 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2062 | delete param; |
| 2063 | break; |
| 2064 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 2065 | case MSG_FREE_DATACHANNELS: { |
| 2066 | sctp_data_channels_to_free_.clear(); |
| 2067 | break; |
| 2068 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2069 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 2070 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2075 | void PeerConnection::CreateAudioReceiver( |
| 2076 | MediaStreamInterface* stream, |
| 2077 | const RtpSenderInfo& remote_sender_info) { |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 2078 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 2079 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 2080 | signaling_thread(), |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2081 | new AudioRtpReceiver(remote_sender_info.sender_id, |
| 2082 | remote_sender_info.first_ssrc, voice_channel())); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 2083 | stream->AddTrack( |
| 2084 | static_cast<AudioTrackInterface*>(receiver->internal()->track().get())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2085 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 2086 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 2087 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
| 2088 | observer_->OnAddTrack(receiver, streams); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2091 | void PeerConnection::CreateVideoReceiver( |
| 2092 | MediaStreamInterface* stream, |
| 2093 | const RtpSenderInfo& remote_sender_info) { |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 2094 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 2095 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2096 | signaling_thread(), |
| 2097 | new VideoRtpReceiver(remote_sender_info.sender_id, worker_thread(), |
| 2098 | remote_sender_info.first_ssrc, video_channel())); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 2099 | stream->AddTrack( |
| 2100 | static_cast<VideoTrackInterface*>(receiver->internal()->track().get())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2101 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 2102 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 2103 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
| 2104 | observer_->OnAddTrack(receiver, streams); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2107 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 2108 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2109 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2110 | const RtpSenderInfo& remote_sender_info) { |
| 2111 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 2112 | if (!receiver) { |
| 2113 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 2114 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2115 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2116 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2117 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 2118 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 2119 | } else { |
| 2120 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 2121 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2122 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2125 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 2126 | MediaStreamInterface* stream) { |
| 2127 | RTC_DCHECK(!IsClosed()); |
| 2128 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2129 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2130 | // We already have a sender for this track, so just change the stream_id |
| 2131 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2132 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2133 | return; |
| 2134 | } |
| 2135 | |
| 2136 | // Normal case; we've never seen this track before. |
| 2137 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender = |
| 2138 | RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 2139 | signaling_thread(), |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2140 | new AudioRtpSender(track, {stream->label()}, voice_channel(), |
| 2141 | stats_.get())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2142 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2143 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 2144 | // which will connect the sender to the underlying transport. This can |
| 2145 | // occur if a local session description that contains the ID of the sender |
| 2146 | // is set before AddStream is called. It can also occur if the local |
| 2147 | // session description is not changed and RemoveStream is called, and |
| 2148 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2149 | const RtpSenderInfo* sender_info = |
| 2150 | FindSenderInfo(local_audio_sender_infos_, stream->label(), track->id()); |
| 2151 | if (sender_info) { |
| 2152 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 2157 | // indefinitely, when we have unified plan SDP. |
| 2158 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 2159 | MediaStreamInterface* stream) { |
| 2160 | RTC_DCHECK(!IsClosed()); |
| 2161 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2162 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2163 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 2164 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2165 | return; |
| 2166 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2167 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 2171 | MediaStreamInterface* stream) { |
| 2172 | RTC_DCHECK(!IsClosed()); |
| 2173 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2174 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2175 | // We already have a sender for this track, so just change the stream_id |
| 2176 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2177 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
| 2181 | // Normal case; we've never seen this track before. |
| 2182 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender = |
| 2183 | RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2184 | signaling_thread(), |
| 2185 | new VideoRtpSender(track, {stream->label()}, video_channel())); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2186 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 2187 | const RtpSenderInfo* sender_info = |
| 2188 | FindSenderInfo(local_video_sender_infos_, stream->label(), track->id()); |
| 2189 | if (sender_info) { |
| 2190 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 2195 | MediaStreamInterface* stream) { |
| 2196 | RTC_DCHECK(!IsClosed()); |
| 2197 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2198 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2199 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 2200 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2201 | return; |
| 2202 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2203 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 2206 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 2207 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 2208 | if (ice_connection_state_ == new_state) { |
| 2209 | return; |
| 2210 | } |
| 2211 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2212 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 2213 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2214 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2215 | return; |
| 2216 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 2217 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2218 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 2219 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 2220 | RTC_DCHECK(ice_connection_state_ != |
| 2221 | PeerConnectionInterface::kIceConnectionClosed); |
| 2222 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2223 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 2224 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | void PeerConnection::OnIceGatheringChange( |
| 2228 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 2229 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2230 | if (IsClosed()) { |
| 2231 | return; |
| 2232 | } |
| 2233 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 2234 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 2237 | void PeerConnection::OnIceCandidate( |
| 2238 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 2239 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2240 | if (IsClosed()) { |
| 2241 | return; |
| 2242 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 2243 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2246 | void PeerConnection::OnIceCandidatesRemoved( |
| 2247 | const std::vector<cricket::Candidate>& candidates) { |
| 2248 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2249 | if (IsClosed()) { |
| 2250 | return; |
| 2251 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2252 | observer_->OnIceCandidatesRemoved(candidates); |
| 2253 | } |
| 2254 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2255 | void PeerConnection::ChangeSignalingState( |
| 2256 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 2257 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 2258 | if (signaling_state_ == signaling_state) { |
| 2259 | return; |
| 2260 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2261 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 2262 | << GetSignalingStateString(signaling_state_) |
| 2263 | << " New state: " |
| 2264 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2265 | signaling_state_ = signaling_state; |
| 2266 | if (signaling_state == kClosed) { |
| 2267 | ice_connection_state_ = kIceConnectionClosed; |
| 2268 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 2269 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 2270 | ice_gathering_state_ = kIceGatheringComplete; |
| 2271 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 2272 | } |
| 2273 | } |
| 2274 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 2277 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 2278 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2279 | if (IsClosed()) { |
| 2280 | return; |
| 2281 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2282 | AddAudioTrack(track, stream); |
| 2283 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 2286 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 2287 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2288 | if (IsClosed()) { |
| 2289 | return; |
| 2290 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2291 | RemoveAudioTrack(track, stream); |
| 2292 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 2296 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2297 | if (IsClosed()) { |
| 2298 | return; |
| 2299 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2300 | AddVideoTrack(track, stream); |
| 2301 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 2305 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2306 | if (IsClosed()) { |
| 2307 | return; |
| 2308 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2309 | RemoveVideoTrack(track, stream); |
| 2310 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2313 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 2314 | SetSessionDescriptionObserver* observer, |
| 2315 | const std::string& error) { |
| 2316 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 2317 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2318 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 2319 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 2323 | CreateSessionDescriptionObserver* observer, |
| 2324 | const std::string& error) { |
| 2325 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 2326 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2327 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 2328 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2331 | void PeerConnection::GetOptionsForOffer( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2332 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 2333 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2334 | ExtractSharedMediaSessionOptions(rtc_options, session_options); |
| 2335 | |
| 2336 | // Figure out transceiver directional preferences. |
| 2337 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 2338 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 2339 | |
| 2340 | // By default, generate sendrecv/recvonly m= sections. |
| 2341 | bool recv_audio = true; |
| 2342 | bool recv_video = true; |
| 2343 | |
| 2344 | // By default, only offer a new m= section if we have media to send with it. |
| 2345 | bool offer_new_audio_description = send_audio; |
| 2346 | bool offer_new_video_description = send_video; |
| 2347 | bool offer_new_data_description = HasDataChannels(); |
| 2348 | |
| 2349 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
| 2350 | if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2351 | recv_audio = (rtc_options.offer_to_receive_audio > 0); |
| 2352 | offer_new_audio_description = |
| 2353 | offer_new_audio_description || (rtc_options.offer_to_receive_audio > 0); |
| 2354 | } |
| 2355 | if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2356 | recv_video = (rtc_options.offer_to_receive_video > 0); |
| 2357 | offer_new_video_description = |
| 2358 | offer_new_video_description || (rtc_options.offer_to_receive_video > 0); |
| 2359 | } |
| 2360 | |
| 2361 | rtc::Optional<size_t> audio_index; |
| 2362 | rtc::Optional<size_t> video_index; |
| 2363 | rtc::Optional<size_t> data_index; |
| 2364 | // If a current description exists, generate m= sections in the same order, |
| 2365 | // using the first audio/video/data section that appears and rejecting |
| 2366 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2367 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2368 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2369 | local_description(), |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2370 | cricket::RtpTransceiverDirection(send_audio, recv_audio), |
| 2371 | cricket::RtpTransceiverDirection(send_video, recv_video), &audio_index, |
| 2372 | &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2373 | } |
| 2374 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2375 | // Add audio/video/data m= sections to the end if needed. |
| 2376 | if (!audio_index && offer_new_audio_description) { |
| 2377 | session_options->media_description_options.push_back( |
| 2378 | cricket::MediaDescriptionOptions( |
| 2379 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
| 2380 | cricket::RtpTransceiverDirection(send_audio, recv_audio), false)); |
| 2381 | audio_index = rtc::Optional<size_t>( |
| 2382 | session_options->media_description_options.size() - 1); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 2383 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2384 | if (!video_index && offer_new_video_description) { |
| 2385 | session_options->media_description_options.push_back( |
| 2386 | cricket::MediaDescriptionOptions( |
| 2387 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
| 2388 | cricket::RtpTransceiverDirection(send_video, recv_video), false)); |
| 2389 | video_index = rtc::Optional<size_t>( |
| 2390 | session_options->media_description_options.size() - 1); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 2391 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2392 | if (!data_index && offer_new_data_description) { |
| 2393 | session_options->media_description_options.push_back( |
| 2394 | cricket::MediaDescriptionOptions( |
| 2395 | cricket::MEDIA_TYPE_DATA, cricket::CN_DATA, |
| 2396 | cricket::RtpTransceiverDirection(true, true), false)); |
| 2397 | data_index = rtc::Optional<size_t>( |
| 2398 | session_options->media_description_options.size() - 1); |
| 2399 | } |
| 2400 | |
| 2401 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 2402 | !audio_index ? nullptr |
| 2403 | : &session_options->media_description_options[*audio_index]; |
| 2404 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 2405 | !video_index ? nullptr |
| 2406 | : &session_options->media_description_options[*video_index]; |
| 2407 | cricket::MediaDescriptionOptions* data_media_description_options = |
| 2408 | !data_index ? nullptr |
| 2409 | : &session_options->media_description_options[*data_index]; |
| 2410 | |
| 2411 | // Apply ICE restart flag and renomination flag. |
| 2412 | for (auto& options : session_options->media_description_options) { |
| 2413 | options.transport_options.ice_restart = rtc_options.ice_restart; |
| 2414 | options.transport_options.enable_ice_renomination = |
| 2415 | configuration_.enable_ice_renomination; |
| 2416 | } |
| 2417 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2418 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2419 | video_media_description_options); |
| 2420 | AddRtpDataChannelOptions(rtp_data_channels_, data_media_description_options); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 2421 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2422 | // Intentionally unset the data channel type for RTP data channel with the |
| 2423 | // second condition. Otherwise the RTP data channels would be successfully |
| 2424 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 2425 | // when building with chromium. We want to leave RTP data channels broken, so |
| 2426 | // people won't try to use them. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2427 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 2428 | session_options->data_channel_type = data_channel_type(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2429 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 2430 | |
| 2431 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 2432 | session_options->crypto_options = factory_->options().crypto_options; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2435 | void PeerConnection::GetOptionsForAnswer( |
| 2436 | const RTCOfferAnswerOptions& rtc_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2437 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2438 | ExtractSharedMediaSessionOptions(rtc_options, session_options); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2439 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2440 | // Figure out transceiver directional preferences. |
| 2441 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 2442 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 2443 | |
| 2444 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 2445 | // restricted by the direction in the offer. |
| 2446 | bool recv_audio = true; |
| 2447 | bool recv_video = true; |
| 2448 | |
| 2449 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
| 2450 | if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2451 | recv_audio = (rtc_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 2452 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2453 | if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2454 | recv_video = (rtc_options.offer_to_receive_video > 0); |
| 2455 | } |
| 2456 | |
| 2457 | rtc::Optional<size_t> audio_index; |
| 2458 | rtc::Optional<size_t> video_index; |
| 2459 | rtc::Optional<size_t> data_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2460 | if (remote_description()) { |
zhihuang | 141aacb | 2017-08-29 13:23:53 -0700 | [diff] [blame] | 2461 | // The pending remote description should be an offer. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2462 | RTC_DCHECK(remote_description()->type() == |
zhihuang | 141aacb | 2017-08-29 13:23:53 -0700 | [diff] [blame] | 2463 | SessionDescriptionInterface::kOffer); |
| 2464 | // Generate m= sections that match those in the offer. |
| 2465 | // Note that mediasession.cc will handle intersection our preferred |
| 2466 | // direction with the offered direction. |
| 2467 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2468 | remote_description(), |
zhihuang | 141aacb | 2017-08-29 13:23:53 -0700 | [diff] [blame] | 2469 | cricket::RtpTransceiverDirection(send_audio, recv_audio), |
| 2470 | cricket::RtpTransceiverDirection(send_video, recv_video), &audio_index, |
| 2471 | &video_index, &data_index, session_options); |
| 2472 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2473 | |
| 2474 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 2475 | !audio_index ? nullptr |
| 2476 | : &session_options->media_description_options[*audio_index]; |
| 2477 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 2478 | !video_index ? nullptr |
| 2479 | : &session_options->media_description_options[*video_index]; |
| 2480 | cricket::MediaDescriptionOptions* data_media_description_options = |
| 2481 | !data_index ? nullptr |
| 2482 | : &session_options->media_description_options[*data_index]; |
| 2483 | |
| 2484 | // Apply ICE renomination flag. |
| 2485 | for (auto& options : session_options->media_description_options) { |
| 2486 | options.transport_options.enable_ice_renomination = |
| 2487 | configuration_.enable_ice_renomination; |
| 2488 | } |
| 2489 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2490 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2491 | video_media_description_options); |
| 2492 | AddRtpDataChannelOptions(rtp_data_channels_, data_media_description_options); |
| 2493 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2494 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 2495 | // the RTP data channels would be successfully negotiated by default and the |
| 2496 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 2497 | // We want to leave RTP data channels broken, so people won't try to use them. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2498 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 2499 | session_options->data_channel_type = data_channel_type(); |
deadbeef | 907abe4 | 2016-08-04 12:22:18 -0700 | [diff] [blame] | 2500 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 2501 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2502 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 2503 | session_options->crypto_options = factory_->options().crypto_options; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2504 | } |
| 2505 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2506 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 2507 | const SessionDescriptionInterface* session_desc, |
| 2508 | cricket::RtpTransceiverDirection audio_direction, |
| 2509 | cricket::RtpTransceiverDirection video_direction, |
| 2510 | rtc::Optional<size_t>* audio_index, |
| 2511 | rtc::Optional<size_t>* video_index, |
| 2512 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2513 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2514 | for (const cricket::ContentInfo& content : |
| 2515 | session_desc->description()->contents()) { |
| 2516 | if (IsAudioContent(&content)) { |
| 2517 | // If we already have an audio m= section, reject this extra one. |
| 2518 | if (*audio_index) { |
| 2519 | session_options->media_description_options.push_back( |
| 2520 | cricket::MediaDescriptionOptions( |
| 2521 | cricket::MEDIA_TYPE_AUDIO, content.name, |
| 2522 | cricket::RtpTransceiverDirection(false, false), true)); |
| 2523 | } else { |
| 2524 | session_options->media_description_options.push_back( |
| 2525 | cricket::MediaDescriptionOptions( |
| 2526 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
| 2527 | !audio_direction.send && !audio_direction.recv)); |
| 2528 | *audio_index = rtc::Optional<size_t>( |
| 2529 | session_options->media_description_options.size() - 1); |
| 2530 | } |
| 2531 | } else if (IsVideoContent(&content)) { |
| 2532 | // If we already have an video m= section, reject this extra one. |
| 2533 | if (*video_index) { |
| 2534 | session_options->media_description_options.push_back( |
| 2535 | cricket::MediaDescriptionOptions( |
| 2536 | cricket::MEDIA_TYPE_VIDEO, content.name, |
| 2537 | cricket::RtpTransceiverDirection(false, false), true)); |
| 2538 | } else { |
| 2539 | session_options->media_description_options.push_back( |
| 2540 | cricket::MediaDescriptionOptions( |
| 2541 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
| 2542 | !video_direction.send && !video_direction.recv)); |
| 2543 | *video_index = rtc::Optional<size_t>( |
| 2544 | session_options->media_description_options.size() - 1); |
| 2545 | } |
| 2546 | } else { |
| 2547 | RTC_DCHECK(IsDataContent(&content)); |
| 2548 | // If we already have an data m= section, reject this extra one. |
| 2549 | if (*data_index) { |
| 2550 | session_options->media_description_options.push_back( |
| 2551 | cricket::MediaDescriptionOptions( |
| 2552 | cricket::MEDIA_TYPE_DATA, content.name, |
| 2553 | cricket::RtpTransceiverDirection(false, false), true)); |
| 2554 | } else { |
| 2555 | session_options->media_description_options.push_back( |
| 2556 | cricket::MediaDescriptionOptions( |
| 2557 | cricket::MEDIA_TYPE_DATA, content.name, |
| 2558 | // Direction for data sections is meaningless, but legacy |
| 2559 | // endpoints might expect sendrecv. |
| 2560 | cricket::RtpTransceiverDirection(true, true), false)); |
| 2561 | *data_index = rtc::Optional<size_t>( |
| 2562 | session_options->media_description_options.size() - 1); |
| 2563 | } |
| 2564 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2565 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2568 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 2569 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 2570 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2571 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2572 | } |
| 2573 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2574 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2575 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2576 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2577 | cricket::MediaType media_type, |
| 2578 | StreamCollection* new_streams) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2579 | std::vector<RtpSenderInfo>* current_senders = |
| 2580 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2581 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2582 | // 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] | 2583 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2584 | for (auto sender_it = current_senders->begin(); |
| 2585 | sender_it != current_senders->end(); |
| 2586 | /* incremented manually */) { |
| 2587 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2588 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2589 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 2590 | bool sender_exists = params && params->id == info.sender_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2591 | // 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] | 2592 | if ((info.stream_label == kDefaultStreamLabel && default_sender_needed) || |
| 2593 | sender_exists) { |
| 2594 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2595 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2596 | OnRemoteSenderRemoved(info, media_type); |
| 2597 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2601 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2602 | for (const cricket::StreamParams& params : streams) { |
| 2603 | // 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] | 2604 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2605 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2606 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2607 | uint32_t ssrc = params.first_ssrc(); |
| 2608 | |
| 2609 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2610 | remote_streams_->find(stream_label); |
| 2611 | if (!stream) { |
| 2612 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2613 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2614 | MediaStream::Create(stream_label)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2615 | remote_streams_->AddStream(stream); |
| 2616 | new_streams->AddStream(stream); |
| 2617 | } |
| 2618 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2619 | const RtpSenderInfo* sender_info = |
| 2620 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 2621 | if (!sender_info) { |
| 2622 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 2623 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2624 | } |
| 2625 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2626 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2627 | // Add default sender if necessary. |
| 2628 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2629 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
| 2630 | remote_streams_->find(kDefaultStreamLabel); |
| 2631 | if (!default_stream) { |
| 2632 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2633 | default_stream = MediaStreamProxy::Create( |
| 2634 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamLabel)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2635 | remote_streams_->AddStream(default_stream); |
| 2636 | new_streams->AddStream(default_stream); |
| 2637 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2638 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 2639 | ? kDefaultAudioSenderId |
| 2640 | : kDefaultVideoSenderId; |
| 2641 | const RtpSenderInfo* default_sender_info = FindSenderInfo( |
| 2642 | *current_senders, kDefaultStreamLabel, default_sender_id); |
| 2643 | if (!default_sender_info) { |
| 2644 | current_senders->push_back( |
| 2645 | RtpSenderInfo(kDefaultStreamLabel, default_sender_id, 0)); |
| 2646 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2647 | } |
| 2648 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2649 | } |
| 2650 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2651 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 2652 | cricket::MediaType media_type) { |
| 2653 | MediaStreamInterface* stream = |
| 2654 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2655 | |
| 2656 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2657 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2658 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2659 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2660 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 2661 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2662 | } |
| 2663 | } |
| 2664 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2665 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 2666 | cricket::MediaType media_type) { |
| 2667 | MediaStreamInterface* stream = |
| 2668 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2669 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2670 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2671 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2672 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 2673 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2674 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2675 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2676 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2677 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2678 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2679 | } |
| 2680 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2681 | // Stopping or destroying a VideoRtpReceiver will end the |
| 2682 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2683 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2684 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2685 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2686 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2687 | // There's no guarantee the track is still available, e.g. the track may |
| 2688 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2689 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2690 | } |
| 2691 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 2692 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2693 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2694 | if (receiver) { |
| 2695 | observer_->OnRemoveTrack(receiver); |
| 2696 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 2700 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 2701 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 2702 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 2703 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 2704 | streams_to_remove.push_back(stream); |
| 2705 | } |
| 2706 | } |
| 2707 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 2708 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2709 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 2710 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2711 | } |
| 2712 | } |
| 2713 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2714 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2715 | const std::vector<cricket::StreamParams>& streams, |
| 2716 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2717 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2718 | |
| 2719 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 2720 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2721 | for (auto sender_it = current_senders->begin(); |
| 2722 | sender_it != current_senders->end(); |
| 2723 | /* incremented manually */) { |
| 2724 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2725 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2726 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 2727 | if (!params || params->id != info.sender_id || |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2728 | params->sync_label != info.stream_label) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2729 | OnLocalSenderRemoved(info, media_type); |
| 2730 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2731 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2732 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2733 | } |
| 2734 | } |
| 2735 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2736 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2737 | for (const cricket::StreamParams& params : streams) { |
| 2738 | // 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] | 2739 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2740 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2741 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2742 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2743 | const RtpSenderInfo* sender_info = |
| 2744 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 2745 | if (!sender_info) { |
| 2746 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 2747 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2752 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 2753 | cricket::MediaType media_type) { |
| 2754 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2755 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2756 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 2757 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2758 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2759 | return; |
| 2760 | } |
| 2761 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2762 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2763 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 2764 | << " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2765 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2766 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2767 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2768 | sender->internal()->set_stream_id(sender_info.stream_label); |
| 2769 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2772 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 2773 | cricket::MediaType media_type) { |
| 2774 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2775 | if (!sender) { |
| 2776 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2777 | // SessionDescriptions has been renegotiated. |
| 2778 | return; |
| 2779 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2780 | |
| 2781 | // A sender has been removed from the SessionDescription but it's still |
| 2782 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 2783 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 2784 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2785 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 2786 | << " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2787 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2788 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2789 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2790 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 2794 | const cricket::StreamParamsVec& streams) { |
| 2795 | std::vector<std::string> existing_channels; |
| 2796 | |
| 2797 | // Find new and active data channels. |
| 2798 | for (const cricket::StreamParams& params : streams) { |
| 2799 | // |it->sync_label| is actually the data channel label. The reason is that |
| 2800 | // we use the same naming of data channels as we do for |
| 2801 | // MediaStreams and Tracks. |
| 2802 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 2803 | // track label is the same as |streamid|. |
| 2804 | const std::string& channel_label = params.sync_label; |
| 2805 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2806 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2807 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2808 | continue; |
| 2809 | } |
| 2810 | // Set the SSRC the data channel should use for sending. |
| 2811 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 2812 | existing_channels.push_back(data_channel_it->first); |
| 2813 | } |
| 2814 | |
| 2815 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 2816 | } |
| 2817 | |
| 2818 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 2819 | const cricket::StreamParamsVec& streams) { |
| 2820 | std::vector<std::string> existing_channels; |
| 2821 | |
| 2822 | // Find new and active data channels. |
| 2823 | for (const cricket::StreamParams& params : streams) { |
| 2824 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 2825 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 2826 | std::string label = params.sync_label.empty() |
| 2827 | ? rtc::ToString(params.first_ssrc()) |
| 2828 | : params.sync_label; |
| 2829 | auto data_channel_it = rtp_data_channels_.find(label); |
| 2830 | if (data_channel_it == rtp_data_channels_.end()) { |
| 2831 | // This is a new data channel. |
| 2832 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 2833 | } else { |
| 2834 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 2835 | } |
| 2836 | existing_channels.push_back(label); |
| 2837 | } |
| 2838 | |
| 2839 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 2840 | } |
| 2841 | |
| 2842 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 2843 | const std::vector<std::string>& active_channels, |
| 2844 | bool is_local_update) { |
| 2845 | auto it = rtp_data_channels_.begin(); |
| 2846 | while (it != rtp_data_channels_.end()) { |
| 2847 | DataChannel* data_channel = it->second; |
| 2848 | if (std::find(active_channels.begin(), active_channels.end(), |
| 2849 | data_channel->label()) != active_channels.end()) { |
| 2850 | ++it; |
| 2851 | continue; |
| 2852 | } |
| 2853 | |
| 2854 | if (is_local_update) { |
| 2855 | data_channel->SetSendSsrc(0); |
| 2856 | } else { |
| 2857 | data_channel->RemotePeerRequestClose(); |
| 2858 | } |
| 2859 | |
| 2860 | if (data_channel->state() == DataChannel::kClosed) { |
| 2861 | rtp_data_channels_.erase(it); |
| 2862 | it = rtp_data_channels_.begin(); |
| 2863 | } else { |
| 2864 | ++it; |
| 2865 | } |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 2870 | uint32_t remote_ssrc) { |
| 2871 | rtc::scoped_refptr<DataChannel> channel( |
| 2872 | InternalCreateDataChannel(label, nullptr)); |
| 2873 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2874 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 2875 | << "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2876 | return; |
| 2877 | } |
| 2878 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2879 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 2880 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 2881 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 2885 | const std::string& label, |
| 2886 | const InternalDataChannelInit* config) { |
| 2887 | if (IsClosed()) { |
| 2888 | return nullptr; |
| 2889 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2890 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2891 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2892 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 2893 | return nullptr; |
| 2894 | } |
| 2895 | InternalDataChannelInit new_config = |
| 2896 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2897 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2898 | if (new_config.id < 0) { |
| 2899 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2900 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2901 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2902 | RTC_LOG(LS_ERROR) |
| 2903 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2904 | return nullptr; |
| 2905 | } |
| 2906 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2907 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 2908 | << "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2909 | return nullptr; |
| 2910 | } |
| 2911 | } |
| 2912 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2913 | rtc::scoped_refptr<DataChannel> channel( |
| 2914 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2915 | if (!channel) { |
| 2916 | sid_allocator_.ReleaseSid(new_config.id); |
| 2917 | return nullptr; |
| 2918 | } |
| 2919 | |
| 2920 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 2921 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2922 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 2923 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2924 | return nullptr; |
| 2925 | } |
| 2926 | rtp_data_channels_[channel->label()] = channel; |
| 2927 | } else { |
| 2928 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 2929 | sctp_data_channels_.push_back(channel); |
| 2930 | channel->SignalClosed.connect(this, |
| 2931 | &PeerConnection::OnSctpDataChannelClosed); |
| 2932 | } |
| 2933 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 2934 | SignalDataChannelCreated(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2935 | return channel; |
| 2936 | } |
| 2937 | |
| 2938 | bool PeerConnection::HasDataChannels() const { |
| 2939 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 2940 | } |
| 2941 | |
| 2942 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 2943 | for (const auto& channel : sctp_data_channels_) { |
| 2944 | if (channel->id() < 0) { |
| 2945 | int sid; |
| 2946 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2947 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2948 | continue; |
| 2949 | } |
| 2950 | channel->SetSctpSid(sid); |
| 2951 | } |
| 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 2956 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2957 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 2958 | ++it) { |
| 2959 | if (it->get() == channel) { |
| 2960 | if (channel->id() >= 0) { |
| 2961 | sid_allocator_.ReleaseSid(channel->id()); |
| 2962 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 2963 | // Since this method is triggered by a signal from the DataChannel, |
| 2964 | // we can't free it directly here; we need to free it asynchronously. |
| 2965 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2966 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2967 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 2968 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2969 | return; |
| 2970 | } |
| 2971 | } |
| 2972 | } |
| 2973 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2974 | void PeerConnection::OnDataChannelDestroyed() { |
| 2975 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 2976 | // DataChannel may callback to us and try to modify the list. |
| 2977 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 2978 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 2979 | for (const auto& kv : temp_rtp_dcs) { |
| 2980 | kv.second->OnTransportChannelDestroyed(); |
| 2981 | } |
| 2982 | |
| 2983 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 2984 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 2985 | for (const auto& channel : temp_sctp_dcs) { |
| 2986 | channel->OnTransportChannelDestroyed(); |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | void PeerConnection::OnDataChannelOpenMessage( |
| 2991 | const std::string& label, |
| 2992 | const InternalDataChannelInit& config) { |
| 2993 | rtc::scoped_refptr<DataChannel> channel( |
| 2994 | InternalCreateDataChannel(label, &config)); |
| 2995 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2996 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2997 | return; |
| 2998 | } |
| 2999 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 3000 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 3001 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3002 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3003 | } |
| 3004 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3005 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3006 | PeerConnection::GetAudioTransceiver() const { |
| 3007 | // This method only works with Plan B SDP, where there is a single |
| 3008 | // audio/video transceiver. |
| 3009 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3010 | for (auto transceiver : transceivers_) { |
| 3011 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3012 | return transceiver; |
| 3013 | } |
| 3014 | } |
| 3015 | RTC_NOTREACHED(); |
| 3016 | return nullptr; |
| 3017 | } |
| 3018 | |
| 3019 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3020 | PeerConnection::GetVideoTransceiver() const { |
| 3021 | // This method only works with Plan B SDP, where there is a single |
| 3022 | // audio/video transceiver. |
| 3023 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3024 | for (auto transceiver : transceivers_) { |
| 3025 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 3026 | return transceiver; |
| 3027 | } |
| 3028 | } |
| 3029 | RTC_NOTREACHED(); |
| 3030 | return nullptr; |
| 3031 | } |
| 3032 | |
| 3033 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 3034 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3035 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3036 | switch (type) { |
| 3037 | case cricket::MEDIA_TYPE_AUDIO: |
| 3038 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 3039 | case cricket::MEDIA_TYPE_VIDEO: |
| 3040 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 3041 | case cricket::MEDIA_TYPE_DATA: |
| 3042 | return false; |
| 3043 | } |
| 3044 | RTC_NOTREACHED(); |
| 3045 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3048 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 3049 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 3050 | for (auto transceiver : transceivers_) { |
| 3051 | for (auto sender : transceiver->internal()->senders()) { |
| 3052 | if (sender->track() == track) { |
| 3053 | return sender; |
| 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3058 | } |
| 3059 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3060 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 3061 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 3062 | for (auto transceiver : transceivers_) { |
| 3063 | for (auto sender : transceiver->internal()->senders()) { |
| 3064 | if (sender->id() == sender_id) { |
| 3065 | return sender; |
| 3066 | } |
| 3067 | } |
| 3068 | } |
| 3069 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3072 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 3073 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 3074 | for (auto transceiver : transceivers_) { |
| 3075 | for (auto receiver : transceiver->internal()->receivers()) { |
| 3076 | if (receiver->id() == receiver_id) { |
| 3077 | return receiver; |
| 3078 | } |
| 3079 | } |
| 3080 | } |
| 3081 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3084 | std::vector<PeerConnection::RtpSenderInfo>* |
| 3085 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 3086 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3087 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 3088 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 3089 | ? &remote_audio_sender_infos_ |
| 3090 | : &remote_video_sender_infos_; |
| 3091 | } |
| 3092 | |
| 3093 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3094 | cricket::MediaType media_type) { |
| 3095 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3096 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3097 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 3098 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3099 | } |
| 3100 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3101 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 3102 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3103 | const std::string& stream_label, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3104 | const std::string sender_id) const { |
| 3105 | for (const RtpSenderInfo& sender_info : infos) { |
| 3106 | if (sender_info.stream_label == stream_label && |
| 3107 | sender_info.sender_id == sender_id) { |
| 3108 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3109 | } |
| 3110 | } |
| 3111 | return nullptr; |
| 3112 | } |
| 3113 | |
| 3114 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 3115 | for (const auto& channel : sctp_data_channels_) { |
| 3116 | if (channel->id() == sid) { |
| 3117 | return channel; |
| 3118 | } |
| 3119 | } |
| 3120 | return nullptr; |
| 3121 | } |
| 3122 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 3123 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3124 | const RTCConfiguration& configuration) { |
| 3125 | cricket::ServerAddresses stun_servers; |
| 3126 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3127 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 3128 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3129 | return false; |
| 3130 | } |
| 3131 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 3132 | port_allocator_->Initialize(); |
| 3133 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3134 | // To handle both internal and externally created port allocator, we will |
| 3135 | // enable BUNDLE here. |
| 3136 | int portallocator_flags = port_allocator_->flags(); |
| 3137 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 3138 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 3139 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3140 | // If the disable-IPv6 flag was specified, we'll not override it |
| 3141 | // by experiment. |
| 3142 | if (configuration.disable_ipv6) { |
| 3143 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 3144 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 3145 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3146 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 3147 | } |
| 3148 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 3149 | if (configuration.disable_ipv6_on_wifi) { |
| 3150 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3151 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 3152 | } |
| 3153 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3154 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 3155 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3156 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3157 | } |
| 3158 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 3159 | if (configuration.candidate_network_policy == |
| 3160 | kCandidateNetworkPolicyLowCost) { |
| 3161 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3162 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 3163 | } |
| 3164 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3165 | port_allocator_->set_flags(portallocator_flags); |
| 3166 | // No step delay is used while allocating ports. |
| 3167 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 3168 | port_allocator_->set_candidate_filter( |
| 3169 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 3170 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3171 | |
| 3172 | // Call this last since it may create pooled allocator sessions using the |
| 3173 | // properties set above. |
| 3174 | port_allocator_->SetConfiguration(stun_servers, turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 3175 | configuration.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3176 | configuration.prune_turn_ports, |
| 3177 | configuration.turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3178 | return true; |
| 3179 | } |
| 3180 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 3181 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3182 | const cricket::ServerAddresses& stun_servers, |
| 3183 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 3184 | IceTransportsType type, |
| 3185 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3186 | bool prune_turn_ports, |
| 3187 | webrtc::TurnCustomizer* turn_customizer) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3188 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3189 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3190 | // Call this last since it may create pooled allocator sessions using the |
| 3191 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 3192 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3193 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
| 3194 | turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3195 | } |
| 3196 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3197 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 3198 | return factory_->channel_manager(); |
| 3199 | } |
| 3200 | |
| 3201 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 3202 | return uma_observer_; |
| 3203 | } |
| 3204 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3205 | bool PeerConnection::StartRtcEventLog_w( |
Zhi Huang | 33c5c7f | 2017-11-17 20:59:29 +0000 | [diff] [blame] | 3206 | std::unique_ptr<RtcEventLogOutput> output) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 3207 | if (!event_log_) { |
| 3208 | return false; |
| 3209 | } |
Zhi Huang | 33c5c7f | 2017-11-17 20:59:29 +0000 | [diff] [blame] | 3210 | return event_log_->StartLogging(std::move(output)); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 3214 | if (event_log_) { |
| 3215 | event_log_->StopLogging(); |
| 3216 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3217 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3218 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3219 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 3220 | const std::string& content_name) { |
| 3221 | if (voice_channel() && voice_channel()->content_name() == content_name) { |
| 3222 | return voice_channel(); |
| 3223 | } |
| 3224 | if (video_channel() && video_channel()->content_name() == content_name) { |
| 3225 | return video_channel(); |
| 3226 | } |
| 3227 | if (rtp_data_channel() && |
| 3228 | rtp_data_channel()->content_name() == content_name) { |
| 3229 | return rtp_data_channel(); |
| 3230 | } |
| 3231 | return nullptr; |
| 3232 | } |
| 3233 | |
| 3234 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 3235 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3236 | RTC_LOG(LS_INFO) |
| 3237 | << "Local and Remote descriptions must be applied to get the " |
| 3238 | << "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3239 | return false; |
| 3240 | } |
| 3241 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3242 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
| 3243 | << "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3244 | return false; |
| 3245 | } |
| 3246 | |
| 3247 | return transport_controller_->GetSslRole(*sctp_transport_name_, role); |
| 3248 | } |
| 3249 | |
| 3250 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 3251 | rtc::SSLRole* role) { |
| 3252 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3253 | RTC_LOG(LS_INFO) |
| 3254 | << "Local and Remote descriptions must be applied to get the " |
| 3255 | << "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3256 | return false; |
| 3257 | } |
| 3258 | |
| 3259 | return transport_controller_->GetSslRole(GetTransportName(content_name), |
| 3260 | role); |
| 3261 | } |
| 3262 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3263 | bool PeerConnection::SetLocalDescription( |
| 3264 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 3265 | std::string* err_desc) { |
| 3266 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3267 | |
| 3268 | // Validate SDP. |
| 3269 | if (!ValidateSessionDescription(desc.get(), cricket::CS_LOCAL, err_desc)) { |
| 3270 | return false; |
| 3271 | } |
| 3272 | |
| 3273 | // Update the initial_offerer flag if this session is the initial_offerer. |
| 3274 | Action action = GetAction(desc->type()); |
| 3275 | if (!initial_offerer_.has_value()) { |
| 3276 | initial_offerer_.emplace(action == kOffer); |
| 3277 | if (*initial_offerer_) { |
| 3278 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3279 | } else { |
| 3280 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | if (action == kAnswer) { |
| 3285 | current_local_description_ = std::move(desc); |
| 3286 | pending_local_description_ = nullptr; |
| 3287 | current_remote_description_ = std::move(pending_remote_description_); |
| 3288 | } else { |
| 3289 | pending_local_description_ = std::move(desc); |
| 3290 | } |
| 3291 | |
| 3292 | // Transport and Media channels will be created only when offer is set. |
| 3293 | if (action == kOffer && !CreateChannels(local_description()->description())) { |
| 3294 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 3295 | // is applied. Restore back to old description. |
| 3296 | return BadLocalSdp(local_description()->type(), kCreateChannelFailed, |
| 3297 | err_desc); |
| 3298 | } |
| 3299 | |
| 3300 | // Remove unused channels if MediaContentDescription is rejected. |
| 3301 | RemoveUnusedChannels(local_description()->description()); |
| 3302 | |
| 3303 | if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) { |
| 3304 | return false; |
| 3305 | } |
| 3306 | if (remote_description()) { |
| 3307 | // Now that we have a local description, we can push down remote candidates. |
| 3308 | UseCandidatesInSessionDescription(remote_description()); |
| 3309 | } |
| 3310 | |
| 3311 | pending_ice_restarts_.clear(); |
| 3312 | if (error() != ERROR_NONE) { |
| 3313 | return BadLocalSdp(local_description()->type(), GetSessionErrorMsg(), |
| 3314 | err_desc); |
| 3315 | } |
| 3316 | return true; |
| 3317 | } |
| 3318 | |
| 3319 | bool PeerConnection::SetRemoteDescription( |
| 3320 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 3321 | std::string* err_desc) { |
| 3322 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3323 | |
| 3324 | // Validate SDP. |
| 3325 | if (!ValidateSessionDescription(desc.get(), cricket::CS_REMOTE, err_desc)) { |
| 3326 | return false; |
| 3327 | } |
| 3328 | |
| 3329 | // Hold this pointer so candidates can be copied to it later in the method. |
| 3330 | SessionDescriptionInterface* desc_ptr = desc.get(); |
| 3331 | |
| 3332 | const SessionDescriptionInterface* old_remote_description = |
| 3333 | remote_description(); |
| 3334 | // Grab ownership of the description being replaced for the remainder of this |
| 3335 | // method, since it's used below as |old_remote_description|. |
| 3336 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
| 3337 | Action action = GetAction(desc->type()); |
| 3338 | if (action == kAnswer) { |
| 3339 | replaced_remote_description = pending_remote_description_ |
| 3340 | ? std::move(pending_remote_description_) |
| 3341 | : std::move(current_remote_description_); |
| 3342 | current_remote_description_ = std::move(desc); |
| 3343 | pending_remote_description_ = nullptr; |
| 3344 | current_local_description_ = std::move(pending_local_description_); |
| 3345 | } else { |
| 3346 | replaced_remote_description = std::move(pending_remote_description_); |
| 3347 | pending_remote_description_ = std::move(desc); |
| 3348 | } |
| 3349 | |
| 3350 | // Transport and Media channels will be created only when offer is set. |
| 3351 | if (action == kOffer && |
| 3352 | !CreateChannels(remote_description()->description())) { |
| 3353 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 3354 | // is applied. Restore back to old description. |
| 3355 | return BadRemoteSdp(remote_description()->type(), kCreateChannelFailed, |
| 3356 | err_desc); |
| 3357 | } |
| 3358 | |
| 3359 | // Remove unused channels if MediaContentDescription is rejected. |
| 3360 | RemoveUnusedChannels(remote_description()->description()); |
| 3361 | |
| 3362 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 3363 | // is called. |
| 3364 | if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) { |
| 3365 | return false; |
| 3366 | } |
| 3367 | |
| 3368 | if (local_description() && |
| 3369 | !UseCandidatesInSessionDescription(remote_description())) { |
| 3370 | return BadRemoteSdp(remote_description()->type(), kInvalidCandidates, |
| 3371 | err_desc); |
| 3372 | } |
| 3373 | |
| 3374 | if (old_remote_description) { |
| 3375 | for (const cricket::ContentInfo& content : |
| 3376 | old_remote_description->description()->contents()) { |
| 3377 | // Check if this new SessionDescription contains new ICE ufrag and |
| 3378 | // password that indicates the remote peer requests an ICE restart. |
| 3379 | // TODO(deadbeef): When we start storing both the current and pending |
| 3380 | // remote description, this should reset pending_ice_restarts and compare |
| 3381 | // against the current description. |
| 3382 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 3383 | content.name)) { |
| 3384 | if (action == kOffer) { |
| 3385 | pending_ice_restarts_.insert(content.name); |
| 3386 | } |
| 3387 | } else { |
| 3388 | // We retain all received candidates only if ICE is not restarted. |
| 3389 | // When ICE is restarted, all previous candidates belong to an old |
| 3390 | // generation and should not be kept. |
| 3391 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 3392 | // description should only contain candidates from the last set remote |
| 3393 | // description plus any candidates added since then. We should remove |
| 3394 | // this once we're sure it won't break anything. |
| 3395 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 3396 | old_remote_description, content.name, desc_ptr); |
| 3397 | } |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | if (error() != ERROR_NONE) { |
| 3402 | return BadRemoteSdp(remote_description()->type(), GetSessionErrorMsg(), |
| 3403 | err_desc); |
| 3404 | } |
| 3405 | |
| 3406 | // Set the the ICE connection state to connecting since the connection may |
| 3407 | // become writable with peer reflexive candidates before any remote candidate |
| 3408 | // is signaled. |
| 3409 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 3410 | // is to have a new signal the indicates a change in checking state from the |
| 3411 | // transport and expose a new checking() member from transport that can be |
| 3412 | // read to determine the current checking state. The existing SignalConnecting |
| 3413 | // actually means "gathering candidates", so cannot be be used here. |
| 3414 | if (remote_description()->type() != SessionDescriptionInterface::kOffer && |
| 3415 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 3416 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 3417 | } |
| 3418 | return true; |
| 3419 | } |
| 3420 | |
| 3421 | // TODO(steveanton): Eventually it'd be nice to store the channels as a single |
| 3422 | // vector of BaseChannel pointers instead of separate voice and video channel |
| 3423 | // vectors. At that point, this will become a simple getter. |
| 3424 | std::vector<cricket::BaseChannel*> PeerConnection::Channels() const { |
| 3425 | std::vector<cricket::BaseChannel*> channels; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3426 | if (voice_channel()) { |
| 3427 | channels.push_back(voice_channel()); |
| 3428 | } |
| 3429 | if (video_channel()) { |
| 3430 | channels.push_back(video_channel()); |
| 3431 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3432 | if (rtp_data_channel_) { |
| 3433 | channels.push_back(rtp_data_channel_); |
| 3434 | } |
| 3435 | return channels; |
| 3436 | } |
| 3437 | |
| 3438 | void PeerConnection::SetError(Error error, const std::string& error_desc) { |
| 3439 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3440 | if (error != error_) { |
| 3441 | error_ = error; |
| 3442 | error_desc_ = error_desc; |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | bool PeerConnection::UpdateSessionState(Action action, |
| 3447 | cricket::ContentSource source, |
| 3448 | std::string* err_desc) { |
| 3449 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3450 | |
| 3451 | // If there's already a pending error then no state transition should happen. |
| 3452 | // But all call-sites should be verifying this before calling us! |
| 3453 | RTC_DCHECK(error() == ERROR_NONE); |
| 3454 | std::string td_err; |
| 3455 | if (action == kOffer) { |
| 3456 | if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) { |
| 3457 | return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc); |
| 3458 | } |
| 3459 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 3460 | ? PeerConnectionInterface::kHaveLocalOffer |
| 3461 | : PeerConnectionInterface::kHaveRemoteOffer); |
| 3462 | if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) { |
| 3463 | SetError(ERROR_CONTENT, *err_desc); |
| 3464 | } |
| 3465 | if (error() != ERROR_NONE) { |
| 3466 | return BadOfferSdp(source, GetSessionErrorMsg(), err_desc); |
| 3467 | } |
| 3468 | } else if (action == kPrAnswer) { |
| 3469 | if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) { |
| 3470 | return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc); |
| 3471 | } |
| 3472 | EnableChannels(); |
| 3473 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 3474 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 3475 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 3476 | if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) { |
| 3477 | SetError(ERROR_CONTENT, *err_desc); |
| 3478 | } |
| 3479 | if (error() != ERROR_NONE) { |
| 3480 | return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc); |
| 3481 | } |
| 3482 | } else if (action == kAnswer) { |
| 3483 | const cricket::ContentGroup* local_bundle = |
| 3484 | local_description()->description()->GetGroupByName( |
| 3485 | cricket::GROUP_TYPE_BUNDLE); |
| 3486 | const cricket::ContentGroup* remote_bundle = |
| 3487 | remote_description()->description()->GetGroupByName( |
| 3488 | cricket::GROUP_TYPE_BUNDLE); |
| 3489 | if (local_bundle && remote_bundle) { |
| 3490 | // The answerer decides the transport to bundle on. |
| 3491 | const cricket::ContentGroup* answer_bundle = |
| 3492 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 3493 | if (!EnableBundle(*answer_bundle)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3494 | RTC_LOG(LS_WARNING) << "Failed to enable BUNDLE."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3495 | return BadAnswerSdp(source, kEnableBundleFailed, err_desc); |
| 3496 | } |
| 3497 | } |
| 3498 | // Only push down the transport description after enabling BUNDLE; we don't |
| 3499 | // want to push down a description on a transport about to be destroyed. |
| 3500 | if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) { |
| 3501 | return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc); |
| 3502 | } |
| 3503 | EnableChannels(); |
| 3504 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 3505 | if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) { |
| 3506 | SetError(ERROR_CONTENT, *err_desc); |
| 3507 | } |
| 3508 | if (error() != ERROR_NONE) { |
| 3509 | return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc); |
| 3510 | } |
| 3511 | } |
| 3512 | return true; |
| 3513 | } |
| 3514 | |
| 3515 | PeerConnection::Action PeerConnection::GetAction(const std::string& type) { |
| 3516 | if (type == SessionDescriptionInterface::kOffer) { |
| 3517 | return PeerConnection::kOffer; |
| 3518 | } else if (type == SessionDescriptionInterface::kPrAnswer) { |
| 3519 | return PeerConnection::kPrAnswer; |
| 3520 | } else if (type == SessionDescriptionInterface::kAnswer) { |
| 3521 | return PeerConnection::kAnswer; |
| 3522 | } |
| 3523 | RTC_NOTREACHED() << "unknown action type"; |
| 3524 | return PeerConnection::kOffer; |
| 3525 | } |
| 3526 | |
| 3527 | bool PeerConnection::PushdownMediaDescription(cricket::ContentAction action, |
| 3528 | cricket::ContentSource source, |
| 3529 | std::string* err) { |
| 3530 | const SessionDescription* sdesc = |
| 3531 | (source == cricket::CS_LOCAL ? local_description() : remote_description()) |
| 3532 | ->description(); |
| 3533 | RTC_DCHECK(sdesc); |
| 3534 | bool all_success = true; |
| 3535 | for (auto* channel : Channels()) { |
| 3536 | // TODO(steveanton): Add support for multiple channels of the same type. |
| 3537 | const ContentInfo* content_info = |
| 3538 | cricket::GetFirstMediaContent(sdesc->contents(), channel->media_type()); |
| 3539 | if (!content_info) { |
| 3540 | continue; |
| 3541 | } |
| 3542 | const MediaContentDescription* content_desc = |
| 3543 | static_cast<const MediaContentDescription*>(content_info->description); |
| 3544 | if (content_desc && !content_info->rejected) { |
| 3545 | bool success = (source == cricket::CS_LOCAL) |
| 3546 | ? channel->SetLocalContent(content_desc, action, err) |
| 3547 | : channel->SetRemoteContent(content_desc, action, err); |
| 3548 | if (!success) { |
| 3549 | all_success = false; |
| 3550 | break; |
| 3551 | } |
| 3552 | } |
| 3553 | } |
| 3554 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 3555 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 3556 | if (sctp_transport_ && local_description() && remote_description() && |
| 3557 | cricket::GetFirstDataContent(local_description()->description()) && |
| 3558 | cricket::GetFirstDataContent(remote_description()->description())) { |
| 3559 | all_success &= network_thread()->Invoke<bool>( |
| 3560 | RTC_FROM_HERE, |
| 3561 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
| 3562 | } |
| 3563 | return all_success; |
| 3564 | } |
| 3565 | |
| 3566 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 3567 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 3568 | RTC_DCHECK(local_description()); |
| 3569 | RTC_DCHECK(remote_description()); |
| 3570 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 3571 | // When we support "max-message-size", that would also be pushed down here. |
| 3572 | return sctp_transport_->Start( |
| 3573 | GetSctpPort(local_description()->description()), |
| 3574 | GetSctpPort(remote_description()->description())); |
| 3575 | } |
| 3576 | |
| 3577 | bool PeerConnection::PushdownTransportDescription(cricket::ContentSource source, |
| 3578 | cricket::ContentAction action, |
| 3579 | std::string* error_desc) { |
| 3580 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3581 | |
| 3582 | if (source == cricket::CS_LOCAL) { |
| 3583 | return PushdownLocalTransportDescription(local_description()->description(), |
| 3584 | action, error_desc); |
| 3585 | } |
| 3586 | return PushdownRemoteTransportDescription(remote_description()->description(), |
| 3587 | action, error_desc); |
| 3588 | } |
| 3589 | |
| 3590 | bool PeerConnection::PushdownLocalTransportDescription( |
| 3591 | const SessionDescription* sdesc, |
| 3592 | cricket::ContentAction action, |
| 3593 | std::string* err) { |
| 3594 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3595 | |
| 3596 | if (!sdesc) { |
| 3597 | return false; |
| 3598 | } |
| 3599 | |
| 3600 | for (const TransportInfo& tinfo : sdesc->transport_infos()) { |
| 3601 | if (!transport_controller_->SetLocalTransportDescription( |
| 3602 | tinfo.content_name, tinfo.description, action, err)) { |
| 3603 | return false; |
| 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | return true; |
| 3608 | } |
| 3609 | |
| 3610 | bool PeerConnection::PushdownRemoteTransportDescription( |
| 3611 | const SessionDescription* sdesc, |
| 3612 | cricket::ContentAction action, |
| 3613 | std::string* err) { |
| 3614 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3615 | |
| 3616 | if (!sdesc) { |
| 3617 | return false; |
| 3618 | } |
| 3619 | |
| 3620 | for (const TransportInfo& tinfo : sdesc->transport_infos()) { |
| 3621 | if (!transport_controller_->SetRemoteTransportDescription( |
| 3622 | tinfo.content_name, tinfo.description, action, err)) { |
| 3623 | return false; |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | return true; |
| 3628 | } |
| 3629 | |
| 3630 | bool PeerConnection::GetTransportDescription( |
| 3631 | const SessionDescription* description, |
| 3632 | const std::string& content_name, |
| 3633 | cricket::TransportDescription* tdesc) { |
| 3634 | if (!description || !tdesc) { |
| 3635 | return false; |
| 3636 | } |
| 3637 | const TransportInfo* transport_info = |
| 3638 | description->GetTransportInfoByName(content_name); |
| 3639 | if (!transport_info) { |
| 3640 | return false; |
| 3641 | } |
| 3642 | *tdesc = transport_info->description; |
| 3643 | return true; |
| 3644 | } |
| 3645 | |
| 3646 | bool PeerConnection::EnableBundle(const cricket::ContentGroup& bundle) { |
| 3647 | const std::string* first_content_name = bundle.FirstContentName(); |
| 3648 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3649 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3650 | return false; |
| 3651 | } |
| 3652 | const std::string& transport_name = *first_content_name; |
| 3653 | |
| 3654 | auto maybe_set_transport = [this, bundle, |
| 3655 | transport_name](cricket::BaseChannel* ch) { |
| 3656 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
| 3657 | return true; |
| 3658 | } |
| 3659 | |
| 3660 | std::string old_transport_name = ch->transport_name(); |
| 3661 | if (old_transport_name == transport_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3662 | RTC_LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 3663 | << " on " << transport_name << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3664 | return true; |
| 3665 | } |
| 3666 | |
| 3667 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 3668 | transport_controller_->CreateDtlsTransport( |
| 3669 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 3670 | bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr); |
| 3671 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 3672 | if (need_rtcp) { |
| 3673 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 3674 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 3675 | } |
| 3676 | |
| 3677 | ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3678 | RTC_LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 3679 | << transport_name << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3680 | transport_controller_->DestroyDtlsTransport( |
| 3681 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 3682 | // If the channel needs rtcp, it means that the channel used to have a |
| 3683 | // rtcp transport which needs to be deleted now. |
| 3684 | if (need_rtcp) { |
| 3685 | transport_controller_->DestroyDtlsTransport( |
| 3686 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 3687 | } |
| 3688 | return true; |
| 3689 | }; |
| 3690 | |
| 3691 | if (!maybe_set_transport(voice_channel()) || |
| 3692 | !maybe_set_transport(video_channel()) || |
| 3693 | !maybe_set_transport(rtp_data_channel())) { |
| 3694 | return false; |
| 3695 | } |
| 3696 | // For SCTP, transport creation/deletion happens here instead of in the |
| 3697 | // object itself. |
| 3698 | if (sctp_transport_) { |
| 3699 | RTC_DCHECK(sctp_transport_name_); |
| 3700 | RTC_DCHECK(sctp_content_name_); |
| 3701 | if (transport_name != *sctp_transport_name_ && |
| 3702 | bundle.HasContentName(*sctp_content_name_)) { |
| 3703 | network_thread()->Invoke<void>( |
| 3704 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::ChangeSctpTransport_n, this, |
| 3705 | transport_name)); |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | return true; |
| 3710 | } |
| 3711 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3712 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 3713 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 3714 | cricket::ContinualGatheringPolicy gathering_policy; |
| 3715 | // TODO(honghaiz): Add the third continual gathering policy in |
| 3716 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 3717 | switch (config.continual_gathering_policy) { |
| 3718 | case PeerConnectionInterface::GATHER_ONCE: |
| 3719 | gathering_policy = cricket::GATHER_ONCE; |
| 3720 | break; |
| 3721 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 3722 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 3723 | break; |
| 3724 | default: |
| 3725 | RTC_NOTREACHED(); |
| 3726 | gathering_policy = cricket::GATHER_ONCE; |
| 3727 | } |
| 3728 | cricket::IceConfig ice_config; |
| 3729 | ice_config.receiving_timeout = config.ice_connection_receiving_timeout; |
| 3730 | ice_config.prioritize_most_likely_candidate_pairs = |
| 3731 | config.prioritize_most_likely_ice_candidate_pairs; |
| 3732 | ice_config.backup_connection_ping_interval = |
| 3733 | config.ice_backup_candidate_pair_ping_interval; |
| 3734 | ice_config.continual_gathering_policy = gathering_policy; |
| 3735 | ice_config.presume_writable_when_fully_relayed = |
| 3736 | config.presume_writable_when_fully_relayed; |
| 3737 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
| 3738 | ice_config.regather_all_networks_interval_range = |
| 3739 | config.ice_regather_interval_range; |
| 3740 | return ice_config; |
| 3741 | } |
| 3742 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3743 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 3744 | std::string* track_id) { |
| 3745 | if (!local_description()) { |
| 3746 | return false; |
| 3747 | } |
| 3748 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 3749 | track_id); |
| 3750 | } |
| 3751 | |
| 3752 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 3753 | std::string* track_id) { |
| 3754 | if (!remote_description()) { |
| 3755 | return false; |
| 3756 | } |
| 3757 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 3758 | track_id); |
| 3759 | } |
| 3760 | |
| 3761 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 3762 | const rtc::CopyOnWriteBuffer& payload, |
| 3763 | cricket::SendDataResult* result) { |
| 3764 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3765 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
| 3766 | << "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3767 | return false; |
| 3768 | } |
| 3769 | return rtp_data_channel_ |
| 3770 | ? rtp_data_channel_->SendData(params, payload, result) |
| 3771 | : network_thread()->Invoke<bool>( |
| 3772 | RTC_FROM_HERE, |
| 3773 | Bind(&cricket::SctpTransportInternal::SendData, |
| 3774 | sctp_transport_.get(), params, payload, result)); |
| 3775 | } |
| 3776 | |
| 3777 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 3778 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 3779 | // Don't log an error here, because DataChannels are expected to call |
| 3780 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 3781 | // whether or not the underlying transport is ready. |
| 3782 | return false; |
| 3783 | } |
| 3784 | if (rtp_data_channel_) { |
| 3785 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 3786 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 3787 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 3788 | &DataChannel::OnDataReceived); |
| 3789 | } else { |
| 3790 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 3791 | &DataChannel::OnChannelReady); |
| 3792 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 3793 | &DataChannel::OnDataReceived); |
| 3794 | SignalSctpStreamClosedRemotely.connect( |
| 3795 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 3796 | } |
| 3797 | return true; |
| 3798 | } |
| 3799 | |
| 3800 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 3801 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3802 | RTC_LOG(LS_ERROR) |
| 3803 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 3804 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3805 | return; |
| 3806 | } |
| 3807 | if (rtp_data_channel_) { |
| 3808 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 3809 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 3810 | } else { |
| 3811 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 3812 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 3813 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 3814 | } |
| 3815 | } |
| 3816 | |
| 3817 | void PeerConnection::AddSctpDataStream(int sid) { |
| 3818 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3819 | RTC_LOG(LS_ERROR) |
| 3820 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3821 | return; |
| 3822 | } |
| 3823 | network_thread()->Invoke<void>( |
| 3824 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 3825 | sctp_transport_.get(), sid)); |
| 3826 | } |
| 3827 | |
| 3828 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 3829 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3830 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
| 3831 | << "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3832 | return; |
| 3833 | } |
| 3834 | network_thread()->Invoke<void>( |
| 3835 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 3836 | sctp_transport_.get(), sid)); |
| 3837 | } |
| 3838 | |
| 3839 | bool PeerConnection::ReadyToSendData() const { |
| 3840 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 3841 | sctp_ready_to_send_data_; |
| 3842 | } |
| 3843 | |
| 3844 | std::unique_ptr<SessionStats> PeerConnection::GetSessionStats_s() { |
| 3845 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3846 | ChannelNamePairs channel_name_pairs; |
| 3847 | if (voice_channel()) { |
| 3848 | channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair( |
| 3849 | voice_channel()->content_name(), voice_channel()->transport_name())); |
| 3850 | } |
| 3851 | if (video_channel()) { |
| 3852 | channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair( |
| 3853 | video_channel()->content_name(), video_channel()->transport_name())); |
| 3854 | } |
| 3855 | if (rtp_data_channel()) { |
| 3856 | channel_name_pairs.data = rtc::Optional<ChannelNamePair>( |
| 3857 | ChannelNamePair(rtp_data_channel()->content_name(), |
| 3858 | rtp_data_channel()->transport_name())); |
| 3859 | } |
| 3860 | if (sctp_transport_) { |
| 3861 | RTC_DCHECK(sctp_content_name_); |
| 3862 | RTC_DCHECK(sctp_transport_name_); |
| 3863 | channel_name_pairs.data = rtc::Optional<ChannelNamePair>( |
| 3864 | ChannelNamePair(*sctp_content_name_, *sctp_transport_name_)); |
| 3865 | } |
| 3866 | return GetSessionStats(channel_name_pairs); |
| 3867 | } |
| 3868 | |
| 3869 | std::unique_ptr<SessionStats> PeerConnection::GetSessionStats( |
| 3870 | const ChannelNamePairs& channel_name_pairs) { |
| 3871 | if (network_thread()->IsCurrent()) { |
| 3872 | return GetSessionStats_n(channel_name_pairs); |
| 3873 | } |
| 3874 | return network_thread()->Invoke<std::unique_ptr<SessionStats>>( |
| 3875 | RTC_FROM_HERE, |
| 3876 | rtc::Bind(&PeerConnection::GetSessionStats_n, this, channel_name_pairs)); |
| 3877 | } |
| 3878 | |
| 3879 | bool PeerConnection::GetLocalCertificate( |
| 3880 | const std::string& transport_name, |
| 3881 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 3882 | return transport_controller_->GetLocalCertificate(transport_name, |
| 3883 | certificate); |
| 3884 | } |
| 3885 | |
| 3886 | std::unique_ptr<rtc::SSLCertificate> PeerConnection::GetRemoteSSLCertificate( |
| 3887 | const std::string& transport_name) { |
| 3888 | return transport_controller_->GetRemoteSSLCertificate(transport_name); |
| 3889 | } |
| 3890 | |
| 3891 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 3892 | return data_channel_type_; |
| 3893 | } |
| 3894 | |
| 3895 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 3896 | return pending_ice_restarts_.find(content_name) != |
| 3897 | pending_ice_restarts_.end(); |
| 3898 | } |
| 3899 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3900 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 3901 | return transport_controller_->NeedsIceRestart(content_name); |
| 3902 | } |
| 3903 | |
| 3904 | void PeerConnection::OnCertificateReady( |
| 3905 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 3906 | transport_controller_->SetLocalCertificate(certificate); |
| 3907 | } |
| 3908 | |
| 3909 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
| 3910 | SetError(ERROR_TRANSPORT, |
| 3911 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
| 3912 | } |
| 3913 | |
| 3914 | void PeerConnection::OnTransportControllerConnectionState( |
| 3915 | cricket::IceConnectionState state) { |
| 3916 | switch (state) { |
| 3917 | case cricket::kIceConnectionConnecting: |
| 3918 | // If the current state is Connected or Completed, then there were |
| 3919 | // writable channels but now there are not, so the next state must |
| 3920 | // be Disconnected. |
| 3921 | // kIceConnectionConnecting is currently used as the default, |
| 3922 | // un-connected state by the TransportController, so its only use is |
| 3923 | // detecting disconnections. |
| 3924 | if (ice_connection_state_ == |
| 3925 | PeerConnectionInterface::kIceConnectionConnected || |
| 3926 | ice_connection_state_ == |
| 3927 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 3928 | SetIceConnectionState( |
| 3929 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 3930 | } |
| 3931 | break; |
| 3932 | case cricket::kIceConnectionFailed: |
| 3933 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 3934 | break; |
| 3935 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3936 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 3937 | << "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3938 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 3939 | break; |
| 3940 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3941 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 3942 | << "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3943 | if (ice_connection_state_ != |
| 3944 | PeerConnectionInterface::kIceConnectionConnected) { |
| 3945 | // If jumping directly from "checking" to "connected", |
| 3946 | // signal "connected" first. |
| 3947 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 3948 | } |
| 3949 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 3950 | if (metrics_observer()) { |
| 3951 | ReportTransportStats(); |
| 3952 | } |
| 3953 | break; |
| 3954 | default: |
| 3955 | RTC_NOTREACHED(); |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 3960 | const std::string& transport_name, |
| 3961 | const cricket::Candidates& candidates) { |
| 3962 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3963 | int sdp_mline_index; |
| 3964 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3965 | RTC_LOG(LS_ERROR) |
| 3966 | << "OnTransportControllerCandidatesGathered: content name " |
| 3967 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3968 | return; |
| 3969 | } |
| 3970 | |
| 3971 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 3972 | citer != candidates.end(); ++citer) { |
| 3973 | // Use transport_name as the candidate media id. |
| 3974 | std::unique_ptr<JsepIceCandidate> candidate( |
| 3975 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 3976 | if (local_description()) { |
| 3977 | mutable_local_description()->AddCandidate(candidate.get()); |
| 3978 | } |
| 3979 | OnIceCandidate(std::move(candidate)); |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 3984 | const std::vector<cricket::Candidate>& candidates) { |
| 3985 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3986 | // Sanity check. |
| 3987 | for (const cricket::Candidate& candidate : candidates) { |
| 3988 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3989 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
| 3990 | << "empty content name in candidate " |
| 3991 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3992 | return; |
| 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | if (local_description()) { |
| 3997 | mutable_local_description()->RemoveCandidates(candidates); |
| 3998 | } |
| 3999 | OnIceCandidatesRemoved(candidates); |
| 4000 | } |
| 4001 | |
| 4002 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 4003 | rtc::SSLHandshakeError error) { |
| 4004 | if (metrics_observer()) { |
| 4005 | metrics_observer()->IncrementEnumCounter( |
| 4006 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 4007 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 4008 | } |
| 4009 | } |
| 4010 | |
| 4011 | // Enabling voice and video (and RTP data) channels. |
| 4012 | void PeerConnection::EnableChannels() { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4013 | if (voice_channel() && !voice_channel()->enabled()) { |
| 4014 | voice_channel()->Enable(true); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4015 | } |
| 4016 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4017 | if (video_channel() && !video_channel()->enabled()) { |
| 4018 | video_channel()->Enable(true); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4019 | } |
| 4020 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4021 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4022 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4023 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4024 | } |
| 4025 | |
| 4026 | // Returns the media index for a local ice candidate given the content name. |
| 4027 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 4028 | const std::string& content_name, |
| 4029 | int* sdp_mline_index) { |
| 4030 | if (!local_description() || !sdp_mline_index) { |
| 4031 | return false; |
| 4032 | } |
| 4033 | |
| 4034 | bool content_found = false; |
| 4035 | const ContentInfos& contents = local_description()->description()->contents(); |
| 4036 | for (size_t index = 0; index < contents.size(); ++index) { |
| 4037 | if (contents[index].name == content_name) { |
| 4038 | *sdp_mline_index = static_cast<int>(index); |
| 4039 | content_found = true; |
| 4040 | break; |
| 4041 | } |
| 4042 | } |
| 4043 | return content_found; |
| 4044 | } |
| 4045 | |
| 4046 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 4047 | const SessionDescriptionInterface* remote_desc) { |
| 4048 | if (!remote_desc) { |
| 4049 | return true; |
| 4050 | } |
| 4051 | bool ret = true; |
| 4052 | |
| 4053 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 4054 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 4055 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 4056 | const IceCandidateInterface* candidate = candidates->at(n); |
| 4057 | bool valid = false; |
| 4058 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 4059 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4060 | RTC_LOG(LS_INFO) |
| 4061 | << "UseCandidatesInSessionDescription: Not ready to use " |
| 4062 | << "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4063 | } |
| 4064 | continue; |
| 4065 | } |
| 4066 | ret = UseCandidate(candidate); |
| 4067 | if (!ret) { |
| 4068 | break; |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | return ret; |
| 4073 | } |
| 4074 | |
| 4075 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 4076 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 4077 | size_t remote_content_size = |
| 4078 | remote_description()->description()->contents().size(); |
| 4079 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4080 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4081 | return false; |
| 4082 | } |
| 4083 | |
| 4084 | cricket::ContentInfo content = |
| 4085 | remote_description()->description()->contents()[mediacontent_index]; |
| 4086 | std::vector<cricket::Candidate> candidates; |
| 4087 | candidates.push_back(candidate->candidate()); |
| 4088 | // Invoking BaseSession method to handle remote candidates. |
| 4089 | std::string error; |
| 4090 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 4091 | &error)) { |
| 4092 | // Candidates successfully submitted for checking. |
| 4093 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 4094 | ice_connection_state_ == |
| 4095 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 4096 | // If state is New, then the session has just gotten its first remote ICE |
| 4097 | // candidates, so go to Checking. |
| 4098 | // If state is Disconnected, the session is re-using old candidates or |
| 4099 | // receiving additional ones, so go to Checking. |
| 4100 | // If state is Connected, stay Connected. |
| 4101 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 4102 | // newly added transport, then the state actually _should_ move to |
| 4103 | // checking. Add a way to distinguish that case. |
| 4104 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 4105 | } |
| 4106 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 4107 | } else { |
| 4108 | if (!error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4109 | RTC_LOG(LS_WARNING) << error; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4110 | } |
| 4111 | } |
| 4112 | return true; |
| 4113 | } |
| 4114 | |
| 4115 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
| 4116 | // TODO(steveanton): Add support for multiple audio/video channels. |
| 4117 | // Destroy video channel first since it may have a pointer to the |
| 4118 | // voice channel. |
| 4119 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
| 4120 | if ((!video_info || video_info->rejected) && video_channel()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4121 | DestroyVideoChannel(video_channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | const cricket::ContentInfo* voice_info = cricket::GetFirstAudioContent(desc); |
| 4125 | if ((!voice_info || voice_info->rejected) && voice_channel()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4126 | DestroyVoiceChannel(voice_channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 4130 | if (!data_info || data_info->rejected) { |
| 4131 | if (rtp_data_channel_) { |
| 4132 | DestroyDataChannel(); |
| 4133 | } |
| 4134 | if (sctp_transport_) { |
| 4135 | OnDataChannelDestroyed(); |
| 4136 | network_thread()->Invoke<void>( |
| 4137 | RTC_FROM_HERE, |
| 4138 | rtc::Bind(&PeerConnection::DestroySctpTransport_n, this)); |
| 4139 | } |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | // Returns the name of the transport channel when BUNDLE is enabled, or nullptr |
| 4144 | // if the channel is not part of any bundle. |
| 4145 | const std::string* PeerConnection::GetBundleTransportName( |
| 4146 | const cricket::ContentInfo* content, |
| 4147 | const cricket::ContentGroup* bundle) { |
| 4148 | if (!bundle) { |
| 4149 | return nullptr; |
| 4150 | } |
| 4151 | const std::string* first_content_name = bundle->FirstContentName(); |
| 4152 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4153 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4154 | return nullptr; |
| 4155 | } |
| 4156 | if (!bundle->HasContentName(content->name)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4157 | RTC_LOG(LS_WARNING) << content->name << " is not part of any bundle group"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4158 | return nullptr; |
| 4159 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4160 | RTC_LOG(LS_INFO) << "Bundling " << content->name << " on " |
| 4161 | << *first_content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4162 | return first_content_name; |
| 4163 | } |
| 4164 | |
| 4165 | bool PeerConnection::CreateChannels(const SessionDescription* desc) { |
| 4166 | // TODO(steveanton): Add support for multiple audio/video channels. |
| 4167 | const cricket::ContentGroup* bundle_group = nullptr; |
| 4168 | if (configuration_.bundle_policy == |
| 4169 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
| 4170 | bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 4171 | if (!bundle_group) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4172 | RTC_LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4173 | return false; |
| 4174 | } |
| 4175 | } |
| 4176 | // Creating the media channels and transport proxies. |
| 4177 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc); |
| 4178 | if (voice && !voice->rejected && !voice_channel()) { |
| 4179 | if (!CreateVoiceChannel(voice, |
| 4180 | GetBundleTransportName(voice, bundle_group))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4181 | RTC_LOG(LS_ERROR) << "Failed to create voice channel."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4182 | return false; |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc); |
| 4187 | if (video && !video->rejected && !video_channel()) { |
| 4188 | if (!CreateVideoChannel(video, |
| 4189 | GetBundleTransportName(video, bundle_group))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4190 | RTC_LOG(LS_ERROR) << "Failed to create video channel."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4191 | return false; |
| 4192 | } |
| 4193 | } |
| 4194 | |
| 4195 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc); |
| 4196 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 4197 | !rtp_data_channel_ && !sctp_transport_) { |
| 4198 | if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4199 | RTC_LOG(LS_ERROR) << "Failed to create data channel."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4200 | return false; |
| 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | return true; |
| 4205 | } |
| 4206 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4207 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4208 | bool PeerConnection::CreateVoiceChannel(const cricket::ContentInfo* content, |
| 4209 | const std::string* bundle_transport) { |
| 4210 | // TODO(steveanton): Check to see if it's safe to create multiple voice |
| 4211 | // channels. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4212 | RTC_DCHECK(!voice_channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4213 | |
| 4214 | std::string transport_name = |
| 4215 | bundle_transport ? *bundle_transport : content->name; |
| 4216 | |
| 4217 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4218 | transport_controller_->CreateDtlsTransport( |
| 4219 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4220 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4221 | if (configuration_.rtcp_mux_policy != |
| 4222 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 4223 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4224 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4225 | } |
| 4226 | |
| 4227 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
| 4228 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
| 4229 | rtcp_dtls_transport, transport_controller_->signaling_thread(), |
| 4230 | content->name, SrtpRequired(), audio_options_); |
| 4231 | if (!voice_channel) { |
| 4232 | transport_controller_->DestroyDtlsTransport( |
| 4233 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4234 | if (rtcp_dtls_transport) { |
| 4235 | transport_controller_->DestroyDtlsTransport( |
| 4236 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4237 | } |
| 4238 | return false; |
| 4239 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4240 | voice_channel->SignalRtcpMuxFullyActive.connect( |
| 4241 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 4242 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 4243 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4244 | voice_channel->SignalSentPacket.connect(this, |
| 4245 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4246 | |
| 4247 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 4248 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4249 | return true; |
| 4250 | } |
| 4251 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4252 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4253 | bool PeerConnection::CreateVideoChannel(const cricket::ContentInfo* content, |
| 4254 | const std::string* bundle_transport) { |
| 4255 | // TODO(steveanton): Check to see if it's safe to create multiple video |
| 4256 | // channels. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4257 | RTC_DCHECK(!video_channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4258 | |
| 4259 | std::string transport_name = |
| 4260 | bundle_transport ? *bundle_transport : content->name; |
| 4261 | |
| 4262 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4263 | transport_controller_->CreateDtlsTransport( |
| 4264 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4265 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4266 | if (configuration_.rtcp_mux_policy != |
| 4267 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 4268 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4269 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4270 | } |
| 4271 | |
| 4272 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
| 4273 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
| 4274 | rtcp_dtls_transport, transport_controller_->signaling_thread(), |
| 4275 | content->name, SrtpRequired(), video_options_); |
| 4276 | |
| 4277 | if (!video_channel) { |
| 4278 | transport_controller_->DestroyDtlsTransport( |
| 4279 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4280 | if (rtcp_dtls_transport) { |
| 4281 | transport_controller_->DestroyDtlsTransport( |
| 4282 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4283 | } |
| 4284 | return false; |
| 4285 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4286 | video_channel->SignalRtcpMuxFullyActive.connect( |
| 4287 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 4288 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 4289 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4290 | video_channel->SignalSentPacket.connect(this, |
| 4291 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4292 | |
| 4293 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
| 4294 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4295 | return true; |
| 4296 | } |
| 4297 | |
| 4298 | bool PeerConnection::CreateDataChannel(const cricket::ContentInfo* content, |
| 4299 | const std::string* bundle_transport) { |
| 4300 | const std::string transport_name = |
| 4301 | bundle_transport ? *bundle_transport : content->name; |
| 4302 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 4303 | if (sctp) { |
| 4304 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4305 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4306 | << "Trying to create SCTP transport, but didn't compile with " |
| 4307 | "SCTP support (HAVE_SCTP)"; |
| 4308 | return false; |
| 4309 | } |
| 4310 | if (!network_thread()->Invoke<bool>( |
| 4311 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::CreateSctpTransport_n, |
| 4312 | this, content->name, transport_name))) { |
| 4313 | return false; |
| 4314 | } |
| 4315 | } else { |
| 4316 | std::string transport_name = |
| 4317 | bundle_transport ? *bundle_transport : content->name; |
| 4318 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4319 | transport_controller_->CreateDtlsTransport( |
| 4320 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4321 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4322 | if (configuration_.rtcp_mux_policy != |
| 4323 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 4324 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4325 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4326 | } |
| 4327 | |
| 4328 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 4329 | configuration_.media_config, rtp_dtls_transport, rtcp_dtls_transport, |
| 4330 | transport_controller_->signaling_thread(), content->name, |
| 4331 | SrtpRequired()); |
| 4332 | |
| 4333 | if (!rtp_data_channel_) { |
| 4334 | transport_controller_->DestroyDtlsTransport( |
| 4335 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4336 | if (rtcp_dtls_transport) { |
| 4337 | transport_controller_->DestroyDtlsTransport( |
| 4338 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4339 | } |
| 4340 | return false; |
| 4341 | } |
| 4342 | |
| 4343 | rtp_data_channel_->SignalRtcpMuxFullyActive.connect( |
| 4344 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 4345 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 4346 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 4347 | rtp_data_channel_->SignalSentPacket.connect( |
| 4348 | this, &PeerConnection::OnSentPacket_w); |
| 4349 | } |
| 4350 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4351 | for (const auto& channel : sctp_data_channels_) { |
| 4352 | channel->OnTransportChannelCreated(); |
| 4353 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4354 | |
| 4355 | return true; |
| 4356 | } |
| 4357 | |
| 4358 | Call::Stats PeerConnection::GetCallStats() { |
| 4359 | if (!worker_thread()->IsCurrent()) { |
| 4360 | return worker_thread()->Invoke<Call::Stats>( |
| 4361 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 4362 | } |
| 4363 | if (call_) { |
| 4364 | return call_->GetStats(); |
| 4365 | } else { |
| 4366 | return Call::Stats(); |
| 4367 | } |
| 4368 | } |
| 4369 | |
| 4370 | std::unique_ptr<SessionStats> PeerConnection::GetSessionStats_n( |
| 4371 | const ChannelNamePairs& channel_name_pairs) { |
| 4372 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4373 | std::unique_ptr<SessionStats> session_stats(new SessionStats()); |
| 4374 | for (const auto channel_name_pair : |
| 4375 | {&channel_name_pairs.voice, &channel_name_pairs.video, |
| 4376 | &channel_name_pairs.data}) { |
| 4377 | if (*channel_name_pair) { |
| 4378 | cricket::TransportStats transport_stats; |
| 4379 | if (!transport_controller_->GetStats((*channel_name_pair)->transport_name, |
| 4380 | &transport_stats)) { |
| 4381 | return nullptr; |
| 4382 | } |
| 4383 | session_stats->proxy_to_transport[(*channel_name_pair)->content_name] = |
| 4384 | (*channel_name_pair)->transport_name; |
| 4385 | session_stats->transport_stats[(*channel_name_pair)->transport_name] = |
| 4386 | std::move(transport_stats); |
| 4387 | } |
| 4388 | } |
| 4389 | return session_stats; |
| 4390 | } |
| 4391 | |
| 4392 | bool PeerConnection::CreateSctpTransport_n(const std::string& content_name, |
| 4393 | const std::string& transport_name) { |
| 4394 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4395 | RTC_DCHECK(sctp_factory_); |
| 4396 | cricket::DtlsTransportInternal* tc = |
| 4397 | transport_controller_->CreateDtlsTransport_n( |
| 4398 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4399 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 4400 | RTC_DCHECK(sctp_transport_); |
| 4401 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 4402 | sctp_transport_->SignalReadyToSendData.connect( |
| 4403 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 4404 | sctp_transport_->SignalDataReceived.connect( |
| 4405 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 4406 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 4407 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
| 4408 | sctp_transport_name_ = rtc::Optional<std::string>(transport_name); |
| 4409 | sctp_content_name_ = rtc::Optional<std::string>(content_name); |
| 4410 | return true; |
| 4411 | } |
| 4412 | |
| 4413 | void PeerConnection::ChangeSctpTransport_n(const std::string& transport_name) { |
| 4414 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4415 | RTC_DCHECK(sctp_transport_); |
| 4416 | RTC_DCHECK(sctp_transport_name_); |
| 4417 | std::string old_sctp_transport_name = *sctp_transport_name_; |
| 4418 | sctp_transport_name_ = rtc::Optional<std::string>(transport_name); |
| 4419 | cricket::DtlsTransportInternal* tc = |
| 4420 | transport_controller_->CreateDtlsTransport_n( |
| 4421 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4422 | sctp_transport_->SetTransportChannel(tc); |
| 4423 | transport_controller_->DestroyDtlsTransport_n( |
| 4424 | old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4425 | } |
| 4426 | |
| 4427 | void PeerConnection::DestroySctpTransport_n() { |
| 4428 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4429 | sctp_transport_.reset(nullptr); |
| 4430 | sctp_content_name_.reset(); |
| 4431 | sctp_transport_name_.reset(); |
| 4432 | sctp_invoker_.reset(nullptr); |
| 4433 | sctp_ready_to_send_data_ = false; |
| 4434 | } |
| 4435 | |
| 4436 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 4437 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 4438 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4439 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 4440 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 4441 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 4442 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 4443 | // pending tasks will be cleared. |
| 4444 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 4445 | OnSctpTransportReadyToSendData_s(true); |
| 4446 | }); |
| 4447 | } |
| 4448 | |
| 4449 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 4450 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 4451 | sctp_ready_to_send_data_ = ready; |
| 4452 | SignalSctpReadyToSendData(ready); |
| 4453 | } |
| 4454 | |
| 4455 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 4456 | const cricket::ReceiveDataParams& params, |
| 4457 | const rtc::CopyOnWriteBuffer& payload) { |
| 4458 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 4459 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4460 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 4461 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 4462 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 4463 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 4464 | // pending tasks will be cleared. |
| 4465 | sctp_invoker_->AsyncInvoke<void>( |
| 4466 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 4467 | OnSctpTransportDataReceived_s(params, payload); |
| 4468 | }); |
| 4469 | } |
| 4470 | |
| 4471 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 4472 | const cricket::ReceiveDataParams& params, |
| 4473 | const rtc::CopyOnWriteBuffer& payload) { |
| 4474 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 4475 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 4476 | // Received OPEN message; parse and signal that a new data channel should |
| 4477 | // be created. |
| 4478 | std::string label; |
| 4479 | InternalDataChannelInit config; |
| 4480 | config.id = params.ssrc; |
| 4481 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4482 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 4483 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4484 | return; |
| 4485 | } |
| 4486 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 4487 | OnDataChannelOpenMessage(label, config); |
| 4488 | } else { |
| 4489 | // Otherwise just forward the signal. |
| 4490 | SignalSctpDataReceived(params, payload); |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 4495 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 4496 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4497 | sctp_invoker_->AsyncInvoke<void>( |
| 4498 | RTC_FROM_HERE, signaling_thread(), |
| 4499 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 4500 | &SignalSctpStreamClosedRemotely, sid)); |
| 4501 | } |
| 4502 | |
| 4503 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 4504 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 4505 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 4506 | if (!bundle_enabled) |
| 4507 | return true; |
| 4508 | |
| 4509 | const cricket::ContentGroup* bundle_group = |
| 4510 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 4511 | RTC_DCHECK(bundle_group != NULL); |
| 4512 | |
| 4513 | const cricket::ContentInfos& contents = desc->contents(); |
| 4514 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 4515 | citer != contents.end(); ++citer) { |
| 4516 | const cricket::ContentInfo* content = (&*citer); |
| 4517 | RTC_DCHECK(content != NULL); |
| 4518 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
| 4519 | content->type == cricket::NS_JINGLE_RTP) { |
| 4520 | if (!HasRtcpMuxEnabled(content)) |
| 4521 | return false; |
| 4522 | } |
| 4523 | } |
| 4524 | // RTCP-MUX is enabled in all the contents. |
| 4525 | return true; |
| 4526 | } |
| 4527 | |
| 4528 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
| 4529 | const cricket::MediaContentDescription* description = |
| 4530 | static_cast<cricket::MediaContentDescription*>(content->description); |
| 4531 | return description->rtcp_mux(); |
| 4532 | } |
| 4533 | |
| 4534 | bool PeerConnection::ValidateSessionDescription( |
| 4535 | const SessionDescriptionInterface* sdesc, |
| 4536 | cricket::ContentSource source, |
| 4537 | std::string* err_desc) { |
| 4538 | std::string type; |
| 4539 | if (error() != ERROR_NONE) { |
| 4540 | return BadSdp(source, type, GetSessionErrorMsg(), err_desc); |
| 4541 | } |
| 4542 | |
| 4543 | if (!sdesc || !sdesc->description()) { |
| 4544 | return BadSdp(source, type, kInvalidSdp, err_desc); |
| 4545 | } |
| 4546 | |
| 4547 | type = sdesc->type(); |
| 4548 | Action action = GetAction(sdesc->type()); |
| 4549 | if (source == cricket::CS_LOCAL) { |
| 4550 | if (!ExpectSetLocalDescription(action)) |
| 4551 | return BadLocalSdp(type, BadStateErrMsg(signaling_state()), err_desc); |
| 4552 | } else { |
| 4553 | if (!ExpectSetRemoteDescription(action)) |
| 4554 | return BadRemoteSdp(type, BadStateErrMsg(signaling_state()), err_desc); |
| 4555 | } |
| 4556 | |
| 4557 | // Verify crypto settings. |
| 4558 | std::string crypto_error; |
| 4559 | if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 4560 | dtls_enabled_) && |
| 4561 | !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) { |
| 4562 | return BadSdp(source, type, crypto_error, err_desc); |
| 4563 | } |
| 4564 | |
| 4565 | // Verify ice-ufrag and ice-pwd. |
| 4566 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
| 4567 | return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc); |
| 4568 | } |
| 4569 | |
| 4570 | if (!ValidateBundleSettings(sdesc->description())) { |
| 4571 | return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc); |
| 4572 | } |
| 4573 | |
| 4574 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 4575 | // m-lines that do not rtcp-mux enabled. |
| 4576 | |
| 4577 | // Verify m-lines in Answer when compared against Offer. |
| 4578 | if (action == kAnswer || action == kPrAnswer) { |
| 4579 | const cricket::SessionDescription* offer_desc = |
| 4580 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 4581 | : local_description()->description(); |
| 4582 | if (!MediaSectionsHaveSameCount(offer_desc, sdesc->description()) || |
| 4583 | !MediaSectionsInSameOrder(offer_desc, sdesc->description())) { |
| 4584 | return BadAnswerSdp(source, kMlineMismatchInAnswer, err_desc); |
| 4585 | } |
| 4586 | } else { |
| 4587 | const cricket::SessionDescription* current_desc = nullptr; |
| 4588 | if (source == cricket::CS_LOCAL && local_description()) { |
| 4589 | current_desc = local_description()->description(); |
| 4590 | } else if (source == cricket::CS_REMOTE && remote_description()) { |
| 4591 | current_desc = remote_description()->description(); |
| 4592 | } |
| 4593 | // The re-offers should respect the order of m= sections in current |
| 4594 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
| 4595 | if (current_desc && |
| 4596 | !MediaSectionsInSameOrder(current_desc, sdesc->description())) { |
| 4597 | return BadOfferSdp(source, kMlineMismatchInSubsequentOffer, err_desc); |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | return true; |
| 4602 | } |
| 4603 | |
| 4604 | bool PeerConnection::ExpectSetLocalDescription(Action action) { |
| 4605 | PeerConnectionInterface::SignalingState state = signaling_state(); |
| 4606 | if (action == kOffer) { |
| 4607 | return (state == PeerConnectionInterface::kStable) || |
| 4608 | (state == PeerConnectionInterface::kHaveLocalOffer); |
| 4609 | } else { // Answer or PrAnswer |
| 4610 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 4611 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 4612 | } |
| 4613 | } |
| 4614 | |
| 4615 | bool PeerConnection::ExpectSetRemoteDescription(Action action) { |
| 4616 | PeerConnectionInterface::SignalingState state = signaling_state(); |
| 4617 | if (action == kOffer) { |
| 4618 | return (state == PeerConnectionInterface::kStable) || |
| 4619 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
| 4620 | } else { // Answer or PrAnswer. |
| 4621 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 4622 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | std::string PeerConnection::GetSessionErrorMsg() { |
| 4627 | std::ostringstream desc; |
| 4628 | desc << kSessionError << GetErrorCodeString(error()) << ". "; |
| 4629 | desc << kSessionErrorDesc << error_desc() << "."; |
| 4630 | return desc.str(); |
| 4631 | } |
| 4632 | |
| 4633 | // We need to check the local/remote description for the Transport instead of |
| 4634 | // the session, because a new Transport added during renegotiation may have |
| 4635 | // them unset while the session has them set from the previous negotiation. |
| 4636 | // Not doing so may trigger the auto generation of transport description and |
| 4637 | // mess up DTLS identity information, ICE credential, etc. |
| 4638 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 4639 | const IceCandidateInterface* candidate, |
| 4640 | const SessionDescriptionInterface* remote_desc, |
| 4641 | bool* valid) { |
| 4642 | *valid = true; |
| 4643 | |
| 4644 | const SessionDescriptionInterface* current_remote_desc = |
| 4645 | remote_desc ? remote_desc : remote_description(); |
| 4646 | |
| 4647 | if (!current_remote_desc) { |
| 4648 | return false; |
| 4649 | } |
| 4650 | |
| 4651 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 4652 | size_t remote_content_size = |
| 4653 | current_remote_desc->description()->contents().size(); |
| 4654 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4655 | RTC_LOG(LS_ERROR) |
| 4656 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 4657 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4658 | |
| 4659 | *valid = false; |
| 4660 | return false; |
| 4661 | } |
| 4662 | |
| 4663 | cricket::ContentInfo content = |
| 4664 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 4665 | |
| 4666 | const std::string transport_name = GetTransportName(content.name); |
| 4667 | if (transport_name.empty()) { |
| 4668 | return false; |
| 4669 | } |
| 4670 | return transport_controller_->ReadyForRemoteCandidates(transport_name); |
| 4671 | } |
| 4672 | |
| 4673 | bool PeerConnection::SrtpRequired() const { |
| 4674 | return dtls_enabled_ || |
| 4675 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 4676 | } |
| 4677 | |
| 4678 | void PeerConnection::OnTransportControllerGatheringState( |
| 4679 | cricket::IceGatheringState state) { |
| 4680 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 4681 | if (state == cricket::kIceGatheringGathering) { |
| 4682 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 4683 | } else if (state == cricket::kIceGatheringComplete) { |
| 4684 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | void PeerConnection::ReportTransportStats() { |
| 4689 | // Use a set so we don't report the same stats twice if two channels share |
| 4690 | // a transport. |
| 4691 | std::set<std::string> transport_names; |
| 4692 | if (voice_channel()) { |
| 4693 | transport_names.insert(voice_channel()->transport_name()); |
| 4694 | } |
| 4695 | if (video_channel()) { |
| 4696 | transport_names.insert(video_channel()->transport_name()); |
| 4697 | } |
| 4698 | if (rtp_data_channel()) { |
| 4699 | transport_names.insert(rtp_data_channel()->transport_name()); |
| 4700 | } |
| 4701 | if (sctp_transport_name_) { |
| 4702 | transport_names.insert(*sctp_transport_name_); |
| 4703 | } |
| 4704 | for (const auto& name : transport_names) { |
| 4705 | cricket::TransportStats stats; |
| 4706 | if (transport_controller_->GetStats(name, &stats)) { |
| 4707 | ReportBestConnectionState(stats); |
| 4708 | ReportNegotiatedCiphers(stats); |
| 4709 | } |
| 4710 | } |
| 4711 | } |
| 4712 | // Walk through the ConnectionInfos to gather best connection usage |
| 4713 | // for IPv4 and IPv6. |
| 4714 | void PeerConnection::ReportBestConnectionState( |
| 4715 | const cricket::TransportStats& stats) { |
| 4716 | RTC_DCHECK(metrics_observer()); |
| 4717 | for (cricket::TransportChannelStatsList::const_iterator it = |
| 4718 | stats.channel_stats.begin(); |
| 4719 | it != stats.channel_stats.end(); ++it) { |
| 4720 | for (cricket::ConnectionInfos::const_iterator it_info = |
| 4721 | it->connection_infos.begin(); |
| 4722 | it_info != it->connection_infos.end(); ++it_info) { |
| 4723 | if (!it_info->best_connection) { |
| 4724 | continue; |
| 4725 | } |
| 4726 | |
| 4727 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
| 4728 | const cricket::Candidate& local = it_info->local_candidate; |
| 4729 | const cricket::Candidate& remote = it_info->remote_candidate; |
| 4730 | |
| 4731 | // Increment the counter for IceCandidatePairType. |
| 4732 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 4733 | (local.type() == RELAY_PORT_TYPE && |
| 4734 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 4735 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 4736 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 4737 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 4738 | } else { |
| 4739 | RTC_CHECK(0); |
| 4740 | } |
| 4741 | metrics_observer()->IncrementEnumCounter( |
| 4742 | type, GetIceCandidatePairCounter(local, remote), |
| 4743 | kIceCandidatePairMax); |
| 4744 | |
| 4745 | // Increment the counter for IP type. |
| 4746 | if (local.address().family() == AF_INET) { |
| 4747 | metrics_observer()->IncrementEnumCounter( |
| 4748 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 4749 | kPeerConnectionAddressFamilyCounter_Max); |
| 4750 | |
| 4751 | } else if (local.address().family() == AF_INET6) { |
| 4752 | metrics_observer()->IncrementEnumCounter( |
| 4753 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 4754 | kPeerConnectionAddressFamilyCounter_Max); |
| 4755 | } else { |
| 4756 | RTC_CHECK(0); |
| 4757 | } |
| 4758 | |
| 4759 | return; |
| 4760 | } |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | void PeerConnection::ReportNegotiatedCiphers( |
| 4765 | const cricket::TransportStats& stats) { |
| 4766 | RTC_DCHECK(metrics_observer()); |
| 4767 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 4768 | return; |
| 4769 | } |
| 4770 | |
| 4771 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 4772 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 4773 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 4774 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 4775 | return; |
| 4776 | } |
| 4777 | |
| 4778 | PeerConnectionEnumCounterType srtp_counter_type; |
| 4779 | PeerConnectionEnumCounterType ssl_counter_type; |
| 4780 | if (stats.transport_name == cricket::CN_AUDIO) { |
| 4781 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 4782 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 4783 | } else if (stats.transport_name == cricket::CN_VIDEO) { |
| 4784 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 4785 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 4786 | } else if (stats.transport_name == cricket::CN_DATA) { |
| 4787 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 4788 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 4789 | } else { |
| 4790 | RTC_NOTREACHED(); |
| 4791 | return; |
| 4792 | } |
| 4793 | |
| 4794 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 4795 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 4796 | srtp_crypto_suite); |
| 4797 | } |
| 4798 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 4799 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 4800 | ssl_cipher_suite); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 4805 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 4806 | RTC_DCHECK(call_); |
| 4807 | call_->OnSentPacket(sent_packet); |
| 4808 | } |
| 4809 | |
| 4810 | const std::string PeerConnection::GetTransportName( |
| 4811 | const std::string& content_name) { |
| 4812 | cricket::BaseChannel* channel = GetChannel(content_name); |
| 4813 | if (!channel) { |
| 4814 | if (sctp_transport_) { |
| 4815 | RTC_DCHECK(sctp_content_name_); |
| 4816 | RTC_DCHECK(sctp_transport_name_); |
| 4817 | if (content_name == *sctp_content_name_) { |
| 4818 | return *sctp_transport_name_; |
| 4819 | } |
| 4820 | } |
| 4821 | // Return an empty string if failed to retrieve the transport name. |
| 4822 | return ""; |
| 4823 | } |
| 4824 | return channel->transport_name(); |
| 4825 | } |
| 4826 | |
| 4827 | void PeerConnection::DestroyRtcpTransport_n(const std::string& transport_name) { |
| 4828 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4829 | transport_controller_->DestroyDtlsTransport_n( |
| 4830 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4831 | } |
| 4832 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4833 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4834 | void PeerConnection::DestroyVideoChannel(cricket::VideoChannel* video_channel) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4835 | RTC_DCHECK(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4836 | RTC_DCHECK(video_channel->rtp_dtls_transport()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4837 | RTC_DCHECK_EQ(GetVideoTransceiver()->internal()->channel(), video_channel); |
| 4838 | GetVideoTransceiver()->internal()->SetChannel(nullptr); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4839 | const std::string transport_name = |
| 4840 | video_channel->rtp_dtls_transport()->transport_name(); |
| 4841 | const bool need_to_delete_rtcp = |
| 4842 | (video_channel->rtcp_dtls_transport() != nullptr); |
| 4843 | // The above need to be cached before destroying the video channel so that we |
| 4844 | // do not access uninitialized memory. |
| 4845 | channel_manager()->DestroyVideoChannel(video_channel); |
| 4846 | transport_controller_->DestroyDtlsTransport( |
| 4847 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4848 | if (need_to_delete_rtcp) { |
| 4849 | transport_controller_->DestroyDtlsTransport( |
| 4850 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4851 | } |
| 4852 | } |
| 4853 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4854 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4855 | void PeerConnection::DestroyVoiceChannel(cricket::VoiceChannel* voice_channel) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4856 | RTC_DCHECK(voice_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4857 | RTC_DCHECK(voice_channel->rtp_dtls_transport()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4858 | RTC_DCHECK_EQ(GetAudioTransceiver()->internal()->channel(), voice_channel); |
| 4859 | GetAudioTransceiver()->internal()->SetChannel(nullptr); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4860 | const std::string transport_name = |
| 4861 | voice_channel->rtp_dtls_transport()->transport_name(); |
| 4862 | const bool need_to_delete_rtcp = |
| 4863 | (voice_channel->rtcp_dtls_transport() != nullptr); |
| 4864 | // The above need to be cached before destroying the video channel so that we |
| 4865 | // do not access uninitialized memory. |
| 4866 | channel_manager()->DestroyVoiceChannel(voice_channel); |
| 4867 | transport_controller_->DestroyDtlsTransport( |
| 4868 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4869 | if (need_to_delete_rtcp) { |
| 4870 | transport_controller_->DestroyDtlsTransport( |
| 4871 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | void PeerConnection::DestroyDataChannel() { |
| 4876 | OnDataChannelDestroyed(); |
| 4877 | RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport()); |
| 4878 | std::string transport_name; |
| 4879 | transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name(); |
| 4880 | bool need_to_delete_rtcp = |
| 4881 | (rtp_data_channel_->rtcp_dtls_transport() != nullptr); |
| 4882 | channel_manager()->DestroyRtpDataChannel(rtp_data_channel_); |
| 4883 | rtp_data_channel_ = nullptr; |
| 4884 | transport_controller_->DestroyDtlsTransport( |
| 4885 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4886 | if (need_to_delete_rtcp) { |
| 4887 | transport_controller_->DestroyDtlsTransport( |
| 4888 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4889 | } |
| 4890 | } |
| 4891 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4892 | } // namespace webrtc |