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