henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/peerconnection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 14 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 15 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/jsepicecandidate.h" |
| 20 | #include "api/jsepsessiondescription.h" |
| 21 | #include "api/mediaconstraintsinterface.h" |
| 22 | #include "api/mediastreamproxy.h" |
| 23 | #include "api/mediastreamtrackproxy.h" |
| 24 | #include "call/call.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 25 | #include "logging/rtc_event_log/icelogger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 26 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 28 | #include "media/sctp/sctptransport.h" |
| 29 | #include "pc/audiotrack.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 30 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "pc/channelmanager.h" |
| 32 | #include "pc/dtmfsender.h" |
| 33 | #include "pc/mediastream.h" |
| 34 | #include "pc/mediastreamobserver.h" |
| 35 | #include "pc/remoteaudiosource.h" |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 36 | #include "pc/rtpmediautils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "pc/rtpreceiver.h" |
| 38 | #include "pc/rtpsender.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 39 | #include "pc/sctputils.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 40 | #include "pc/sdputils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "pc/streamcollection.h" |
| 42 | #include "pc/videocapturertracksource.h" |
| 43 | #include "pc/videotrack.h" |
| 44 | #include "rtc_base/bind.h" |
| 45 | #include "rtc_base/checks.h" |
| 46 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 47 | #include "rtc_base/numerics/safe_conversions.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 48 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 49 | #include "rtc_base/stringencode.h" |
| 50 | #include "rtc_base/stringutils.h" |
| 51 | #include "rtc_base/trace_event.h" |
| 52 | #include "system_wrappers/include/clock.h" |
| 53 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 55 | using cricket::ContentInfo; |
| 56 | using cricket::ContentInfos; |
| 57 | using cricket::MediaContentDescription; |
| 58 | using cricket::SessionDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 59 | using cricket::MediaProtocolType; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 60 | using cricket::TransportInfo; |
| 61 | |
| 62 | using cricket::LOCAL_PORT_TYPE; |
| 63 | using cricket::STUN_PORT_TYPE; |
| 64 | using cricket::RELAY_PORT_TYPE; |
| 65 | using cricket::PRFLX_PORT_TYPE; |
| 66 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 67 | namespace webrtc { |
| 68 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 69 | // Error messages |
| 70 | const char kBundleWithoutRtcpMux[] = |
| 71 | "rtcp-mux must be enabled when BUNDLE " |
| 72 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 73 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 74 | const char kInvalidSdp[] = "Invalid session description."; |
| 75 | const char kMlineMismatchInAnswer[] = |
| 76 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 77 | "answer."; |
| 78 | const char kMlineMismatchInSubsequentOffer[] = |
| 79 | "The order of m-lines in subsequent offer doesn't match order from " |
| 80 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 81 | const char kSdpWithoutDtlsFingerprint[] = |
| 82 | "Called with SDP without DTLS fingerprint."; |
| 83 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 84 | const char kSdpWithoutIceUfragPwd[] = |
| 85 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 86 | const char kSessionError[] = "Session error code: "; |
| 87 | const char kSessionErrorDesc[] = "Session error description: "; |
| 88 | const char kDtlsSrtpSetupFailureRtp[] = |
| 89 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 90 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 91 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
| 92 | const char kEnableBundleFailed[] = "Failed to enable BUNDLE."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 94 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 96 | static const char kDefaultStreamLabel[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 97 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 98 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 99 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 100 | // The length of RTCP CNAMEs. |
| 101 | static const int kRtcpCnameLength = 16; |
| 102 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 104 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 106 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 108 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | explicit SetSessionDescriptionMsg( |
| 113 | webrtc::SetSessionDescriptionObserver* observer) |
| 114 | : observer(observer) { |
| 115 | } |
| 116 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 117 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | std::string error; |
| 119 | }; |
| 120 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 121 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 122 | explicit CreateSessionDescriptionMsg( |
| 123 | webrtc::CreateSessionDescriptionObserver* observer) |
| 124 | : observer(observer) {} |
| 125 | |
| 126 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 127 | std::string error; |
| 128 | }; |
| 129 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 130 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 131 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 132 | webrtc::MediaStreamTrackInterface* track) |
| 133 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 135 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 136 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 139 | // Check if we can send |new_stream| on a PeerConnection. |
| 140 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 141 | webrtc::MediaStreamInterface* new_stream) { |
| 142 | if (!new_stream || !current_streams) { |
| 143 | return false; |
| 144 | } |
| 145 | if (current_streams->find(new_stream->label()) != nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 146 | RTC_LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 147 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 153 | // If the direction is "recvonly" or "inactive", treat the description |
| 154 | // as containing no streams. |
| 155 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 156 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 157 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 158 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 159 | ? desc->streams() |
| 160 | : std::vector<cricket::StreamParams>(); |
| 161 | } |
| 162 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 163 | bool IsValidOfferToReceiveMedia(int value) { |
| 164 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 165 | return (value >= Options::kUndefined) && |
| 166 | (value <= Options::kMaxOfferToReceiveMedia); |
| 167 | } |
| 168 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 169 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 170 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 171 | const std::vector<rtc::scoped_refptr< |
| 172 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 173 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 174 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 175 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 176 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 177 | if (audio_media_description_options) { |
| 178 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 179 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 180 | } |
| 181 | } else { |
| 182 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 183 | if (video_media_description_options) { |
| 184 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 185 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 188 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 189 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 190 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 191 | // Add options to |session_options| from |rtp_data_channels|. |
| 192 | void AddRtpDataChannelOptions( |
| 193 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 194 | rtp_data_channels, |
| 195 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 196 | if (!data_media_description_options) { |
| 197 | return; |
| 198 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 199 | // Check for data channels. |
| 200 | for (const auto& kv : rtp_data_channels) { |
| 201 | const DataChannel* channel = kv.second; |
| 202 | if (channel->state() == DataChannel::kConnecting || |
| 203 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 204 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 205 | // the data channel's label. |
| 206 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 207 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 212 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 213 | PeerConnectionInterface::IceTransportsType type) { |
| 214 | switch (type) { |
| 215 | case PeerConnectionInterface::kNone: |
| 216 | return cricket::CF_NONE; |
| 217 | case PeerConnectionInterface::kRelay: |
| 218 | return cricket::CF_RELAY; |
| 219 | case PeerConnectionInterface::kNoHost: |
| 220 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 221 | case PeerConnectionInterface::kAll: |
| 222 | return cricket::CF_ALL; |
| 223 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 224 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 225 | } |
| 226 | return cricket::CF_NONE; |
| 227 | } |
| 228 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 229 | // Helper to set an error and return from a method. |
| 230 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 231 | if (error) { |
| 232 | error->set_type(type); |
| 233 | } |
| 234 | return type == webrtc::RTCErrorType::NONE; |
| 235 | } |
| 236 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 237 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 238 | if (error_out) { |
| 239 | *error_out = std::move(error); |
| 240 | } |
| 241 | return error.ok(); |
| 242 | } |
| 243 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 244 | std::string GetSignalingStateString( |
| 245 | PeerConnectionInterface::SignalingState state) { |
| 246 | switch (state) { |
| 247 | case PeerConnectionInterface::kStable: |
| 248 | return "kStable"; |
| 249 | case PeerConnectionInterface::kHaveLocalOffer: |
| 250 | return "kHaveLocalOffer"; |
| 251 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 252 | return "kHavePrAnswer"; |
| 253 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 254 | return "kHaveRemoteOffer"; |
| 255 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 256 | return "kHaveRemotePrAnswer"; |
| 257 | case PeerConnectionInterface::kClosed: |
| 258 | return "kClosed"; |
| 259 | } |
| 260 | RTC_NOTREACHED(); |
| 261 | return ""; |
| 262 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 263 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 264 | IceCandidatePairType GetIceCandidatePairCounter( |
| 265 | const cricket::Candidate& local, |
| 266 | const cricket::Candidate& remote) { |
| 267 | const auto& l = local.type(); |
| 268 | const auto& r = remote.type(); |
| 269 | const auto& host = LOCAL_PORT_TYPE; |
| 270 | const auto& srflx = STUN_PORT_TYPE; |
| 271 | const auto& relay = RELAY_PORT_TYPE; |
| 272 | const auto& prflx = PRFLX_PORT_TYPE; |
| 273 | if (l == host && r == host) { |
| 274 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 275 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 276 | if (local_private) { |
| 277 | if (remote_private) { |
| 278 | return kIceCandidatePairHostPrivateHostPrivate; |
| 279 | } else { |
| 280 | return kIceCandidatePairHostPrivateHostPublic; |
| 281 | } |
| 282 | } else { |
| 283 | if (remote_private) { |
| 284 | return kIceCandidatePairHostPublicHostPrivate; |
| 285 | } else { |
| 286 | return kIceCandidatePairHostPublicHostPublic; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | if (l == host && r == srflx) |
| 291 | return kIceCandidatePairHostSrflx; |
| 292 | if (l == host && r == relay) |
| 293 | return kIceCandidatePairHostRelay; |
| 294 | if (l == host && r == prflx) |
| 295 | return kIceCandidatePairHostPrflx; |
| 296 | if (l == srflx && r == host) |
| 297 | return kIceCandidatePairSrflxHost; |
| 298 | if (l == srflx && r == srflx) |
| 299 | return kIceCandidatePairSrflxSrflx; |
| 300 | if (l == srflx && r == relay) |
| 301 | return kIceCandidatePairSrflxRelay; |
| 302 | if (l == srflx && r == prflx) |
| 303 | return kIceCandidatePairSrflxPrflx; |
| 304 | if (l == relay && r == host) |
| 305 | return kIceCandidatePairRelayHost; |
| 306 | if (l == relay && r == srflx) |
| 307 | return kIceCandidatePairRelaySrflx; |
| 308 | if (l == relay && r == relay) |
| 309 | return kIceCandidatePairRelayRelay; |
| 310 | if (l == relay && r == prflx) |
| 311 | return kIceCandidatePairRelayPrflx; |
| 312 | if (l == prflx && r == host) |
| 313 | return kIceCandidatePairPrflxHost; |
| 314 | if (l == prflx && r == srflx) |
| 315 | return kIceCandidatePairPrflxSrflx; |
| 316 | if (l == prflx && r == relay) |
| 317 | return kIceCandidatePairPrflxRelay; |
| 318 | return kIceCandidatePairMax; |
| 319 | } |
| 320 | |
| 321 | // Verify that the order of media sections in |new_desc| matches |
| 322 | // |existing_desc|. The number of m= sections in |new_desc| should be no less |
| 323 | // than |existing_desc|. |
| 324 | bool MediaSectionsInSameOrder(const SessionDescription* existing_desc, |
| 325 | const SessionDescription* new_desc) { |
| 326 | if (!existing_desc || !new_desc) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | if (existing_desc->contents().size() > new_desc->contents().size()) { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | for (size_t i = 0; i < existing_desc->contents().size(); ++i) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 335 | if (existing_desc->contents()[i].rejected) { |
| 336 | // If the media section can be recycled, it's valid for the MID and media |
| 337 | // type to change. |
| 338 | continue; |
| 339 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 340 | if (new_desc->contents()[i].name != existing_desc->contents()[i].name) { |
| 341 | return false; |
| 342 | } |
| 343 | const MediaContentDescription* new_desc_mdesc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 344 | new_desc->contents()[i].media_description(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 345 | const MediaContentDescription* existing_desc_mdesc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 346 | existing_desc->contents()[i].media_description(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 347 | if (new_desc_mdesc->type() != existing_desc_mdesc->type()) { |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | bool MediaSectionsHaveSameCount(const SessionDescription* desc1, |
| 355 | const SessionDescription* desc2) { |
| 356 | if (!desc1 || !desc2) { |
| 357 | return false; |
| 358 | } |
| 359 | return desc1->contents().size() == desc2->contents().size(); |
| 360 | } |
| 361 | |
| 362 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 363 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 364 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 365 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 366 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 367 | // by Channel's |srtp_required| check. |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 368 | RTCError VerifyCrypto(const SessionDescription* desc, |
| 369 | bool dtls_enabled, |
| 370 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 371 | const cricket::ContentGroup* bundle = |
| 372 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 373 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 374 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 375 | continue; |
| 376 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 377 | const std::string& mid = content_info.name; |
| 378 | if (bundle && bundle->HasContentName(mid) && |
| 379 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 380 | // This isn't the first media section in the BUNDLE group, so it's not |
| 381 | // required to have crypto attributes, since only the crypto attributes |
| 382 | // from the first section actually get used. |
| 383 | continue; |
| 384 | } |
| 385 | |
| 386 | // If the content isn't rejected or bundled into another m= section, crypto |
| 387 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 388 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 389 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 390 | if (!media || !tinfo) { |
| 391 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 392 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 393 | } |
| 394 | if (dtls_enabled) { |
| 395 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 396 | RTC_LOG(LS_WARNING) |
| 397 | << "Session description must have DTLS fingerprint if " |
| 398 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 399 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 400 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 401 | } |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 402 | if (uma_observer) { |
| 403 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 404 | webrtc::kEnumCounterKeyProtocolDtls, |
| 405 | webrtc::kEnumCounterKeyProtocolMax); |
| 406 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 407 | } else { |
| 408 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 409 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 410 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 411 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 412 | } |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 413 | if (uma_observer) { |
| 414 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 415 | webrtc::kEnumCounterKeyProtocolSdes, |
| 416 | webrtc::kEnumCounterKeyProtocolMax); |
| 417 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 420 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 424 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 425 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 426 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 427 | const cricket::ContentGroup* bundle = |
| 428 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 429 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 430 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 431 | continue; |
| 432 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 433 | const std::string& mid = content_info.name; |
| 434 | if (bundle && bundle->HasContentName(mid) && |
| 435 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 436 | // This isn't the first media section in the BUNDLE group, so it's not |
| 437 | // required to have ufrag/password, since only the ufrag/password from |
| 438 | // the first section actually get used. |
| 439 | continue; |
| 440 | } |
| 441 | |
| 442 | // If the content isn't rejected or bundled into another m= section, |
| 443 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 444 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 445 | if (!tinfo) { |
| 446 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 447 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 448 | return false; |
| 449 | } |
| 450 | if (tinfo->description.ice_ufrag.empty() || |
| 451 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 452 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 460 | uint32_t ssrc, |
| 461 | std::string* track_id) { |
| 462 | RTC_DCHECK(track_id != NULL); |
| 463 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 464 | const cricket::AudioContentDescription* audio_desc = |
| 465 | cricket::GetFirstAudioContentDescription(session_description); |
| 466 | if (audio_desc) { |
| 467 | const auto* found = cricket::GetStreamBySsrc(audio_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 468 | if (found) { |
| 469 | *track_id = found->id; |
| 470 | return true; |
| 471 | } |
| 472 | } |
| 473 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 474 | const cricket::VideoContentDescription* video_desc = |
| 475 | cricket::GetFirstVideoContentDescription(session_description); |
| 476 | if (video_desc) { |
| 477 | const auto* found = cricket::GetStreamBySsrc(video_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 478 | if (found) { |
| 479 | *track_id = found->id; |
| 480 | return true; |
| 481 | } |
| 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | // Get the SCTP port out of a SessionDescription. |
| 487 | // Return -1 if not found. |
| 488 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 489 | const cricket::DataContentDescription* data_desc = |
| 490 | GetFirstDataContentDescription(session_description); |
| 491 | RTC_DCHECK(data_desc); |
| 492 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 493 | return -1; |
| 494 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 495 | std::string value; |
| 496 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 497 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 498 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 499 | if (!codec.Matches(match_pattern)) { |
| 500 | continue; |
| 501 | } |
| 502 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 503 | return rtc::FromString<int>(value); |
| 504 | } |
| 505 | } |
| 506 | return -1; |
| 507 | } |
| 508 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 509 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 510 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 511 | const SessionDescriptionInterface* new_desc, |
| 512 | const std::string& content_name) { |
| 513 | if (!old_desc) { |
| 514 | return false; |
| 515 | } |
| 516 | const SessionDescription* new_sd = new_desc->description(); |
| 517 | const SessionDescription* old_sd = old_desc->description(); |
| 518 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 519 | if (!cinfo || cinfo->rejected) { |
| 520 | return false; |
| 521 | } |
| 522 | // If the content isn't rejected, check if ufrag and password has changed. |
| 523 | const cricket::TransportDescription* new_transport_desc = |
| 524 | new_sd->GetTransportDescriptionByName(content_name); |
| 525 | const cricket::TransportDescription* old_transport_desc = |
| 526 | old_sd->GetTransportDescriptionByName(content_name); |
| 527 | if (!new_transport_desc || !old_transport_desc) { |
| 528 | // No transport description exists. This is not an ICE restart. |
| 529 | return false; |
| 530 | } |
| 531 | if (cricket::IceCredentialsChanged( |
| 532 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 533 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 534 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 535 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | } // namespace |
| 542 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 543 | // Upon completion, posts a task to execute the callback of the |
| 544 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 545 | // point, the state of the peer connection might no longer reflect the effects |
| 546 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 547 | // modified during the post. |
| 548 | // TODO(hbos): Remove this class once we remove the version of |
| 549 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 550 | // SetSessionDescriptionObserver as an argument. |
| 551 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 552 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 553 | public: |
| 554 | SetRemoteDescriptionObserverAdapter( |
| 555 | rtc::scoped_refptr<PeerConnection> pc, |
| 556 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 557 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 558 | |
| 559 | // SetRemoteDescriptionObserverInterface implementation. |
| 560 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 561 | if (error.ok()) |
| 562 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 563 | else |
| 564 | pc_->PostSetSessionDescriptionFailure(wrapper_, error.message()); |
| 565 | } |
| 566 | |
| 567 | private: |
| 568 | rtc::scoped_refptr<PeerConnection> pc_; |
| 569 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 570 | }; |
| 571 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 572 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 573 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 574 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 575 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 576 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 577 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 578 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 579 | BundlePolicy bundle_policy; |
| 580 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 581 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 582 | int ice_candidate_pool_size; |
| 583 | bool disable_ipv6; |
| 584 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 585 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame^] | 586 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 587 | bool enable_rtp_data_channel; |
| 588 | rtc::Optional<int> screencast_min_bitrate; |
| 589 | rtc::Optional<bool> combined_audio_video_bwe; |
| 590 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 591 | TcpCandidatePolicy tcp_candidate_policy; |
| 592 | CandidateNetworkPolicy candidate_network_policy; |
| 593 | int audio_jitter_buffer_max_packets; |
| 594 | bool audio_jitter_buffer_fast_accelerate; |
| 595 | int ice_connection_receiving_timeout; |
| 596 | int ice_backup_candidate_pair_ping_interval; |
| 597 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 598 | bool prioritize_most_likely_ice_candidate_pairs; |
| 599 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 600 | bool prune_turn_ports; |
| 601 | bool presume_writable_when_fully_relayed; |
| 602 | bool enable_ice_renomination; |
| 603 | bool redetermine_role_on_ice_restart; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 604 | rtc::Optional<int> ice_check_min_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 605 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 606 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 607 | SdpSemantics sdp_semantics; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 608 | rtc::Optional<rtc::AdapterType> network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 609 | }; |
| 610 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 611 | "Did you add something to RTCConfiguration and forget to " |
| 612 | "update operator==?"); |
| 613 | return type == o.type && servers == o.servers && |
| 614 | bundle_policy == o.bundle_policy && |
| 615 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 616 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 617 | candidate_network_policy == o.candidate_network_policy && |
| 618 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 619 | audio_jitter_buffer_fast_accelerate == |
| 620 | o.audio_jitter_buffer_fast_accelerate && |
| 621 | ice_connection_receiving_timeout == |
| 622 | o.ice_connection_receiving_timeout && |
| 623 | ice_backup_candidate_pair_ping_interval == |
| 624 | o.ice_backup_candidate_pair_ping_interval && |
| 625 | continual_gathering_policy == o.continual_gathering_policy && |
| 626 | certificates == o.certificates && |
| 627 | prioritize_most_likely_ice_candidate_pairs == |
| 628 | o.prioritize_most_likely_ice_candidate_pairs && |
| 629 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 630 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 631 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame^] | 632 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 633 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 634 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 635 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 636 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 637 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 638 | prune_turn_ports == o.prune_turn_ports && |
| 639 | presume_writable_when_fully_relayed == |
| 640 | o.presume_writable_when_fully_relayed && |
| 641 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 642 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 643 | ice_check_min_interval == o.ice_check_min_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 644 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 645 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 646 | sdp_semantics == o.sdp_semantics && |
| 647 | network_preference == o.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 651 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 652 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 653 | } |
| 654 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 655 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 656 | std::string GenerateRtcpCname() { |
| 657 | std::string cname; |
| 658 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 659 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 660 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 661 | } |
| 662 | return cname; |
| 663 | } |
| 664 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 665 | bool ValidateOfferAnswerOptions( |
| 666 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 667 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 668 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 669 | } |
| 670 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 671 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 672 | // m= sections (in other words, nothing that involves a map/array). |
| 673 | void ExtractSharedMediaSessionOptions( |
| 674 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 675 | cricket::MediaSessionOptions* session_options) { |
| 676 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 677 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 678 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 679 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 680 | bool ConvertConstraintsToOfferAnswerOptions( |
| 681 | const MediaConstraintsInterface* constraints, |
| 682 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 683 | if (!constraints) { |
| 684 | return true; |
| 685 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 686 | |
| 687 | bool value = false; |
| 688 | size_t mandatory_constraints_satisfied = 0; |
| 689 | |
| 690 | if (FindConstraint(constraints, |
| 691 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 692 | &mandatory_constraints_satisfied)) { |
| 693 | offer_answer_options->offer_to_receive_audio = |
| 694 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 695 | kOfferToReceiveMediaTrue |
| 696 | : 0; |
| 697 | } |
| 698 | |
| 699 | if (FindConstraint(constraints, |
| 700 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 701 | &mandatory_constraints_satisfied)) { |
| 702 | offer_answer_options->offer_to_receive_video = |
| 703 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 704 | kOfferToReceiveMediaTrue |
| 705 | : 0; |
| 706 | } |
| 707 | if (FindConstraint(constraints, |
| 708 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 709 | &mandatory_constraints_satisfied)) { |
| 710 | offer_answer_options->voice_activity_detection = value; |
| 711 | } |
| 712 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 713 | &mandatory_constraints_satisfied)) { |
| 714 | offer_answer_options->use_rtp_mux = value; |
| 715 | } |
| 716 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 717 | &value, &mandatory_constraints_satisfied)) { |
| 718 | offer_answer_options->ice_restart = value; |
| 719 | } |
| 720 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 721 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 722 | } |
| 723 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 724 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 725 | std::unique_ptr<RtcEventLog> event_log, |
| 726 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 727 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 728 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 729 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 730 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 731 | remote_streams_(StreamCollection::Create()), |
| 732 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 733 | |
| 734 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 735 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 736 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 737 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 738 | // Need to stop transceivers before destroying the stats collector because |
| 739 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 740 | // stopping. |
| 741 | for (auto transceiver : transceivers_) { |
| 742 | transceiver->Stop(); |
| 743 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 744 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 745 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 746 | if (stats_collector_) { |
| 747 | stats_collector_->WaitForPendingRequest(); |
| 748 | stats_collector_ = nullptr; |
| 749 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 750 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 751 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 752 | // the last stats request can still read from the channels. |
| 753 | DestroyAllChannels(); |
| 754 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 755 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 756 | |
| 757 | webrtc_session_desc_factory_.reset(); |
| 758 | sctp_invoker_.reset(); |
| 759 | sctp_factory_.reset(); |
| 760 | transport_controller_.reset(); |
| 761 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 762 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 763 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 764 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 765 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 766 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 767 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 768 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 769 | event_log_.reset(); |
| 770 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 773 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 774 | // Destroy video channels first since they may have a pointer to a voice |
| 775 | // channel. |
| 776 | for (auto transceiver : transceivers_) { |
| 777 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 778 | DestroyTransceiverChannel(transceiver); |
| 779 | } |
| 780 | } |
| 781 | for (auto transceiver : transceivers_) { |
| 782 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 783 | DestroyTransceiverChannel(transceiver); |
| 784 | } |
| 785 | } |
| 786 | DestroyDataChannel(); |
| 787 | } |
| 788 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 789 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 790 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 791 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 792 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 793 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 794 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 795 | |
| 796 | RTCError config_error = ValidateConfiguration(configuration); |
| 797 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 798 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 799 | return false; |
| 800 | } |
| 801 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 802 | if (!allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 803 | RTC_LOG(LS_ERROR) |
| 804 | << "PeerConnection initialized without a PortAllocator? " |
| 805 | << "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 806 | return false; |
| 807 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 808 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 809 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 810 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 811 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
| 812 | << "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 813 | return false; |
| 814 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 815 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 816 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 817 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 818 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 819 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 820 | if (!network_thread()->Invoke<bool>( |
| 821 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 822 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 823 | return false; |
| 824 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 825 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 826 | // RFC 3264: The numeric value of the session id and version in the |
| 827 | // o line MUST be representable with a "64 bit signed integer". |
| 828 | // Due to this constraint session id |session_id_| is max limited to |
| 829 | // LLONG_MAX. |
| 830 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
| 831 | transport_controller_.reset(factory_->CreateTransportController( |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 832 | port_allocator_.get(), configuration.redetermine_role_on_ice_restart, |
| 833 | event_log_.get())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 834 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 835 | transport_controller_->SignalConnectionState.connect( |
| 836 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 837 | transport_controller_->SignalGatheringState.connect( |
| 838 | this, &PeerConnection::OnTransportControllerGatheringState); |
| 839 | transport_controller_->SignalCandidatesGathered.connect( |
| 840 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
| 841 | transport_controller_->SignalCandidatesRemoved.connect( |
| 842 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 843 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 844 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 845 | |
| 846 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 847 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 848 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 849 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 850 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 851 | configuration_ = configuration; |
| 852 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 853 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 854 | |
| 855 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
| 856 | |
| 857 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 858 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 859 | if (!configuration.certificates.empty()) { |
| 860 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 861 | // just picking the first one. The decision should be made based on the DTLS |
| 862 | // handshake. The DTLS negotiations need to know about all certificates. |
| 863 | certificate = configuration.certificates[0]; |
| 864 | } |
| 865 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 866 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 867 | |
| 868 | if (options.disable_encryption) { |
| 869 | dtls_enabled_ = false; |
| 870 | } else { |
| 871 | // Enable DTLS by default if we have an identity store or a certificate. |
| 872 | dtls_enabled_ = (cert_generator || certificate); |
| 873 | // |configuration| can override the default |dtls_enabled_| value. |
| 874 | if (configuration.enable_dtls_srtp) { |
| 875 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 880 | // It takes precendence over the disable_sctp_data_channels |
| 881 | // PeerConnectionFactoryInterface::Options. |
| 882 | if (configuration.enable_rtp_data_channel) { |
| 883 | data_channel_type_ = cricket::DCT_RTP; |
| 884 | } else { |
| 885 | // DTLS has to be enabled to use SCTP. |
| 886 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 887 | data_channel_type_ = cricket::DCT_SCTP; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | video_options_.screencast_min_bitrate_kbps = |
| 892 | configuration.screencast_min_bitrate; |
| 893 | audio_options_.combined_audio_video_bwe = |
| 894 | configuration.combined_audio_video_bwe; |
| 895 | |
| 896 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 897 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 898 | |
| 899 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 900 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 901 | |
| 902 | // Whether the certificate generator/certificate is null or not determines |
| 903 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 904 | // the right instructions by clearing the variables if needed. |
| 905 | if (!dtls_enabled_) { |
| 906 | cert_generator.reset(); |
| 907 | certificate = nullptr; |
| 908 | } else if (certificate) { |
| 909 | // Favor generated certificate over the certificate generator. |
| 910 | cert_generator.reset(); |
| 911 | } |
| 912 | |
| 913 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 914 | signaling_thread(), channel_manager(), this, session_id(), |
| 915 | std::move(cert_generator), certificate)); |
| 916 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 917 | this, &PeerConnection::OnCertificateReady); |
| 918 | |
| 919 | if (options.disable_encryption) { |
| 920 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 921 | } |
| 922 | |
| 923 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 924 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 925 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 926 | // Add default audio/video transceivers for Plan B SDP. |
| 927 | if (!IsUnifiedPlan()) { |
| 928 | transceivers_.push_back( |
| 929 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 930 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 931 | transceivers_.push_back( |
| 932 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 933 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 934 | } |
| 935 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 936 | return true; |
| 937 | } |
| 938 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 939 | RTCError PeerConnection::ValidateConfiguration( |
| 940 | const RTCConfiguration& config) const { |
| 941 | if (config.ice_regather_interval_range && |
| 942 | config.continual_gathering_policy == GATHER_ONCE) { |
| 943 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 944 | "ice_regather_interval_range specified but continual " |
| 945 | "gathering policy is GATHER_ONCE"); |
| 946 | } |
| 947 | return RTCError::OK(); |
| 948 | } |
| 949 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 950 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 951 | PeerConnection::local_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 952 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 953 | } |
| 954 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 955 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 956 | PeerConnection::remote_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 957 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 958 | } |
| 959 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 960 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 961 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 962 | if (IsClosed()) { |
| 963 | return false; |
| 964 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 965 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 966 | return false; |
| 967 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 968 | |
| 969 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 970 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 971 | observer->SignalAudioTrackAdded.connect(this, |
| 972 | &PeerConnection::OnAudioTrackAdded); |
| 973 | observer->SignalAudioTrackRemoved.connect( |
| 974 | this, &PeerConnection::OnAudioTrackRemoved); |
| 975 | observer->SignalVideoTrackAdded.connect(this, |
| 976 | &PeerConnection::OnVideoTrackAdded); |
| 977 | observer->SignalVideoTrackRemoved.connect( |
| 978 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 979 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 980 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 981 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 982 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 983 | } |
| 984 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 985 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 986 | } |
| 987 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 988 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 989 | observer_->OnRenegotiationNeeded(); |
| 990 | return true; |
| 991 | } |
| 992 | |
| 993 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 994 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 995 | if (!IsClosed()) { |
| 996 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 997 | RemoveAudioTrack(track.get(), local_stream); |
| 998 | } |
| 999 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1000 | RemoveVideoTrack(track.get(), local_stream); |
| 1001 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1002 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1003 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1004 | stream_observers_.erase( |
| 1005 | std::remove_if( |
| 1006 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1007 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1008 | return observer->stream()->label().compare(local_stream->label()) == |
| 1009 | 0; |
| 1010 | }), |
| 1011 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1012 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1013 | if (IsClosed()) { |
| 1014 | return; |
| 1015 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1016 | observer_->OnRenegotiationNeeded(); |
| 1017 | } |
| 1018 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1019 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1020 | MediaStreamTrackInterface* track, |
| 1021 | std::vector<MediaStreamInterface*> streams) { |
| 1022 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1023 | std::vector<std::string> stream_labels; |
| 1024 | for (auto* stream : streams) { |
| 1025 | if (!stream) { |
| 1026 | RTC_LOG(LS_ERROR) << "Stream list has null element."; |
| 1027 | return nullptr; |
| 1028 | } |
| 1029 | stream_labels.push_back(stream->label()); |
| 1030 | } |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1031 | auto sender_or_error = AddTrack(track, stream_labels); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1032 | if (!sender_or_error.ok()) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1033 | return nullptr; |
| 1034 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1035 | return sender_or_error.MoveValue(); |
| 1036 | } |
| 1037 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1038 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1039 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1040 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1041 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1042 | if (!track) { |
| 1043 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1044 | } |
| 1045 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1046 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1047 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1048 | "Track has invalid kind: " + track->kind()); |
| 1049 | } |
| 1050 | // TODO(bugs.webrtc.org/7932): Support adding a track to multiple streams. |
| 1051 | if (stream_labels.size() > 1u) { |
| 1052 | LOG_AND_RETURN_ERROR( |
| 1053 | RTCErrorType::UNSUPPORTED_OPERATION, |
| 1054 | "AddTrack with more than one stream is not currently supported."); |
| 1055 | } |
| 1056 | if (IsClosed()) { |
| 1057 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1058 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1059 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1060 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1061 | LOG_AND_RETURN_ERROR( |
| 1062 | RTCErrorType::INVALID_PARAMETER, |
| 1063 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1064 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1065 | // TODO(bugs.webrtc.org/7933): MediaSession expects the sender to have exactly |
| 1066 | // one stream. AddTrackInternal will return an error if there is more than one |
| 1067 | // stream, but if the caller specifies none then we need to generate a random |
| 1068 | // stream label. |
| 1069 | std::vector<std::string> adjusted_stream_labels = stream_labels; |
| 1070 | if (stream_labels.empty()) { |
| 1071 | adjusted_stream_labels.push_back(rtc::CreateRandomUuid()); |
| 1072 | } |
| 1073 | RTC_DCHECK_EQ(1, adjusted_stream_labels.size()); |
| 1074 | auto sender_or_error = |
| 1075 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, adjusted_stream_labels) |
| 1076 | : AddTrackPlanB(track, adjusted_stream_labels)); |
| 1077 | if (sender_or_error.ok()) { |
| 1078 | observer_->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1079 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1080 | } |
| 1081 | return sender_or_error; |
| 1082 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1083 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1084 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1085 | PeerConnection::AddTrackPlanB( |
| 1086 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1087 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1088 | cricket::MediaType media_type = |
| 1089 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1090 | ? cricket::MEDIA_TYPE_AUDIO |
| 1091 | : cricket::MEDIA_TYPE_VIDEO); |
| 1092 | auto new_sender = CreateSender(media_type, track, stream_labels); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1093 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1094 | static_cast<AudioRtpSender*>(new_sender->internal()) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1095 | ->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1096 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1097 | const RtpSenderInfo* sender_info = |
| 1098 | FindSenderInfo(local_audio_sender_infos_, |
| 1099 | new_sender->internal()->stream_id(), track->id()); |
| 1100 | if (sender_info) { |
| 1101 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1102 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1103 | } else { |
| 1104 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1105 | static_cast<VideoRtpSender*>(new_sender->internal()) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1106 | ->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1107 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1108 | const RtpSenderInfo* sender_info = |
| 1109 | FindSenderInfo(local_video_sender_infos_, |
| 1110 | new_sender->internal()->stream_id(), track->id()); |
| 1111 | if (sender_info) { |
| 1112 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1113 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1114 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1115 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1116 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1117 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1118 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1119 | PeerConnection::AddTrackUnifiedPlan( |
| 1120 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1121 | const std::vector<std::string>& stream_labels) { |
| 1122 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1123 | if (transceiver) { |
| 1124 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
| 1125 | transceiver->SetDirection(RtpTransceiverDirection::kSendRecv); |
| 1126 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
| 1127 | transceiver->SetDirection(RtpTransceiverDirection::kSendOnly); |
| 1128 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1129 | transceiver->sender()->SetTrack(track); |
| 1130 | transceiver->internal()->sender_internal()->set_stream_ids(stream_labels); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1131 | } else { |
| 1132 | cricket::MediaType media_type = |
| 1133 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1134 | ? cricket::MEDIA_TYPE_AUDIO |
| 1135 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1136 | auto sender = CreateSender(media_type, track, stream_labels); |
| 1137 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1138 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1139 | transceiver->internal()->set_created_by_addtrack(true); |
| 1140 | transceiver->SetDirection(RtpTransceiverDirection::kSendRecv); |
| 1141 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1142 | return transceiver->sender(); |
| 1143 | } |
| 1144 | |
| 1145 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1146 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1147 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1148 | RTC_DCHECK(track); |
| 1149 | for (auto transceiver : transceivers_) { |
| 1150 | if (!transceiver->sender()->track() && |
| 1151 | cricket::MediaTypeToString(transceiver->internal()->media_type()) == |
| 1152 | track->kind() && |
| 1153 | !transceiver->internal()->has_ever_been_used_to_send()) { |
| 1154 | return transceiver; |
| 1155 | } |
| 1156 | } |
| 1157 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1161 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1162 | return RemoveTrackInternal(sender).ok(); |
| 1163 | } |
| 1164 | |
| 1165 | RTCError PeerConnection::RemoveTrackInternal( |
| 1166 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1167 | if (!sender) { |
| 1168 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1169 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1170 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1171 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1172 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1173 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1174 | if (IsUnifiedPlan()) { |
| 1175 | auto transceiver = FindTransceiverBySender(sender); |
| 1176 | if (!transceiver || !sender->track()) { |
| 1177 | return RTCError::OK(); |
| 1178 | } |
| 1179 | sender->SetTrack(nullptr); |
| 1180 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
| 1181 | transceiver->internal()->SetDirection(RtpTransceiverDirection::kRecvOnly); |
| 1182 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
| 1183 | transceiver->internal()->SetDirection(RtpTransceiverDirection::kInactive); |
| 1184 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1185 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1186 | bool removed; |
| 1187 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1188 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1189 | } else { |
| 1190 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1191 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1192 | } |
| 1193 | if (!removed) { |
| 1194 | LOG_AND_RETURN_ERROR( |
| 1195 | RTCErrorType::INVALID_PARAMETER, |
| 1196 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1197 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1198 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1199 | observer_->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1200 | return RTCError::OK(); |
| 1201 | } |
| 1202 | |
| 1203 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1204 | PeerConnection::FindTransceiverBySender( |
| 1205 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1206 | for (auto transceiver : transceivers_) { |
| 1207 | if (transceiver->sender() == sender) { |
| 1208 | return transceiver; |
| 1209 | } |
| 1210 | } |
| 1211 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1214 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1215 | PeerConnection::AddTransceiver( |
| 1216 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1217 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1218 | } |
| 1219 | |
| 1220 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1221 | PeerConnection::AddTransceiver( |
| 1222 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1223 | const RtpTransceiverInit& init) { |
| 1224 | if (!IsUnifiedPlan()) { |
| 1225 | LOG_AND_RETURN_ERROR( |
| 1226 | RTCErrorType::INTERNAL_ERROR, |
| 1227 | "AddTransceiver only supported when Unified Plan is enabled."); |
| 1228 | } |
| 1229 | if (!track) { |
| 1230 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1231 | } |
| 1232 | cricket::MediaType media_type; |
| 1233 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1234 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1235 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1236 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1237 | } else { |
| 1238 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1239 | "Track kind is not audio or video"); |
| 1240 | } |
| 1241 | return AddTransceiver(media_type, track, init); |
| 1242 | } |
| 1243 | |
| 1244 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1245 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1246 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1247 | } |
| 1248 | |
| 1249 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1250 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1251 | const RtpTransceiverInit& init) { |
| 1252 | if (!IsUnifiedPlan()) { |
| 1253 | LOG_AND_RETURN_ERROR( |
| 1254 | RTCErrorType::INTERNAL_ERROR, |
| 1255 | "AddTransceiver only supported when Unified Plan is enabled."); |
| 1256 | } |
| 1257 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1258 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1259 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1260 | "media type is not audio or video"); |
| 1261 | } |
| 1262 | return AddTransceiver(media_type, nullptr, init); |
| 1263 | } |
| 1264 | |
| 1265 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1266 | PeerConnection::AddTransceiver( |
| 1267 | cricket::MediaType media_type, |
| 1268 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1269 | const RtpTransceiverInit& init, |
| 1270 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1271 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1272 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1273 | if (track) { |
| 1274 | RTC_DCHECK_EQ(media_type, |
| 1275 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1276 | ? cricket::MEDIA_TYPE_AUDIO |
| 1277 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1278 | } |
| 1279 | |
| 1280 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1281 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1282 | if (init.stream_labels.size() > 1u) { |
| 1283 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1284 | "More than one stream is not yet supported."); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1287 | std::vector<std::string> stream_labels = {!init.stream_labels.empty() |
| 1288 | ? init.stream_labels[0] |
| 1289 | : rtc::CreateRandomUuid()}; |
| 1290 | |
| 1291 | auto sender = CreateSender(media_type, track, stream_labels); |
| 1292 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1293 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1294 | transceiver->internal()->set_direction(init.direction); |
| 1295 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1296 | if (fire_callback) { |
| 1297 | observer_->OnRenegotiationNeeded(); |
| 1298 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1299 | |
| 1300 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1301 | } |
| 1302 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1303 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1304 | PeerConnection::CreateSender( |
| 1305 | cricket::MediaType media_type, |
| 1306 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1307 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1308 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1309 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1310 | RTC_DCHECK(!track || |
| 1311 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1312 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1313 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1314 | new AudioRtpSender(worker_thread(), |
| 1315 | static_cast<AudioTrackInterface*>(track.get()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1316 | stream_labels, stats_.get())); |
| 1317 | } else { |
| 1318 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1319 | RTC_DCHECK(!track || |
| 1320 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1321 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1322 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1323 | new VideoRtpSender(worker_thread(), |
| 1324 | static_cast<VideoTrackInterface*>(track.get()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1325 | stream_labels)); |
| 1326 | } |
| 1327 | sender->internal()->set_stream_ids(stream_labels); |
| 1328 | return sender; |
| 1329 | } |
| 1330 | |
| 1331 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1332 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1333 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1334 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1335 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1336 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1337 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 1338 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1339 | new AudioRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1340 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1341 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1342 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 1343 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1344 | new VideoRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1345 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1346 | return receiver; |
| 1347 | } |
| 1348 | |
| 1349 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1350 | PeerConnection::CreateAndAddTransceiver( |
| 1351 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1352 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1353 | receiver) { |
| 1354 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1355 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1356 | transceivers_.push_back(transceiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1357 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1360 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1361 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1362 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1363 | if (IsClosed()) { |
| 1364 | return nullptr; |
| 1365 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1366 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1367 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1368 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1369 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1370 | auto track_sender = FindSenderForTrack(track); |
| 1371 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1372 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1373 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1376 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1379 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1380 | const std::string& kind, |
| 1381 | const std::string& stream_id) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1382 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1383 | if (IsClosed()) { |
| 1384 | return nullptr; |
| 1385 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1386 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1387 | std::vector<std::string> stream_labels; |
| 1388 | if (!stream_id.empty()) { |
| 1389 | stream_labels.push_back(stream_id); |
| 1390 | } |
| 1391 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1392 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1393 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1394 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1395 | auto* audio_sender = new AudioRtpSender(worker_thread(), nullptr, |
| 1396 | stream_labels, stats_.get()); |
| 1397 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1398 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1399 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1400 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1401 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1402 | auto* video_sender = |
| 1403 | new VideoRtpSender(worker_thread(), nullptr, stream_labels); |
| 1404 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1405 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1406 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1407 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1408 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1409 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1410 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1411 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1412 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1413 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1416 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1417 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1418 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1419 | for (auto sender : GetSendersInternal()) { |
| 1420 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1421 | } |
| 1422 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1425 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1426 | PeerConnection::GetSendersInternal() const { |
| 1427 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1428 | all_senders; |
| 1429 | for (auto transceiver : transceivers_) { |
| 1430 | auto senders = transceiver->internal()->senders(); |
| 1431 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1432 | } |
| 1433 | return all_senders; |
| 1434 | } |
| 1435 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1436 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1437 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1438 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1439 | for (const auto& receiver : GetReceiversInternal()) { |
| 1440 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1441 | } |
| 1442 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1445 | std::vector< |
| 1446 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1447 | PeerConnection::GetReceiversInternal() const { |
| 1448 | std::vector< |
| 1449 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1450 | all_receivers; |
| 1451 | for (auto transceiver : transceivers_) { |
| 1452 | auto receivers = transceiver->internal()->receivers(); |
| 1453 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1454 | receivers.end()); |
| 1455 | } |
| 1456 | return all_receivers; |
| 1457 | } |
| 1458 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1459 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1460 | PeerConnection::GetTransceivers() const { |
| 1461 | RTC_DCHECK(IsUnifiedPlan()); |
| 1462 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1463 | for (auto transceiver : transceivers_) { |
| 1464 | all_transceivers.push_back(transceiver); |
| 1465 | } |
| 1466 | return all_transceivers; |
| 1467 | } |
| 1468 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1469 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1470 | MediaStreamTrackInterface* track, |
| 1471 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1472 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1473 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
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) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1476 | return false; |
| 1477 | } |
| 1478 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1479 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1480 | // The StatsCollector is used to tell if a track is valid because it may |
| 1481 | // remember tracks that the PeerConnection previously removed. |
| 1482 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1483 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1484 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1485 | return false; |
| 1486 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1487 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1488 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1489 | return true; |
| 1490 | } |
| 1491 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1492 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
| 1493 | RTC_DCHECK(stats_collector_); |
| 1494 | stats_collector_->GetStatsReport(callback); |
| 1495 | } |
| 1496 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1497 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1498 | return signaling_state_; |
| 1499 | } |
| 1500 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1501 | PeerConnectionInterface::IceConnectionState |
| 1502 | PeerConnection::ice_connection_state() { |
| 1503 | return ice_connection_state_; |
| 1504 | } |
| 1505 | |
| 1506 | PeerConnectionInterface::IceGatheringState |
| 1507 | PeerConnection::ice_gathering_state() { |
| 1508 | return ice_gathering_state_; |
| 1509 | } |
| 1510 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1511 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1512 | PeerConnection::CreateDataChannel( |
| 1513 | const std::string& label, |
| 1514 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1515 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1516 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1517 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1518 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1519 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1520 | if (config) { |
| 1521 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1522 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1523 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1524 | InternalCreateDataChannel(label, internal_config.get())); |
| 1525 | if (!channel.get()) { |
| 1526 | return nullptr; |
| 1527 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1528 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1529 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1530 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1531 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1532 | observer_->OnRenegotiationNeeded(); |
| 1533 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1534 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1535 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1536 | } |
| 1537 | |
| 1538 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1539 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1540 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1541 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1542 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1543 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1544 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1545 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1546 | // |constraints| and some downstream applications might create offers with |
| 1547 | // mandatory fields which would not be parsed in the helper method. For |
| 1548 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1549 | // |constraints| as a mandatory field but it is not parsed. |
| 1550 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1551 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1552 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1556 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1557 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1558 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1559 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1560 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1561 | return; |
| 1562 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1563 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1564 | if (IsClosed()) { |
| 1565 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1566 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1567 | PostCreateSessionDescriptionFailure(observer, error); |
| 1568 | return; |
| 1569 | } |
| 1570 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1571 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1572 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1573 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1574 | PostCreateSessionDescriptionFailure(observer, error); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1578 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1579 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1580 | if (IsUnifiedPlan()) { |
| 1581 | RTCError error = HandleLegacyOfferOptions(options); |
| 1582 | if (!error.ok()) { |
| 1583 | PostCreateSessionDescriptionFailure(observer, error.message()); |
| 1584 | return; |
| 1585 | } |
| 1586 | } |
| 1587 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1588 | cricket::MediaSessionOptions session_options; |
| 1589 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1590 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1593 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1594 | const RTCOfferAnswerOptions& options) { |
| 1595 | RTC_DCHECK(IsUnifiedPlan()); |
| 1596 | |
| 1597 | if (options.offer_to_receive_audio == 0) { |
| 1598 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1599 | cricket::MEDIA_TYPE_AUDIO); |
| 1600 | } else if (options.offer_to_receive_audio == 1) { |
| 1601 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1602 | } else if (options.offer_to_receive_audio > 1) { |
| 1603 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1604 | "offer_to_receive_audio > 1 is not supported."); |
| 1605 | } |
| 1606 | |
| 1607 | if (options.offer_to_receive_video == 0) { |
| 1608 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1609 | cricket::MEDIA_TYPE_VIDEO); |
| 1610 | } else if (options.offer_to_receive_video == 1) { |
| 1611 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1612 | } else if (options.offer_to_receive_video > 1) { |
| 1613 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1614 | "offer_to_receive_video > 1 is not supported."); |
| 1615 | } |
| 1616 | |
| 1617 | return RTCError::OK(); |
| 1618 | } |
| 1619 | |
| 1620 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1621 | cricket::MediaType media_type) { |
| 1622 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
| 1623 | transceiver->internal()->set_direction( |
| 1624 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false)); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1629 | cricket::MediaType media_type) { |
| 1630 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
| 1631 | RtpTransceiverInit init; |
| 1632 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1633 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1638 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1639 | std::vector< |
| 1640 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1641 | receiving_transceivers; |
| 1642 | for (auto transceiver : transceivers_) { |
| 1643 | if (!transceiver->stopped() && |
| 1644 | transceiver->internal()->media_type() == media_type && |
| 1645 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1646 | receiving_transceivers.push_back(transceiver); |
| 1647 | } |
| 1648 | } |
| 1649 | return receiving_transceivers; |
| 1650 | } |
| 1651 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1652 | void PeerConnection::CreateAnswer( |
| 1653 | CreateSessionDescriptionObserver* observer, |
| 1654 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1655 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1656 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1657 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1658 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1659 | return; |
| 1660 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1661 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1662 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1663 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1664 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1665 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1666 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1667 | PostCreateSessionDescriptionFailure(observer, error); |
| 1668 | return; |
| 1669 | } |
| 1670 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1671 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1675 | const RTCOfferAnswerOptions& options) { |
| 1676 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1677 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1678 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1679 | return; |
| 1680 | } |
| 1681 | |
Oleh Prypin | c22d6a8 | 2018-02-02 08:42:18 +0000 | [diff] [blame] | 1682 | if (IsClosed()) { |
| 1683 | std::string error = "CreateAnswer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1684 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1685 | PostCreateSessionDescriptionFailure(observer, error); |
| 1686 | return; |
| 1687 | } |
| 1688 | |
Oleh Prypin | c22d6a8 | 2018-02-02 08:42:18 +0000 | [diff] [blame] | 1689 | if (remote_description() && |
| 1690 | remote_description()->GetType() != SdpType::kOffer) { |
| 1691 | std::string error = "CreateAnswer called without remote offer."; |
| 1692 | RTC_LOG(LS_ERROR) << error; |
| 1693 | PostCreateSessionDescriptionFailure(observer, error); |
| 1694 | return; |
| 1695 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1696 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1697 | if (IsUnifiedPlan()) { |
| 1698 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1699 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1700 | "supported with Unified Plan semantics. Use the " |
| 1701 | "RtpTransceiver API instead."; |
| 1702 | } |
| 1703 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1704 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1705 | "supported with Unified Plan semantics. Use the " |
| 1706 | "RtpTransceiver API instead."; |
| 1707 | } |
| 1708 | } |
| 1709 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1710 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1711 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1712 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1713 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | void PeerConnection::SetLocalDescription( |
| 1717 | SetSessionDescriptionObserver* observer, |
| 1718 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1719 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1720 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1721 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1722 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1723 | return; |
| 1724 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1725 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1726 | if (!desc) { |
| 1727 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1728 | return; |
| 1729 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1730 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1731 | SdpType type = desc->GetType(); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1732 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1733 | RTCError error = ApplyLocalDescription(rtc::WrapUnique(desc)); |
| 1734 | // |desc| may be destroyed at this point. |
| 1735 | |
| 1736 | if (!error.ok()) { |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1737 | std::ostringstream oss; |
| 1738 | oss << "Failed to set local " << SdpTypeToString(type) |
| 1739 | << " sdp: " << error.message(); |
| 1740 | std::string error_message = oss.str(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1741 | RTC_LOG(LS_ERROR) << error_message << " (" << error.type() << ")"; |
| 1742 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1743 | return; |
| 1744 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1745 | RTC_DCHECK(local_description()); |
| 1746 | |
| 1747 | PostSetSessionDescriptionSuccess(observer); |
| 1748 | |
| 1749 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1750 | // size is not allowed, and changing the set of ICE servers will not result |
| 1751 | // in new candidates being gathered. |
| 1752 | port_allocator_->FreezeCandidatePool(); |
| 1753 | |
| 1754 | // MaybeStartGathering needs to be called after posting |
| 1755 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1756 | // before signaling that SetLocalDescription completed. |
| 1757 | transport_controller_->MaybeStartGathering(); |
| 1758 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1759 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1760 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1761 | // MaybeStartGathering... |
| 1762 | network_thread()->Invoke<void>( |
| 1763 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1764 | port_allocator_.get())); |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | RTCError PeerConnection::ApplyLocalDescription( |
| 1769 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1770 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1771 | RTC_DCHECK(desc); |
| 1772 | |
| 1773 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1774 | if (!error.ok()) { |
| 1775 | return error; |
| 1776 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1777 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1778 | // Update stats here so that we have the most recent stats for tracks and |
| 1779 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1780 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1781 | |
| 1782 | // Update the initial_offerer flag if this session is the initial_offerer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1783 | SdpType type = desc->GetType(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1784 | if (!initial_offerer_.has_value()) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1785 | initial_offerer_.emplace(type == SdpType::kOffer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1786 | if (*initial_offerer_) { |
| 1787 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1788 | } else { |
| 1789 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1790 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1791 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1792 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1793 | // Take a reference to the old local description since it's used below to |
| 1794 | // compare against the new local description. When setting the new local |
| 1795 | // description, grab ownership of the replaced session description in case it |
| 1796 | // is the same as |old_local_description|, to keep it alive for the duration |
| 1797 | // of the method. |
| 1798 | const SessionDescriptionInterface* old_local_description = |
| 1799 | local_description(); |
| 1800 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1801 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1802 | replaced_local_description = pending_local_description_ |
| 1803 | ? std::move(pending_local_description_) |
| 1804 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1805 | current_local_description_ = std::move(desc); |
| 1806 | pending_local_description_ = nullptr; |
| 1807 | current_remote_description_ = std::move(pending_remote_description_); |
| 1808 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1809 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1810 | pending_local_description_ = std::move(desc); |
| 1811 | } |
| 1812 | // The session description to apply now must be accessed by |
| 1813 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1814 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1815 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1816 | if (IsUnifiedPlan()) { |
| 1817 | RTCError error = UpdateTransceiversAndDataChannels( |
| 1818 | cricket::CS_LOCAL, old_local_description, *local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1819 | if (!error.ok()) { |
| 1820 | return error; |
| 1821 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1822 | for (auto transceiver : transceivers_) { |
| 1823 | const ContentInfo* content = |
| 1824 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1825 | if (!content) { |
| 1826 | continue; |
| 1827 | } |
| 1828 | const MediaContentDescription* media_desc = content->media_description(); |
| 1829 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 1830 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 1831 | } |
| 1832 | if (content->rejected && !transceiver->stopped()) { |
| 1833 | transceiver->Stop(); |
| 1834 | } |
| 1835 | } |
| 1836 | } else { |
| 1837 | // Transport and Media channels will be created only when offer is set. |
| 1838 | if (type == SdpType::kOffer) { |
| 1839 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 1840 | // description is applied. Restore back to old description. |
| 1841 | RTCError error = CreateChannels(*local_description()->description()); |
| 1842 | if (!error.ok()) { |
| 1843 | return error; |
| 1844 | } |
| 1845 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1846 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1847 | // Remove unused channels if MediaContentDescription is rejected. |
| 1848 | RemoveUnusedChannels(local_description()->description()); |
| 1849 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1850 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1851 | error = UpdateSessionState(type, cricket::CS_LOCAL); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1852 | if (!error.ok()) { |
| 1853 | return error; |
| 1854 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1855 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1856 | if (remote_description()) { |
| 1857 | // Now that we have a local description, we can push down remote candidates. |
| 1858 | UseCandidatesInSessionDescription(remote_description()); |
| 1859 | } |
| 1860 | |
| 1861 | pending_ice_restarts_.clear(); |
| 1862 | if (session_error() != SessionError::kNone) { |
| 1863 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 1864 | } |
| 1865 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1866 | // If setting the description decided our SSL role, allocate any necessary |
| 1867 | // SCTP sids. |
| 1868 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1869 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1870 | AllocateSctpSids(role); |
| 1871 | } |
| 1872 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1873 | if (IsUnifiedPlan()) { |
| 1874 | for (auto transceiver : transceivers_) { |
| 1875 | const ContentInfo* content = |
| 1876 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1877 | if (!content) { |
| 1878 | continue; |
| 1879 | } |
| 1880 | if (content->rejected && !transceiver->stopped()) { |
| 1881 | transceiver->Stop(); |
| 1882 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1883 | const auto& streams = content->media_description()->streams(); |
| 1884 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1885 | transceiver->internal()->sender_internal()->set_stream_ids( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1886 | {streams[0].sync_label}); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1887 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1888 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1889 | } |
| 1890 | } |
| 1891 | } else { |
| 1892 | // Plan B semantics. |
| 1893 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1894 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 1895 | // local session description. |
| 1896 | const cricket::ContentInfo* audio_content = |
| 1897 | GetFirstAudioContent(local_description()->description()); |
| 1898 | if (audio_content) { |
| 1899 | if (audio_content->rejected) { |
| 1900 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 1901 | } else { |
| 1902 | const cricket::AudioContentDescription* audio_desc = |
| 1903 | audio_content->media_description()->as_audio(); |
| 1904 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 1905 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1906 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1907 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1908 | const cricket::ContentInfo* video_content = |
| 1909 | GetFirstVideoContent(local_description()->description()); |
| 1910 | if (video_content) { |
| 1911 | if (video_content->rejected) { |
| 1912 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 1913 | } else { |
| 1914 | const cricket::VideoContentDescription* video_desc = |
| 1915 | video_content->media_description()->as_video(); |
| 1916 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 1917 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1918 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1922 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1923 | if (data_content) { |
| 1924 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1925 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1926 | if (rtc::starts_with(data_desc->protocol().data(), |
| 1927 | cricket::kMediaProtocolRtpPrefix)) { |
| 1928 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 1929 | } |
| 1930 | } |
| 1931 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1932 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 1936 | SetSessionDescriptionObserver* observer, |
| 1937 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 1938 | SetRemoteDescription( |
| 1939 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 1940 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 1941 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 1942 | } |
| 1943 | |
| 1944 | void PeerConnection::SetRemoteDescription( |
| 1945 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1946 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1947 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1948 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1949 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1950 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1951 | return; |
| 1952 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1953 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1954 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 1955 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1956 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1957 | return; |
| 1958 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1959 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1960 | const SdpType type = desc->GetType(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1961 | |
| 1962 | RTCError error = ApplyRemoteDescription(std::move(desc)); |
| 1963 | // |desc| may be destroyed at this point. |
| 1964 | |
| 1965 | if (!error.ok()) { |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1966 | std::ostringstream oss; |
| 1967 | oss << "Failed to set remote " << SdpTypeToString(type) |
| 1968 | << " sdp: " << error.message(); |
| 1969 | std::string error_message = oss.str(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1970 | RTC_LOG(LS_ERROR) << error_message << " (" << error.type() << ")"; |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 1971 | observer->OnSetRemoteDescriptionComplete( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1972 | RTCError(error.type(), std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1973 | return; |
| 1974 | } |
| 1975 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1976 | if (remote_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1977 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1978 | // MaybeStartGathering... |
| 1979 | network_thread()->Invoke<void>( |
| 1980 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1981 | port_allocator_.get())); |
| 1982 | } |
| 1983 | |
| 1984 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 1985 | } |
| 1986 | |
| 1987 | RTCError PeerConnection::ApplyRemoteDescription( |
| 1988 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1989 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1990 | RTC_DCHECK(desc); |
| 1991 | |
| 1992 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
| 1993 | if (!error.ok()) { |
| 1994 | return error; |
| 1995 | } |
| 1996 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1997 | // Update stats here so that we have the most recent stats for tracks and |
| 1998 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1999 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2000 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2001 | // Take a reference to the old remote description since it's used below to |
| 2002 | // compare against the new remote description. When setting the new remote |
| 2003 | // description, grab ownership of the replaced session description in case it |
| 2004 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2005 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2006 | const SessionDescriptionInterface* old_remote_description = |
| 2007 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2008 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2009 | SdpType type = desc->GetType(); |
| 2010 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2011 | replaced_remote_description = pending_remote_description_ |
| 2012 | ? std::move(pending_remote_description_) |
| 2013 | : std::move(current_remote_description_); |
| 2014 | current_remote_description_ = std::move(desc); |
| 2015 | pending_remote_description_ = nullptr; |
| 2016 | current_local_description_ = std::move(pending_local_description_); |
| 2017 | } else { |
| 2018 | replaced_remote_description = std::move(pending_remote_description_); |
| 2019 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2020 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2021 | // The session description to apply now must be accessed by |
| 2022 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2023 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2024 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2025 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2026 | if (IsUnifiedPlan()) { |
| 2027 | RTCError error = UpdateTransceiversAndDataChannels( |
| 2028 | cricket::CS_REMOTE, old_remote_description, *remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2029 | if (!error.ok()) { |
| 2030 | return error; |
| 2031 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2032 | } else { |
| 2033 | if (type == SdpType::kOffer) { |
| 2034 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2035 | // description is applied. Restore back to old description. |
| 2036 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2037 | if (!error.ok()) { |
| 2038 | return error; |
| 2039 | } |
| 2040 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2041 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2042 | // Remove unused channels if MediaContentDescription is rejected. |
| 2043 | RemoveUnusedChannels(remote_description()->description()); |
| 2044 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2045 | |
| 2046 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 2047 | // is called. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2048 | error = UpdateSessionState(type, cricket::CS_REMOTE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2049 | if (!error.ok()) { |
| 2050 | return error; |
| 2051 | } |
| 2052 | |
| 2053 | if (local_description() && |
| 2054 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2055 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2056 | } |
| 2057 | |
| 2058 | if (old_remote_description) { |
| 2059 | for (const cricket::ContentInfo& content : |
| 2060 | old_remote_description->description()->contents()) { |
| 2061 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2062 | // password that indicates the remote peer requests an ICE restart. |
| 2063 | // TODO(deadbeef): When we start storing both the current and pending |
| 2064 | // remote description, this should reset pending_ice_restarts and compare |
| 2065 | // against the current description. |
| 2066 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2067 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2068 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2069 | pending_ice_restarts_.insert(content.name); |
| 2070 | } |
| 2071 | } else { |
| 2072 | // We retain all received candidates only if ICE is not restarted. |
| 2073 | // When ICE is restarted, all previous candidates belong to an old |
| 2074 | // generation and should not be kept. |
| 2075 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2076 | // description should only contain candidates from the last set remote |
| 2077 | // description plus any candidates added since then. We should remove |
| 2078 | // this once we're sure it won't break anything. |
| 2079 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2080 | old_remote_description, content.name, mutable_remote_description()); |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | if (session_error() != SessionError::kNone) { |
| 2086 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2087 | } |
| 2088 | |
| 2089 | // Set the the ICE connection state to connecting since the connection may |
| 2090 | // become writable with peer reflexive candidates before any remote candidate |
| 2091 | // is signaled. |
| 2092 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2093 | // is to have a new signal the indicates a change in checking state from the |
| 2094 | // transport and expose a new checking() member from transport that can be |
| 2095 | // read to determine the current checking state. The existing SignalConnecting |
| 2096 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2097 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2098 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2099 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2100 | } |
| 2101 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2102 | // If setting the description decided our SSL role, allocate any necessary |
| 2103 | // SCTP sids. |
| 2104 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2105 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2106 | AllocateSctpSids(role); |
| 2107 | } |
| 2108 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2109 | if (IsUnifiedPlan()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2110 | std::vector<TrackEvent> track_events; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2111 | for (auto transceiver : transceivers_) { |
| 2112 | const ContentInfo* content = |
| 2113 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2114 | if (!content) { |
| 2115 | continue; |
| 2116 | } |
| 2117 | const MediaContentDescription* media_desc = content->media_description(); |
| 2118 | RtpTransceiverDirection local_direction = |
| 2119 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2120 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2121 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2122 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2123 | // process the addition of a remote track for the media description. |
| 2124 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
| 2125 | (!transceiver->current_direction() || |
| 2126 | !RtpTransceiverDirectionHasRecv( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2127 | *transceiver->current_direction())) && |
| 2128 | !media_desc->streams().empty()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2129 | const std::string& sync_label = media_desc->streams()[0].sync_label; |
| 2130 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2131 | remote_streams_->find(sync_label); |
| 2132 | if (!stream) { |
| 2133 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2134 | MediaStream::Create(sync_label)); |
| 2135 | remote_streams_->AddStream(stream); |
| 2136 | } |
| 2137 | transceiver->internal()->receiver_internal()->SetStreams({stream}); |
| 2138 | TrackEvent track_event; |
| 2139 | track_event.receiver = transceiver->receiver(); |
| 2140 | track_event.streams = transceiver->receiver()->streams(); |
| 2141 | track_events.push_back(std::move(track_event)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2142 | } |
| 2143 | // If direction is sendonly or inactive, and transceiver's current |
| 2144 | // direction is neither sendonly nor inactive, process the removal of a |
| 2145 | // remote track for the media description. |
| 2146 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2147 | (transceiver->current_direction() && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2148 | RtpTransceiverDirectionHasRecv(*transceiver->current_direction()))) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2149 | transceiver->internal()->receiver_internal()->SetStreams({}); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2150 | } |
| 2151 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2152 | transceiver->internal()->set_current_direction(local_direction); |
| 2153 | } |
| 2154 | if (content->rejected && !transceiver->stopped()) { |
| 2155 | transceiver->Stop(); |
| 2156 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2157 | if (!content->rejected && !media_desc->streams().empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2158 | transceiver->internal()->receiver_internal()->SetupMediaChannel( |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2159 | media_desc->streams()[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2160 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2161 | } |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2162 | for (auto event : track_events) { |
| 2163 | observer_->OnAddTrack(event.receiver, event.streams); |
| 2164 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2165 | } |
| 2166 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2167 | const cricket::ContentInfo* audio_content = |
| 2168 | GetFirstAudioContent(remote_description()->description()); |
| 2169 | const cricket::ContentInfo* video_content = |
| 2170 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2171 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2172 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2173 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2174 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2175 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2176 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2177 | |
| 2178 | // Check if the descriptions include streams, just in case the peer supports |
| 2179 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2180 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2181 | (audio_desc && !audio_desc->streams().empty()) || |
| 2182 | (video_desc && !video_desc->streams().empty())) { |
| 2183 | remote_peer_supports_msid_ = true; |
| 2184 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2185 | |
| 2186 | // We wait to signal new streams until we finish processing the description, |
| 2187 | // since only at that point will new streams have all their tracks. |
| 2188 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2189 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2190 | if (!IsUnifiedPlan()) { |
| 2191 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2192 | // rejected media section, there is some cleanup logic that expects the |
| 2193 | // voice/ video channel to still be set. But in this method the voice/video |
| 2194 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2195 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2196 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2197 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2198 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2199 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2200 | // and MediaStreams. |
| 2201 | if (audio_content) { |
| 2202 | if (audio_content->rejected) { |
| 2203 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2204 | } else { |
| 2205 | bool default_audio_track_needed = |
| 2206 | !remote_peer_supports_msid_ && |
| 2207 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2208 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2209 | default_audio_track_needed, audio_desc->type(), |
| 2210 | new_streams); |
| 2211 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2212 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2213 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2214 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2215 | // and MediaStreams. |
| 2216 | if (video_content) { |
| 2217 | if (video_content->rejected) { |
| 2218 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2219 | } else { |
| 2220 | bool default_video_track_needed = |
| 2221 | !remote_peer_supports_msid_ && |
| 2222 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2223 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2224 | default_video_track_needed, video_desc->type(), |
| 2225 | new_streams); |
| 2226 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2227 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2228 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2229 | // Update the DataChannels with the information from the remote peer. |
| 2230 | if (data_desc) { |
| 2231 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2232 | cricket::kMediaProtocolRtpPrefix)) { |
| 2233 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2234 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2235 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2236 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2237 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 2238 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2239 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2240 | stats_->AddStream(new_stream); |
| 2241 | observer_->OnAddStream( |
| 2242 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2243 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2244 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2245 | UpdateEndedRemoteMediaStreams(); |
| 2246 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2247 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2248 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2251 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2252 | cricket::ContentSource source, |
| 2253 | const SessionDescriptionInterface* old_session, |
| 2254 | const SessionDescriptionInterface& new_session) { |
| 2255 | RTC_DCHECK(IsUnifiedPlan()); |
| 2256 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2257 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2258 | if (new_session.GetType() == SdpType::kOffer) { |
| 2259 | auto bundle_group_or_error = |
| 2260 | GetEarlyBundleGroup(*new_session.description()); |
| 2261 | if (!bundle_group_or_error.ok()) { |
| 2262 | return bundle_group_or_error.MoveError(); |
| 2263 | } |
| 2264 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2265 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2266 | |
| 2267 | const ContentInfos& old_contents = |
| 2268 | (old_session ? old_session->description()->contents() : ContentInfos()); |
| 2269 | const ContentInfos& new_contents = new_session.description()->contents(); |
| 2270 | |
| 2271 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2272 | const cricket::ContentInfo& new_content = new_contents[i]; |
| 2273 | const cricket::ContentInfo* old_content = |
| 2274 | (i < old_contents.size() ? &old_contents[i] : nullptr); |
| 2275 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2276 | seen_mids_.insert(new_content.name); |
| 2277 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2278 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 2279 | auto transceiver_or_error = |
| 2280 | AssociateTransceiver(source, i, new_content, old_content); |
| 2281 | if (!transceiver_or_error.ok()) { |
| 2282 | return transceiver_or_error.MoveError(); |
| 2283 | } |
| 2284 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2285 | RTCError error = |
| 2286 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2287 | if (!error.ok()) { |
| 2288 | return error; |
| 2289 | } |
| 2290 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2291 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2292 | // Ignore all but the first data section. |
| 2293 | continue; |
| 2294 | } |
| 2295 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2296 | if (!error.ok()) { |
| 2297 | return error; |
| 2298 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2299 | } else { |
| 2300 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2301 | "Unknown section type."); |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | return RTCError::OK(); |
| 2306 | } |
| 2307 | |
| 2308 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2309 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2310 | transceiver, |
| 2311 | const cricket::ContentInfo& content, |
| 2312 | const cricket::ContentGroup* bundle_group) { |
| 2313 | RTC_DCHECK(IsUnifiedPlan()); |
| 2314 | RTC_DCHECK(transceiver); |
| 2315 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2316 | if (content.rejected) { |
| 2317 | if (channel) { |
| 2318 | transceiver->internal()->SetChannel(nullptr); |
| 2319 | DestroyBaseChannel(channel); |
| 2320 | } |
| 2321 | } else { |
| 2322 | if (!channel) { |
| 2323 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 2324 | channel = CreateVoiceChannel( |
| 2325 | content.name, |
| 2326 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2327 | } else { |
| 2328 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, |
| 2329 | transceiver->internal()->media_type()); |
| 2330 | channel = CreateVideoChannel( |
| 2331 | content.name, |
| 2332 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2333 | } |
| 2334 | if (!channel) { |
| 2335 | LOG_AND_RETURN_ERROR( |
| 2336 | RTCErrorType::INTERNAL_ERROR, |
| 2337 | "Failed to create channel for mid=" + content.name); |
| 2338 | } |
| 2339 | transceiver->internal()->SetChannel(channel); |
| 2340 | } |
| 2341 | } |
| 2342 | return RTCError::OK(); |
| 2343 | } |
| 2344 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2345 | RTCError PeerConnection::UpdateDataChannel( |
| 2346 | cricket::ContentSource source, |
| 2347 | const cricket::ContentInfo& content, |
| 2348 | const cricket::ContentGroup* bundle_group) { |
| 2349 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2350 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2351 | // will take care of rejecting it. |
| 2352 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2353 | } |
| 2354 | if (content.rejected) { |
| 2355 | DestroyDataChannel(); |
| 2356 | } else { |
| 2357 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 2358 | if (!CreateDataChannel(content.name, GetTransportNameForMediaSection( |
| 2359 | content.name, bundle_group))) { |
| 2360 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2361 | "Failed to create data channel."); |
| 2362 | } |
| 2363 | } |
| 2364 | if (source == cricket::CS_REMOTE) { |
| 2365 | const MediaContentDescription* data_desc = content.media_description(); |
| 2366 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2367 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | return RTCError::OK(); |
| 2372 | } |
| 2373 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2374 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2375 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
| 2376 | size_t mline_index, |
| 2377 | const ContentInfo& content, |
| 2378 | const ContentInfo* old_content) { |
| 2379 | RTC_DCHECK(IsUnifiedPlan()); |
| 2380 | // If the m= section is being recycled (rejected in previous remote |
| 2381 | // description, not rejected in current description), dissociate the currently |
| 2382 | // associated RtpTransceiver by setting its mid property to null, and discard |
| 2383 | // the mapping between the transceiver and its m= section index. |
| 2384 | if (old_content && old_content->rejected && !content.rejected) { |
| 2385 | auto old_transceiver = GetAssociatedTransceiver(old_content->name); |
| 2386 | if (old_transceiver) { |
| 2387 | old_transceiver->internal()->set_mid(rtc::nullopt); |
| 2388 | old_transceiver->internal()->set_mline_index(rtc::nullopt); |
| 2389 | } |
| 2390 | } |
| 2391 | const MediaContentDescription* media_desc = content.media_description(); |
| 2392 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2393 | if (source == cricket::CS_LOCAL) { |
| 2394 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2395 | // mapping between transceivers and m= section indices established when |
| 2396 | // creating the offer. |
| 2397 | if (!transceiver) { |
| 2398 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2399 | } |
| 2400 | if (!transceiver) { |
| 2401 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2402 | "Unknown transceiver"); |
| 2403 | } |
| 2404 | } else { |
| 2405 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2406 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2407 | // of the same type... |
| 2408 | if (!transceiver && |
| 2409 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2410 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2411 | } |
| 2412 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2413 | // recvonly direction. |
| 2414 | if (!transceiver) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2415 | auto sender = |
| 2416 | CreateSender(media_desc->type(), nullptr, {rtc::CreateRandomUuid()}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2417 | std::string receiver_id; |
| 2418 | if (!media_desc->streams().empty()) { |
| 2419 | receiver_id = media_desc->streams()[0].id; |
| 2420 | } else { |
| 2421 | receiver_id = rtc::CreateRandomUuid(); |
| 2422 | } |
| 2423 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2424 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2425 | transceiver->internal()->set_direction( |
| 2426 | RtpTransceiverDirection::kRecvOnly); |
| 2427 | } |
| 2428 | } |
| 2429 | RTC_DCHECK(transceiver); |
| 2430 | if (transceiver->internal()->media_type() != media_desc->type()) { |
| 2431 | LOG_AND_RETURN_ERROR( |
| 2432 | RTCErrorType::INVALID_PARAMETER, |
| 2433 | "Transceiver type does not match media description type."); |
| 2434 | } |
| 2435 | // Associate the found or created RtpTransceiver with the m= section by |
| 2436 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2437 | // section, and establish a mapping between the transceiver and the index of |
| 2438 | // the m= section. |
| 2439 | transceiver->internal()->set_mid(content.name); |
| 2440 | transceiver->internal()->set_mline_index(mline_index); |
| 2441 | return std::move(transceiver); |
| 2442 | } |
| 2443 | |
| 2444 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2445 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2446 | RTC_DCHECK(IsUnifiedPlan()); |
| 2447 | for (auto transceiver : transceivers_) { |
| 2448 | if (transceiver->mid() == mid) { |
| 2449 | return transceiver; |
| 2450 | } |
| 2451 | } |
| 2452 | return nullptr; |
| 2453 | } |
| 2454 | |
| 2455 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2456 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2457 | RTC_DCHECK(IsUnifiedPlan()); |
| 2458 | for (auto transceiver : transceivers_) { |
| 2459 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2460 | return transceiver; |
| 2461 | } |
| 2462 | } |
| 2463 | return nullptr; |
| 2464 | } |
| 2465 | |
| 2466 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2467 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2468 | cricket::MediaType media_type) const { |
| 2469 | RTC_DCHECK(IsUnifiedPlan()); |
| 2470 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2471 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2472 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2473 | // associated with any m= section and are not stopped, find the first such |
| 2474 | // RtpTransceiver. |
| 2475 | for (auto transceiver : transceivers_) { |
| 2476 | if (transceiver->internal()->media_type() == media_type && |
| 2477 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2478 | !transceiver->stopped()) { |
| 2479 | return transceiver; |
| 2480 | } |
| 2481 | } |
| 2482 | return nullptr; |
| 2483 | } |
| 2484 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2485 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2486 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2487 | transceiver, |
| 2488 | const SessionDescriptionInterface* sdesc) const { |
| 2489 | RTC_DCHECK(transceiver); |
| 2490 | RTC_DCHECK(sdesc); |
| 2491 | if (IsUnifiedPlan()) { |
| 2492 | if (!transceiver->internal()->mid()) { |
| 2493 | // This transceiver is not associated with a media section yet. |
| 2494 | return nullptr; |
| 2495 | } |
| 2496 | return sdesc->description()->GetContentByName( |
| 2497 | *transceiver->internal()->mid()); |
| 2498 | } else { |
| 2499 | // Plan B only allows at most one audio and one video section, so use the |
| 2500 | // first media section of that type. |
| 2501 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
| 2502 | transceiver->internal()->media_type()); |
| 2503 | } |
| 2504 | } |
| 2505 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2506 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2507 | return configuration_; |
| 2508 | } |
| 2509 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2510 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2511 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2512 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2513 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2514 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2515 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2516 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2517 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2518 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2519 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2520 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2521 | // The simplest (and most future-compatible) way to tell if the config was |
| 2522 | // modified in an invalid way is to copy each property we do support |
| 2523 | // modifying, then use operator==. There are far more properties we don't |
| 2524 | // support modifying than those we do, and more could be added. |
| 2525 | RTCConfiguration modified_config = configuration_; |
| 2526 | modified_config.servers = configuration.servers; |
| 2527 | modified_config.type = configuration.type; |
| 2528 | modified_config.ice_candidate_pool_size = |
| 2529 | configuration.ice_candidate_pool_size; |
| 2530 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2531 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2532 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2533 | modified_config.network_preference = configuration.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2534 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2535 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2536 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2537 | } |
| 2538 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2539 | // Validate the modified configuration. |
| 2540 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2541 | if (!validate_error.ok()) { |
| 2542 | return SafeSetError(std::move(validate_error), error); |
| 2543 | } |
| 2544 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2545 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2546 | // short in WebIDL. |
| 2547 | if (configuration.ice_candidate_pool_size < 0 || |
| 2548 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 2549 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2550 | } |
| 2551 | |
| 2552 | // Parse ICE servers before hopping to network thread. |
| 2553 | cricket::ServerAddresses stun_servers; |
| 2554 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2555 | RTCErrorType parse_error = |
| 2556 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2557 | if (parse_error != RTCErrorType::NONE) { |
| 2558 | return SafeSetError(parse_error, error); |
| 2559 | } |
| 2560 | |
| 2561 | // In theory this shouldn't fail. |
| 2562 | if (!network_thread()->Invoke<bool>( |
| 2563 | RTC_FROM_HERE, |
| 2564 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2565 | stun_servers, turn_servers, modified_config.type, |
| 2566 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2567 | modified_config.prune_turn_ports, |
| 2568 | modified_config.turn_customizer))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2569 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2570 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2571 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2572 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2573 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2574 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2575 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2576 | if (modified_config.servers != configuration_.servers || |
| 2577 | modified_config.type != configuration_.type || |
| 2578 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2579 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2580 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2581 | |
| 2582 | if (modified_config.ice_check_min_interval != |
| 2583 | configuration_.ice_check_min_interval) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2584 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2585 | } |
| 2586 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2587 | configuration_ = modified_config; |
| 2588 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2591 | bool PeerConnection::AddIceCandidate( |
| 2592 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2593 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2594 | if (IsClosed()) { |
| 2595 | return false; |
| 2596 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2597 | |
| 2598 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2599 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
| 2600 | << "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2601 | return false; |
| 2602 | } |
| 2603 | |
| 2604 | if (!ice_candidate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2605 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2606 | return false; |
| 2607 | } |
| 2608 | |
| 2609 | bool valid = false; |
| 2610 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2611 | if (!valid) { |
| 2612 | return false; |
| 2613 | } |
| 2614 | |
| 2615 | // Add this candidate to the remote session description. |
| 2616 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2617 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2618 | return false; |
| 2619 | } |
| 2620 | |
| 2621 | if (ready) { |
| 2622 | return UseCandidate(ice_candidate); |
| 2623 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2624 | RTC_LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2625 | return true; |
| 2626 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2629 | bool PeerConnection::RemoveIceCandidates( |
| 2630 | const std::vector<cricket::Candidate>& candidates) { |
| 2631 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2632 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2633 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
| 2634 | << "removed without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2635 | return false; |
| 2636 | } |
| 2637 | |
| 2638 | if (candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2639 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2640 | return false; |
| 2641 | } |
| 2642 | |
| 2643 | size_t number_removed = |
| 2644 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2645 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2646 | RTC_LOG(LS_ERROR) |
| 2647 | << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
| 2648 | << "Requested " << candidates.size() << " but only " << number_removed |
| 2649 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | // Remove the candidates from the transport controller. |
| 2653 | std::string error; |
| 2654 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 2655 | if (!res && !error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2656 | RTC_LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2657 | } |
| 2658 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2661 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2662 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2663 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2664 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2665 | if (transport_controller()) { |
| 2666 | transport_controller()->SetMetricsObserver(uma_observer_); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2669 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2670 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 2671 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2672 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2673 | uma_observer_->IncrementEnumCounter( |
| 2674 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 2675 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 2676 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2677 | uma_observer_->IncrementEnumCounter( |
| 2678 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 2679 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2680 | } |
| 2681 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2684 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2685 | if (!worker_thread()->IsCurrent()) { |
| 2686 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2687 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 2688 | } |
| 2689 | |
| 2690 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 2691 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 2692 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 2693 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 2694 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2695 | "min_bitrate_bps <= 0"); |
| 2696 | } |
| 2697 | if (has_current) { |
| 2698 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2699 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2700 | "current_bitrate_bps < min_bitrate_bps"); |
| 2701 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 2702 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2703 | "curent_bitrate_bps < 0"); |
| 2704 | } |
| 2705 | } |
| 2706 | if (has_max) { |
| 2707 | if (has_current && |
| 2708 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 2709 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2710 | "max_bitrate_bps < current_bitrate_bps"); |
| 2711 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2712 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2713 | "max_bitrate_bps < min_bitrate_bps"); |
| 2714 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 2715 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2716 | "max_bitrate_bps < 0"); |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | Call::Config::BitrateConfigMask mask; |
| 2721 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 2722 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 2723 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 2724 | |
| 2725 | RTC_DCHECK(call_.get()); |
| 2726 | call_->SetBitrateConfigMask(mask); |
| 2727 | |
| 2728 | return RTCError::OK(); |
| 2729 | } |
| 2730 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 2731 | void PeerConnection::SetBitrateAllocationStrategy( |
| 2732 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 2733 | bitrate_allocation_strategy) { |
| 2734 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 2735 | if (!worker_thread->IsCurrent()) { |
| 2736 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 2737 | bitrate_allocation_strategy.release(); |
| 2738 | auto functor = [this, strategy_raw]() { |
| 2739 | call_->SetBitrateAllocationStrategy( |
| 2740 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 2741 | }; |
| 2742 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 2743 | return; |
| 2744 | } |
| 2745 | RTC_DCHECK(call_.get()); |
| 2746 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 2747 | } |
| 2748 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 2749 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 2750 | if (!worker_thread()->IsCurrent()) { |
| 2751 | worker_thread()->Invoke<void>( |
| 2752 | RTC_FROM_HERE, |
| 2753 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 2754 | return; |
| 2755 | } |
| 2756 | auto audio_state = |
| 2757 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2758 | audio_state->SetPlayout(playout); |
| 2759 | } |
| 2760 | |
| 2761 | void PeerConnection::SetAudioRecording(bool recording) { |
| 2762 | if (!worker_thread()->IsCurrent()) { |
| 2763 | worker_thread()->Invoke<void>( |
| 2764 | RTC_FROM_HERE, |
| 2765 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 2766 | return; |
| 2767 | } |
| 2768 | auto audio_state = |
| 2769 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2770 | audio_state->SetRecording(recording); |
| 2771 | } |
| 2772 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2773 | std::unique_ptr<rtc::SSLCertificate> |
| 2774 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2775 | if (!voice_channel()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2776 | return nullptr; |
| 2777 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2778 | return GetRemoteSSLCertificate(voice_channel()->transport_name()); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2779 | } |
| 2780 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2781 | std::unique_ptr<rtc::SSLCertChain> |
| 2782 | PeerConnection::GetRemoteAudioSSLCertChain() { |
| 2783 | if (!voice_channel()) { |
| 2784 | return nullptr; |
| 2785 | } |
| 2786 | |
| 2787 | return transport_controller_->GetRemoteSSLCertChain( |
| 2788 | voice_channel()->transport_name()); |
| 2789 | } |
| 2790 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2791 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 2792 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2793 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 2794 | const size_t max_size = (max_size_bytes < 0) |
| 2795 | ? RtcEventLog::kUnlimitedOutput |
| 2796 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 2797 | return StartRtcEventLog( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2798 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size), |
| 2799 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2800 | } |
| 2801 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2802 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 2803 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 2804 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 2805 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2806 | bool operator()() { |
| 2807 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 2808 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 2809 | PeerConnection* const pc; |
| 2810 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2811 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2812 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2813 | return worker_thread()->Invoke<bool>( |
| 2814 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2818 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2819 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 2820 | } |
| 2821 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2822 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2823 | return pending_local_description_ ? pending_local_description_.get() |
| 2824 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2828 | return pending_remote_description_ ? pending_remote_description_.get() |
| 2829 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2832 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 2833 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2834 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 2838 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2839 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 2843 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2844 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 2848 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2849 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2850 | } |
| 2851 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2852 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2853 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2854 | // Update stats here so that we have the most recent stats for tracks and |
| 2855 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2856 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2857 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2858 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 2859 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 2860 | for (auto transceiver : transceivers_) { |
| 2861 | transceiver->Stop(); |
| 2862 | } |
| 2863 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2864 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 2865 | // The event log is used in the transport controller, which must be outlived |
| 2866 | // by the former. CreateOffer by the peer connection is implemented |
| 2867 | // asynchronously and if the peer connection is closed without resetting the |
| 2868 | // WebRTC session description factory, the session description factory would |
| 2869 | // call the transport controller. |
| 2870 | webrtc_session_desc_factory_.reset(); |
| 2871 | transport_controller_.reset(); |
| 2872 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 2873 | network_thread()->Invoke<void>( |
| 2874 | RTC_FROM_HERE, |
| 2875 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2876 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 2877 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2878 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 2879 | call_.reset(); |
| 2880 | // The event log must outlive call (and any other object that uses it). |
| 2881 | event_log_.reset(); |
| 2882 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2885 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2886 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2887 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 2888 | SetSessionDescriptionMsg* param = |
| 2889 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 2890 | param->observer->OnSuccess(); |
| 2891 | delete param; |
| 2892 | break; |
| 2893 | } |
| 2894 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 2895 | SetSessionDescriptionMsg* param = |
| 2896 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 2897 | param->observer->OnFailure(param->error); |
| 2898 | delete param; |
| 2899 | break; |
| 2900 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2901 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 2902 | CreateSessionDescriptionMsg* param = |
| 2903 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 2904 | param->observer->OnFailure(param->error); |
| 2905 | delete param; |
| 2906 | break; |
| 2907 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2908 | case MSG_GETSTATS: { |
| 2909 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 2910 | StatsReports reports; |
| 2911 | stats_->GetStats(param->track, &reports); |
| 2912 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2913 | delete param; |
| 2914 | break; |
| 2915 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 2916 | case MSG_FREE_DATACHANNELS: { |
| 2917 | sctp_data_channels_to_free_.clear(); |
| 2918 | break; |
| 2919 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2920 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 2921 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2922 | break; |
| 2923 | } |
| 2924 | } |
| 2925 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2926 | void PeerConnection::CreateAudioReceiver( |
| 2927 | MediaStreamInterface* stream, |
| 2928 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 2929 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 2930 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2931 | auto* audio_receiver = new AudioRtpReceiver( |
| 2932 | worker_thread(), remote_sender_info.sender_id, streams); |
| 2933 | audio_receiver->SetMediaChannel(voice_media_channel()); |
| 2934 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 2935 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 2936 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2937 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 2938 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2941 | void PeerConnection::CreateVideoReceiver( |
| 2942 | MediaStreamInterface* stream, |
| 2943 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 2944 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 2945 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2946 | auto* video_receiver = new VideoRtpReceiver( |
| 2947 | worker_thread(), remote_sender_info.sender_id, streams); |
| 2948 | video_receiver->SetMediaChannel(video_media_channel()); |
| 2949 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 2950 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 2951 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2952 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 2953 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2956 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 2957 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2958 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2959 | const RtpSenderInfo& remote_sender_info) { |
| 2960 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 2961 | if (!receiver) { |
| 2962 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 2963 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2964 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2965 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2966 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 2967 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 2968 | } else { |
| 2969 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 2970 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2971 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2974 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 2975 | MediaStreamInterface* stream) { |
| 2976 | RTC_DCHECK(!IsClosed()); |
| 2977 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2978 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2979 | // We already have a sender for this track, so just change the stream_id |
| 2980 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2981 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2982 | return; |
| 2983 | } |
| 2984 | |
| 2985 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2986 | auto new_sender = |
| 2987 | CreateSender(cricket::MEDIA_TYPE_AUDIO, track, {stream->label()}); |
| 2988 | static_cast<AudioRtpSender*>(new_sender->internal()) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 2989 | ->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2990 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 2991 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 2992 | // which will connect the sender to the underlying transport. This can |
| 2993 | // occur if a local session description that contains the ID of the sender |
| 2994 | // is set before AddStream is called. It can also occur if the local |
| 2995 | // session description is not changed and RemoveStream is called, and |
| 2996 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2997 | const RtpSenderInfo* sender_info = |
| 2998 | FindSenderInfo(local_audio_sender_infos_, stream->label(), track->id()); |
| 2999 | if (sender_info) { |
| 3000 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3001 | } |
| 3002 | } |
| 3003 | |
| 3004 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3005 | // indefinitely, when we have unified plan SDP. |
| 3006 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3007 | MediaStreamInterface* stream) { |
| 3008 | RTC_DCHECK(!IsClosed()); |
| 3009 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3010 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3011 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3012 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3013 | return; |
| 3014 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3015 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3019 | MediaStreamInterface* stream) { |
| 3020 | RTC_DCHECK(!IsClosed()); |
| 3021 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3022 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3023 | // We already have a sender for this track, so just change the stream_id |
| 3024 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3025 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3026 | return; |
| 3027 | } |
| 3028 | |
| 3029 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3030 | auto new_sender = |
| 3031 | CreateSender(cricket::MEDIA_TYPE_VIDEO, track, {stream->label()}); |
| 3032 | static_cast<VideoRtpSender*>(new_sender->internal()) |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 3033 | ->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3034 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3035 | const RtpSenderInfo* sender_info = |
| 3036 | FindSenderInfo(local_video_sender_infos_, stream->label(), track->id()); |
| 3037 | if (sender_info) { |
| 3038 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3043 | MediaStreamInterface* stream) { |
| 3044 | RTC_DCHECK(!IsClosed()); |
| 3045 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3046 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3047 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3048 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3049 | return; |
| 3050 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3051 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3052 | } |
| 3053 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3054 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3055 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3056 | if (ice_connection_state_ == new_state) { |
| 3057 | return; |
| 3058 | } |
| 3059 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3060 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3061 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3062 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3063 | return; |
| 3064 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3065 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3066 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3067 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3068 | RTC_DCHECK(ice_connection_state_ != |
| 3069 | PeerConnectionInterface::kIceConnectionClosed); |
| 3070 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3071 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3072 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | void PeerConnection::OnIceGatheringChange( |
| 3076 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3077 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3078 | if (IsClosed()) { |
| 3079 | return; |
| 3080 | } |
| 3081 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3082 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3085 | void PeerConnection::OnIceCandidate( |
| 3086 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3087 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3088 | if (IsClosed()) { |
| 3089 | return; |
| 3090 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3091 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3094 | void PeerConnection::OnIceCandidatesRemoved( |
| 3095 | const std::vector<cricket::Candidate>& candidates) { |
| 3096 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3097 | if (IsClosed()) { |
| 3098 | return; |
| 3099 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3100 | observer_->OnIceCandidatesRemoved(candidates); |
| 3101 | } |
| 3102 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3103 | void PeerConnection::ChangeSignalingState( |
| 3104 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3105 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3106 | if (signaling_state_ == signaling_state) { |
| 3107 | return; |
| 3108 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3109 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3110 | << GetSignalingStateString(signaling_state_) |
| 3111 | << " New state: " |
| 3112 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3113 | signaling_state_ = signaling_state; |
| 3114 | if (signaling_state == kClosed) { |
| 3115 | ice_connection_state_ = kIceConnectionClosed; |
| 3116 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 3117 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3118 | ice_gathering_state_ = kIceGatheringComplete; |
| 3119 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 3120 | } |
| 3121 | } |
| 3122 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3125 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3126 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3127 | if (IsClosed()) { |
| 3128 | return; |
| 3129 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3130 | AddAudioTrack(track, stream); |
| 3131 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3132 | } |
| 3133 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3134 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3135 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3136 | if (IsClosed()) { |
| 3137 | return; |
| 3138 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3139 | RemoveAudioTrack(track, stream); |
| 3140 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3144 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3145 | if (IsClosed()) { |
| 3146 | return; |
| 3147 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3148 | AddVideoTrack(track, stream); |
| 3149 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3153 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3154 | if (IsClosed()) { |
| 3155 | return; |
| 3156 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3157 | RemoveVideoTrack(track, stream); |
| 3158 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3159 | } |
| 3160 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3161 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3162 | SetSessionDescriptionObserver* observer) { |
| 3163 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3164 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3165 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 3166 | } |
| 3167 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3168 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3169 | SetSessionDescriptionObserver* observer, |
| 3170 | const std::string& error) { |
| 3171 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3172 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3173 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3174 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3178 | CreateSessionDescriptionObserver* observer, |
| 3179 | const std::string& error) { |
| 3180 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 3181 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3182 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3183 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3186 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3187 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3188 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3189 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3190 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3191 | if (IsUnifiedPlan()) { |
| 3192 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3193 | } else { |
| 3194 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3195 | } |
| 3196 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3197 | // Intentionally unset the data channel type for RTP data channel with the |
| 3198 | // second condition. Otherwise the RTP data channels would be successfully |
| 3199 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3200 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3201 | // people won't try to use them. |
| 3202 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3203 | session_options->data_channel_type = data_channel_type(); |
| 3204 | } |
| 3205 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3206 | // Apply ICE restart flag and renomination flag. |
| 3207 | for (auto& options : session_options->media_description_options) { |
| 3208 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3209 | options.transport_options.enable_ice_renomination = |
| 3210 | configuration_.enable_ice_renomination; |
| 3211 | } |
| 3212 | |
| 3213 | session_options->rtcp_cname = rtcp_cname_; |
| 3214 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3215 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3219 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3220 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3221 | // Figure out transceiver directional preferences. |
| 3222 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3223 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3224 | |
| 3225 | // By default, generate sendrecv/recvonly m= sections. |
| 3226 | bool recv_audio = true; |
| 3227 | bool recv_video = true; |
| 3228 | |
| 3229 | // By default, only offer a new m= section if we have media to send with it. |
| 3230 | bool offer_new_audio_description = send_audio; |
| 3231 | bool offer_new_video_description = send_video; |
| 3232 | bool offer_new_data_description = HasDataChannels(); |
| 3233 | |
| 3234 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3235 | if (offer_answer_options.offer_to_receive_audio != |
| 3236 | RTCOfferAnswerOptions::kUndefined) { |
| 3237 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3238 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3239 | offer_new_audio_description || |
| 3240 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3241 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3242 | if (offer_answer_options.offer_to_receive_video != |
| 3243 | RTCOfferAnswerOptions::kUndefined) { |
| 3244 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3245 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3246 | offer_new_video_description || |
| 3247 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | rtc::Optional<size_t> audio_index; |
| 3251 | rtc::Optional<size_t> video_index; |
| 3252 | rtc::Optional<size_t> data_index; |
| 3253 | // If a current description exists, generate m= sections in the same order, |
| 3254 | // using the first audio/video/data section that appears and rejecting |
| 3255 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3256 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3257 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3258 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3259 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3260 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3261 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3264 | // Add audio/video/data m= sections to the end if needed. |
| 3265 | if (!audio_index && offer_new_audio_description) { |
| 3266 | session_options->media_description_options.push_back( |
| 3267 | cricket::MediaDescriptionOptions( |
| 3268 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3269 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3270 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3271 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3272 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3273 | if (!video_index && offer_new_video_description) { |
| 3274 | session_options->media_description_options.push_back( |
| 3275 | cricket::MediaDescriptionOptions( |
| 3276 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3277 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3278 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3279 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3280 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3281 | if (!data_index && offer_new_data_description) { |
| 3282 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3283 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3284 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3288 | !audio_index ? nullptr |
| 3289 | : &session_options->media_description_options[*audio_index]; |
| 3290 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3291 | !video_index ? nullptr |
| 3292 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3293 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3294 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3295 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3299 | // and return a reference to it. |
| 3300 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3301 | // the RTP packet. |
| 3302 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3303 | RTC_DCHECK(used_mids); |
| 3304 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3305 | size_t i = 0; |
| 3306 | std::set<std::string>::iterator it; |
| 3307 | bool inserted; |
| 3308 | do { |
| 3309 | std::string mid = rtc::ToString(i++); |
| 3310 | auto insert_result = used_mids->insert(mid); |
| 3311 | it = insert_result.first; |
| 3312 | inserted = insert_result.second; |
| 3313 | } while (!inserted); |
| 3314 | return *it; |
| 3315 | } |
| 3316 | |
| 3317 | static cricket::MediaDescriptionOptions |
| 3318 | GetMediaDescriptionOptionsForTransceiver( |
| 3319 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3320 | transceiver, |
| 3321 | const std::string& mid) { |
| 3322 | cricket::MediaDescriptionOptions media_description_options( |
| 3323 | transceiver->internal()->media_type(), mid, transceiver->direction(), |
| 3324 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3325 | // This behavior is specified in JSEP. The gist is that: |
| 3326 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3327 | // sendrecv. |
| 3328 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3329 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3330 | if (!transceiver->stopped() && |
| 3331 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3332 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3333 | cricket::SenderOptions sender_options; |
| 3334 | sender_options.track_id = transceiver->sender()->id(); |
| 3335 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3336 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3337 | // set in the RTP parameters when the transceiver was added. |
| 3338 | sender_options.num_sim_layers = 1; |
| 3339 | media_description_options.sender_options.push_back(sender_options); |
| 3340 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3341 | return media_description_options; |
| 3342 | } |
| 3343 | |
| 3344 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3345 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3346 | cricket::MediaSessionOptions* session_options) { |
| 3347 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3348 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3349 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3350 | const ContentInfos& local_contents = |
| 3351 | (local_description() ? local_description()->description()->contents() |
| 3352 | : ContentInfos()); |
| 3353 | const ContentInfos& remote_contents = |
| 3354 | (remote_description() ? remote_description()->description()->contents() |
| 3355 | : ContentInfos()); |
| 3356 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3357 | // slots first. |
| 3358 | std::queue<size_t> recycleable_mline_indices; |
| 3359 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3360 | // offer so that new, unique MIDs are generated. |
| 3361 | std::set<std::string> used_mids = seen_mids_; |
| 3362 | // First, go through each media section that exists in either the local or |
| 3363 | // remote description and generate a media section in this offer for the |
| 3364 | // associated transceiver. If a media section can be recycled, generate a |
| 3365 | // default, rejected media section here that can be later overwritten. |
| 3366 | for (size_t i = 0; |
| 3367 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3368 | // Either |local_content| or |remote_content| is non-null. |
| 3369 | const ContentInfo* local_content = |
| 3370 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3371 | const ContentInfo* remote_content = |
| 3372 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3373 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3374 | (remote_content && remote_content->rejected); |
| 3375 | const std::string& mid = |
| 3376 | (local_content ? local_content->name : remote_content->name); |
| 3377 | cricket::MediaType media_type = |
| 3378 | (local_content ? local_content->media_description()->type() |
| 3379 | : remote_content->media_description()->type()); |
| 3380 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3381 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3382 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3383 | RTC_CHECK(transceiver); |
| 3384 | // A media section is considered eligible for recycling if it is marked as |
| 3385 | // rejected in either the local or remote description. |
| 3386 | if (had_been_rejected) { |
| 3387 | session_options->media_description_options.push_back( |
| 3388 | cricket::MediaDescriptionOptions( |
| 3389 | transceiver->internal()->media_type(), mid, |
| 3390 | RtpTransceiverDirection::kInactive, |
| 3391 | /*stopped=*/true)); |
| 3392 | recycleable_mline_indices.push(i); |
| 3393 | } else { |
| 3394 | session_options->media_description_options.push_back( |
| 3395 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3396 | // CreateOffer shouldn't really cause any state changes in |
| 3397 | // PeerConnection, but we need a way to match new transceivers to new |
| 3398 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3399 | // by recording the index of the media section generated for the |
| 3400 | // transceiver in the offer. |
| 3401 | transceiver->internal()->set_mline_index(i); |
| 3402 | } |
| 3403 | } else { |
| 3404 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3405 | RTC_CHECK(GetDataMid()); |
| 3406 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3407 | session_options->media_description_options.push_back( |
| 3408 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3409 | } else { |
| 3410 | session_options->media_description_options.push_back( |
| 3411 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3412 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3416 | // and not associated). Reuse media sections marked as recyclable first, |
| 3417 | // otherwise append to the end of the offer. New media sections should be |
| 3418 | // added in the order they were added to the PeerConnection. |
| 3419 | for (auto transceiver : transceivers_) { |
| 3420 | if (transceiver->mid() || transceiver->stopped()) { |
| 3421 | continue; |
| 3422 | } |
| 3423 | size_t mline_index; |
| 3424 | if (!recycleable_mline_indices.empty()) { |
| 3425 | mline_index = recycleable_mline_indices.front(); |
| 3426 | recycleable_mline_indices.pop(); |
| 3427 | session_options->media_description_options[mline_index] = |
| 3428 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3429 | AllocateMid(&used_mids)); |
| 3430 | } else { |
| 3431 | mline_index = session_options->media_description_options.size(); |
| 3432 | session_options->media_description_options.push_back( |
| 3433 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3434 | AllocateMid(&used_mids))); |
| 3435 | } |
| 3436 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3437 | transceiver->internal()->set_mline_index(mline_index); |
| 3438 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3439 | // Lastly, add a m-section if we have local data channels and an m section |
| 3440 | // does not already exist. |
| 3441 | if (!GetDataMid() && HasDataChannels()) { |
| 3442 | session_options->media_description_options.push_back( |
| 3443 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3444 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | void PeerConnection::GetOptionsForAnswer( |
| 3448 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3449 | cricket::MediaSessionOptions* session_options) { |
| 3450 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3451 | |
| 3452 | if (IsUnifiedPlan()) { |
| 3453 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3454 | } else { |
| 3455 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3456 | } |
| 3457 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3458 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3459 | // the RTP data channels would be successfully negotiated by default and the |
| 3460 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3461 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3462 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3463 | session_options->data_channel_type = data_channel_type(); |
| 3464 | } |
| 3465 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3466 | // Apply ICE renomination flag. |
| 3467 | for (auto& options : session_options->media_description_options) { |
| 3468 | options.transport_options.enable_ice_renomination = |
| 3469 | configuration_.enable_ice_renomination; |
| 3470 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3471 | |
| 3472 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3473 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3474 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3475 | } |
| 3476 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3477 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3478 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3479 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3480 | // Figure out transceiver directional preferences. |
| 3481 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3482 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3483 | |
| 3484 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3485 | // restricted by the direction in the offer. |
| 3486 | bool recv_audio = true; |
| 3487 | bool recv_video = true; |
| 3488 | |
| 3489 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3490 | if (offer_answer_options.offer_to_receive_audio != |
| 3491 | RTCOfferAnswerOptions::kUndefined) { |
| 3492 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3493 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3494 | if (offer_answer_options.offer_to_receive_video != |
| 3495 | RTCOfferAnswerOptions::kUndefined) { |
| 3496 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | rtc::Optional<size_t> audio_index; |
| 3500 | rtc::Optional<size_t> video_index; |
| 3501 | rtc::Optional<size_t> data_index; |
Oleh Prypin | c22d6a8 | 2018-02-02 08:42:18 +0000 | [diff] [blame] | 3502 | if (remote_description()) { |
| 3503 | // The pending remote description should be an offer. |
| 3504 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3505 | // Generate m= sections that match those in the offer. |
| 3506 | // Note that mediasession.cc will handle intersection our preferred |
| 3507 | // direction with the offered direction. |
| 3508 | GenerateMediaDescriptionOptions( |
| 3509 | remote_description(), |
| 3510 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3511 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3512 | &audio_index, &video_index, &data_index, session_options); |
| 3513 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3514 | |
| 3515 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3516 | !audio_index ? nullptr |
| 3517 | : &session_options->media_description_options[*audio_index]; |
| 3518 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3519 | !video_index ? nullptr |
| 3520 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3521 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3522 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3523 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3524 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3525 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3526 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3527 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3528 | cricket::MediaSessionOptions* session_options) { |
| 3529 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3530 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3531 | RTC_DCHECK(remote_description()); |
| 3532 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3533 | for (const ContentInfo& content : |
| 3534 | remote_description()->description()->contents()) { |
| 3535 | cricket::MediaType media_type = content.media_description()->type(); |
| 3536 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3537 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3538 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3539 | RTC_CHECK(transceiver); |
| 3540 | session_options->media_description_options.push_back( |
| 3541 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3542 | } else { |
| 3543 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3544 | // Reject all data sections if data channels are disabled. |
| 3545 | // Reject a data section if it has already been rejected. |
| 3546 | // Reject all data sections except for the first one. |
| 3547 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3548 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3549 | session_options->media_description_options.push_back( |
| 3550 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3551 | } else { |
| 3552 | session_options->media_description_options.push_back( |
| 3553 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3554 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3555 | } |
| 3556 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3557 | } |
| 3558 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3559 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3560 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3561 | RtpTransceiverDirection audio_direction, |
| 3562 | RtpTransceiverDirection video_direction, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3563 | rtc::Optional<size_t>* audio_index, |
| 3564 | rtc::Optional<size_t>* video_index, |
| 3565 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3566 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3567 | for (const cricket::ContentInfo& content : |
| 3568 | session_desc->description()->contents()) { |
| 3569 | if (IsAudioContent(&content)) { |
| 3570 | // If we already have an audio m= section, reject this extra one. |
| 3571 | if (*audio_index) { |
| 3572 | session_options->media_description_options.push_back( |
| 3573 | cricket::MediaDescriptionOptions( |
| 3574 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3575 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3576 | } else { |
| 3577 | session_options->media_description_options.push_back( |
| 3578 | cricket::MediaDescriptionOptions( |
| 3579 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3580 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3581 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3582 | } |
| 3583 | } else if (IsVideoContent(&content)) { |
| 3584 | // If we already have an video m= section, reject this extra one. |
| 3585 | if (*video_index) { |
| 3586 | session_options->media_description_options.push_back( |
| 3587 | cricket::MediaDescriptionOptions( |
| 3588 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3589 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3590 | } else { |
| 3591 | session_options->media_description_options.push_back( |
| 3592 | cricket::MediaDescriptionOptions( |
| 3593 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3594 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3595 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3596 | } |
| 3597 | } else { |
| 3598 | RTC_DCHECK(IsDataContent(&content)); |
| 3599 | // If we already have an data m= section, reject this extra one. |
| 3600 | if (*data_index) { |
| 3601 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3602 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3603 | } else { |
| 3604 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3605 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3606 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3607 | } |
| 3608 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3609 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3612 | cricket::MediaDescriptionOptions |
| 3613 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 3614 | const std::string& mid) const { |
| 3615 | // Direction for data sections is meaningless, but legacy endpoints might |
| 3616 | // expect sendrecv. |
| 3617 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3618 | RtpTransceiverDirection::kSendRecv, |
| 3619 | /*stopped=*/false); |
| 3620 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3621 | return options; |
| 3622 | } |
| 3623 | |
| 3624 | cricket::MediaDescriptionOptions |
| 3625 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 3626 | const std::string& mid) const { |
| 3627 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3628 | RtpTransceiverDirection::kInactive, |
| 3629 | /*stopped=*/true); |
| 3630 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3631 | return options; |
| 3632 | } |
| 3633 | |
| 3634 | rtc::Optional<std::string> PeerConnection::GetDataMid() const { |
| 3635 | switch (data_channel_type_) { |
| 3636 | case cricket::DCT_RTP: |
| 3637 | if (!rtp_data_channel_) { |
| 3638 | return rtc::nullopt; |
| 3639 | } |
| 3640 | return rtp_data_channel_->content_name(); |
| 3641 | case cricket::DCT_SCTP: |
| 3642 | return sctp_content_name_; |
| 3643 | default: |
| 3644 | return rtc::nullopt; |
| 3645 | } |
| 3646 | } |
| 3647 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3648 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 3649 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 3650 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3651 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 3652 | } |
| 3653 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3654 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3655 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3656 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3657 | cricket::MediaType media_type, |
| 3658 | StreamCollection* new_streams) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3659 | std::vector<RtpSenderInfo>* current_senders = |
| 3660 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3661 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3662 | // 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] | 3663 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3664 | for (auto sender_it = current_senders->begin(); |
| 3665 | sender_it != current_senders->end(); |
| 3666 | /* incremented manually */) { |
| 3667 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3668 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3669 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 3670 | bool sender_exists = params && params->id == info.sender_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3671 | // 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] | 3672 | if ((info.stream_label == kDefaultStreamLabel && default_sender_needed) || |
| 3673 | sender_exists) { |
| 3674 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3675 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3676 | OnRemoteSenderRemoved(info, media_type); |
| 3677 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3681 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3682 | for (const cricket::StreamParams& params : streams) { |
| 3683 | // 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] | 3684 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3685 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3686 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3687 | uint32_t ssrc = params.first_ssrc(); |
| 3688 | |
| 3689 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3690 | remote_streams_->find(stream_label); |
| 3691 | if (!stream) { |
| 3692 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3693 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3694 | MediaStream::Create(stream_label)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3695 | remote_streams_->AddStream(stream); |
| 3696 | new_streams->AddStream(stream); |
| 3697 | } |
| 3698 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3699 | const RtpSenderInfo* sender_info = |
| 3700 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 3701 | if (!sender_info) { |
| 3702 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 3703 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3704 | } |
| 3705 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3706 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3707 | // Add default sender if necessary. |
| 3708 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3709 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
| 3710 | remote_streams_->find(kDefaultStreamLabel); |
| 3711 | if (!default_stream) { |
| 3712 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3713 | default_stream = MediaStreamProxy::Create( |
| 3714 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamLabel)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3715 | remote_streams_->AddStream(default_stream); |
| 3716 | new_streams->AddStream(default_stream); |
| 3717 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3718 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 3719 | ? kDefaultAudioSenderId |
| 3720 | : kDefaultVideoSenderId; |
| 3721 | const RtpSenderInfo* default_sender_info = FindSenderInfo( |
| 3722 | *current_senders, kDefaultStreamLabel, default_sender_id); |
| 3723 | if (!default_sender_info) { |
| 3724 | current_senders->push_back( |
| 3725 | RtpSenderInfo(kDefaultStreamLabel, default_sender_id, 0)); |
| 3726 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3727 | } |
| 3728 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3729 | } |
| 3730 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3731 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 3732 | cricket::MediaType media_type) { |
| 3733 | MediaStreamInterface* stream = |
| 3734 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3735 | |
| 3736 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3737 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3738 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3739 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3740 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3741 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3745 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 3746 | cricket::MediaType media_type) { |
| 3747 | MediaStreamInterface* stream = |
| 3748 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3749 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3750 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3751 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3752 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 3753 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3754 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3755 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3756 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3757 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3758 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3759 | } |
| 3760 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3761 | // Stopping or destroying a VideoRtpReceiver will end the |
| 3762 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3763 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3764 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3765 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3766 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3767 | // There's no guarantee the track is still available, e.g. the track may |
| 3768 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3769 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3770 | } |
| 3771 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 3772 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3773 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3774 | if (receiver) { |
| 3775 | observer_->OnRemoveTrack(receiver); |
| 3776 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 3780 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 3781 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 3782 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 3783 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 3784 | streams_to_remove.push_back(stream); |
| 3785 | } |
| 3786 | } |
| 3787 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3788 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3789 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3790 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3791 | } |
| 3792 | } |
| 3793 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3794 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3795 | const std::vector<cricket::StreamParams>& streams, |
| 3796 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3797 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3798 | |
| 3799 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 3800 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3801 | for (auto sender_it = current_senders->begin(); |
| 3802 | sender_it != current_senders->end(); |
| 3803 | /* incremented manually */) { |
| 3804 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3805 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3806 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 3807 | if (!params || params->id != info.sender_id || |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3808 | params->sync_label != info.stream_label) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3809 | OnLocalSenderRemoved(info, media_type); |
| 3810 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3811 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3812 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3813 | } |
| 3814 | } |
| 3815 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3816 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3817 | for (const cricket::StreamParams& params : streams) { |
| 3818 | // 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] | 3819 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3820 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3821 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3822 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3823 | const RtpSenderInfo* sender_info = |
| 3824 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 3825 | if (!sender_info) { |
| 3826 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 3827 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | } |
| 3831 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3832 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 3833 | cricket::MediaType media_type) { |
| 3834 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3835 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3836 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 3837 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3838 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3839 | return; |
| 3840 | } |
| 3841 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3842 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3843 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 3844 | << " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3845 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3846 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3847 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3848 | sender->internal()->set_stream_id(sender_info.stream_label); |
| 3849 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3850 | } |
| 3851 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3852 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 3853 | cricket::MediaType media_type) { |
| 3854 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3855 | if (!sender) { |
| 3856 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3857 | // SessionDescriptions has been renegotiated. |
| 3858 | return; |
| 3859 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3860 | |
| 3861 | // A sender has been removed from the SessionDescription but it's still |
| 3862 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 3863 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 3864 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3865 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 3866 | << " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3867 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3868 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3869 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3870 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 3874 | const cricket::StreamParamsVec& streams) { |
| 3875 | std::vector<std::string> existing_channels; |
| 3876 | |
| 3877 | // Find new and active data channels. |
| 3878 | for (const cricket::StreamParams& params : streams) { |
| 3879 | // |it->sync_label| is actually the data channel label. The reason is that |
| 3880 | // we use the same naming of data channels as we do for |
| 3881 | // MediaStreams and Tracks. |
| 3882 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 3883 | // track label is the same as |streamid|. |
| 3884 | const std::string& channel_label = params.sync_label; |
| 3885 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 3886 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3887 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3888 | continue; |
| 3889 | } |
| 3890 | // Set the SSRC the data channel should use for sending. |
| 3891 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 3892 | existing_channels.push_back(data_channel_it->first); |
| 3893 | } |
| 3894 | |
| 3895 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 3896 | } |
| 3897 | |
| 3898 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 3899 | const cricket::StreamParamsVec& streams) { |
| 3900 | std::vector<std::string> existing_channels; |
| 3901 | |
| 3902 | // Find new and active data channels. |
| 3903 | for (const cricket::StreamParams& params : streams) { |
| 3904 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 3905 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 3906 | std::string label = params.sync_label.empty() |
| 3907 | ? rtc::ToString(params.first_ssrc()) |
| 3908 | : params.sync_label; |
| 3909 | auto data_channel_it = rtp_data_channels_.find(label); |
| 3910 | if (data_channel_it == rtp_data_channels_.end()) { |
| 3911 | // This is a new data channel. |
| 3912 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 3913 | } else { |
| 3914 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 3915 | } |
| 3916 | existing_channels.push_back(label); |
| 3917 | } |
| 3918 | |
| 3919 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 3920 | } |
| 3921 | |
| 3922 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 3923 | const std::vector<std::string>& active_channels, |
| 3924 | bool is_local_update) { |
| 3925 | auto it = rtp_data_channels_.begin(); |
| 3926 | while (it != rtp_data_channels_.end()) { |
| 3927 | DataChannel* data_channel = it->second; |
| 3928 | if (std::find(active_channels.begin(), active_channels.end(), |
| 3929 | data_channel->label()) != active_channels.end()) { |
| 3930 | ++it; |
| 3931 | continue; |
| 3932 | } |
| 3933 | |
| 3934 | if (is_local_update) { |
| 3935 | data_channel->SetSendSsrc(0); |
| 3936 | } else { |
| 3937 | data_channel->RemotePeerRequestClose(); |
| 3938 | } |
| 3939 | |
| 3940 | if (data_channel->state() == DataChannel::kClosed) { |
| 3941 | rtp_data_channels_.erase(it); |
| 3942 | it = rtp_data_channels_.begin(); |
| 3943 | } else { |
| 3944 | ++it; |
| 3945 | } |
| 3946 | } |
| 3947 | } |
| 3948 | |
| 3949 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 3950 | uint32_t remote_ssrc) { |
| 3951 | rtc::scoped_refptr<DataChannel> channel( |
| 3952 | InternalCreateDataChannel(label, nullptr)); |
| 3953 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3954 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 3955 | << "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3956 | return; |
| 3957 | } |
| 3958 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 3959 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 3960 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3961 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 3965 | const std::string& label, |
| 3966 | const InternalDataChannelInit* config) { |
| 3967 | if (IsClosed()) { |
| 3968 | return nullptr; |
| 3969 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3970 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3971 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3972 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 3973 | return nullptr; |
| 3974 | } |
| 3975 | InternalDataChannelInit new_config = |
| 3976 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3977 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3978 | if (new_config.id < 0) { |
| 3979 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3980 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3981 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3982 | RTC_LOG(LS_ERROR) |
| 3983 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3984 | return nullptr; |
| 3985 | } |
| 3986 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3987 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 3988 | << "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3989 | return nullptr; |
| 3990 | } |
| 3991 | } |
| 3992 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3993 | rtc::scoped_refptr<DataChannel> channel( |
| 3994 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3995 | if (!channel) { |
| 3996 | sid_allocator_.ReleaseSid(new_config.id); |
| 3997 | return nullptr; |
| 3998 | } |
| 3999 | |
| 4000 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4001 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4002 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4003 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4004 | return nullptr; |
| 4005 | } |
| 4006 | rtp_data_channels_[channel->label()] = channel; |
| 4007 | } else { |
| 4008 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4009 | sctp_data_channels_.push_back(channel); |
| 4010 | channel->SignalClosed.connect(this, |
| 4011 | &PeerConnection::OnSctpDataChannelClosed); |
| 4012 | } |
| 4013 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4014 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4015 | return channel; |
| 4016 | } |
| 4017 | |
| 4018 | bool PeerConnection::HasDataChannels() const { |
| 4019 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4020 | } |
| 4021 | |
| 4022 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4023 | for (const auto& channel : sctp_data_channels_) { |
| 4024 | if (channel->id() < 0) { |
| 4025 | int sid; |
| 4026 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4027 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4028 | continue; |
| 4029 | } |
| 4030 | channel->SetSctpSid(sid); |
| 4031 | } |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4036 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4037 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4038 | ++it) { |
| 4039 | if (it->get() == channel) { |
| 4040 | if (channel->id() >= 0) { |
| 4041 | sid_allocator_.ReleaseSid(channel->id()); |
| 4042 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4043 | // Since this method is triggered by a signal from the DataChannel, |
| 4044 | // we can't free it directly here; we need to free it asynchronously. |
| 4045 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4046 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 4047 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4048 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4049 | return; |
| 4050 | } |
| 4051 | } |
| 4052 | } |
| 4053 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4054 | void PeerConnection::OnDataChannelDestroyed() { |
| 4055 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4056 | // DataChannel may callback to us and try to modify the list. |
| 4057 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4058 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4059 | for (const auto& kv : temp_rtp_dcs) { |
| 4060 | kv.second->OnTransportChannelDestroyed(); |
| 4061 | } |
| 4062 | |
| 4063 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4064 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4065 | for (const auto& channel : temp_sctp_dcs) { |
| 4066 | channel->OnTransportChannelDestroyed(); |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | void PeerConnection::OnDataChannelOpenMessage( |
| 4071 | const std::string& label, |
| 4072 | const InternalDataChannelInit& config) { |
| 4073 | rtc::scoped_refptr<DataChannel> channel( |
| 4074 | InternalCreateDataChannel(label, &config)); |
| 4075 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4076 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4077 | return; |
| 4078 | } |
| 4079 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4080 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4081 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4082 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4085 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4086 | PeerConnection::GetAudioTransceiver() const { |
| 4087 | // This method only works with Plan B SDP, where there is a single |
| 4088 | // audio/video transceiver. |
| 4089 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4090 | for (auto transceiver : transceivers_) { |
| 4091 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4092 | return transceiver; |
| 4093 | } |
| 4094 | } |
| 4095 | RTC_NOTREACHED(); |
| 4096 | return nullptr; |
| 4097 | } |
| 4098 | |
| 4099 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4100 | PeerConnection::GetVideoTransceiver() const { |
| 4101 | // This method only works with Plan B SDP, where there is a single |
| 4102 | // audio/video transceiver. |
| 4103 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4104 | for (auto transceiver : transceivers_) { |
| 4105 | if (transceiver->internal()->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 4106 | return transceiver; |
| 4107 | } |
| 4108 | } |
| 4109 | RTC_NOTREACHED(); |
| 4110 | return nullptr; |
| 4111 | } |
| 4112 | |
| 4113 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4114 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4115 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4116 | switch (type) { |
| 4117 | case cricket::MEDIA_TYPE_AUDIO: |
| 4118 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4119 | case cricket::MEDIA_TYPE_VIDEO: |
| 4120 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4121 | case cricket::MEDIA_TYPE_DATA: |
| 4122 | return false; |
| 4123 | } |
| 4124 | RTC_NOTREACHED(); |
| 4125 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4128 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4129 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4130 | for (auto transceiver : transceivers_) { |
| 4131 | for (auto sender : transceiver->internal()->senders()) { |
| 4132 | if (sender->track() == track) { |
| 4133 | return sender; |
| 4134 | } |
| 4135 | } |
| 4136 | } |
| 4137 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4138 | } |
| 4139 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4140 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4141 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4142 | for (auto transceiver : transceivers_) { |
| 4143 | for (auto sender : transceiver->internal()->senders()) { |
| 4144 | if (sender->id() == sender_id) { |
| 4145 | return sender; |
| 4146 | } |
| 4147 | } |
| 4148 | } |
| 4149 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4152 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4153 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4154 | for (auto transceiver : transceivers_) { |
| 4155 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4156 | if (receiver->id() == receiver_id) { |
| 4157 | return receiver; |
| 4158 | } |
| 4159 | } |
| 4160 | } |
| 4161 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4162 | } |
| 4163 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4164 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4165 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4166 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4167 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4168 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4169 | ? &remote_audio_sender_infos_ |
| 4170 | : &remote_video_sender_infos_; |
| 4171 | } |
| 4172 | |
| 4173 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4174 | cricket::MediaType media_type) { |
| 4175 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4176 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4177 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4178 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4181 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4182 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4183 | const std::string& stream_label, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4184 | const std::string sender_id) const { |
| 4185 | for (const RtpSenderInfo& sender_info : infos) { |
| 4186 | if (sender_info.stream_label == stream_label && |
| 4187 | sender_info.sender_id == sender_id) { |
| 4188 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | return nullptr; |
| 4192 | } |
| 4193 | |
| 4194 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4195 | for (const auto& channel : sctp_data_channels_) { |
| 4196 | if (channel->id() == sid) { |
| 4197 | return channel; |
| 4198 | } |
| 4199 | } |
| 4200 | return nullptr; |
| 4201 | } |
| 4202 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4203 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4204 | const RTCConfiguration& configuration) { |
| 4205 | cricket::ServerAddresses stun_servers; |
| 4206 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4207 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 4208 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4209 | return false; |
| 4210 | } |
| 4211 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4212 | port_allocator_->Initialize(); |
| 4213 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4214 | // To handle both internal and externally created port allocator, we will |
| 4215 | // enable BUNDLE here. |
| 4216 | int portallocator_flags = port_allocator_->flags(); |
| 4217 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4218 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4219 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4220 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4221 | // by experiment. |
| 4222 | if (configuration.disable_ipv6) { |
| 4223 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4224 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4225 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4226 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 4227 | } |
| 4228 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4229 | if (configuration.disable_ipv6_on_wifi) { |
| 4230 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4231 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4232 | } |
| 4233 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4234 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 4235 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4236 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4239 | if (configuration.candidate_network_policy == |
| 4240 | kCandidateNetworkPolicyLowCost) { |
| 4241 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4242 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4243 | } |
| 4244 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame^] | 4245 | if (configuration.disable_link_local_networks) { |
| 4246 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
| 4247 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4248 | } |
| 4249 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4250 | port_allocator_->set_flags(portallocator_flags); |
| 4251 | // No step delay is used while allocating ports. |
| 4252 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4253 | port_allocator_->set_candidate_filter( |
| 4254 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4255 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4256 | |
| 4257 | // Call this last since it may create pooled allocator sessions using the |
| 4258 | // properties set above. |
| 4259 | port_allocator_->SetConfiguration(stun_servers, turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 4260 | configuration.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4261 | configuration.prune_turn_ports, |
| 4262 | configuration.turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4263 | return true; |
| 4264 | } |
| 4265 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4266 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4267 | const cricket::ServerAddresses& stun_servers, |
| 4268 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4269 | IceTransportsType type, |
| 4270 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4271 | bool prune_turn_ports, |
| 4272 | webrtc::TurnCustomizer* turn_customizer) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4273 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4274 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4275 | // Call this last since it may create pooled allocator sessions using the |
| 4276 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4277 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4278 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
| 4279 | turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4282 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4283 | return factory_->channel_manager(); |
| 4284 | } |
| 4285 | |
| 4286 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 4287 | return uma_observer_; |
| 4288 | } |
| 4289 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4290 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4291 | std::unique_ptr<RtcEventLogOutput> output, |
| 4292 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4293 | if (!event_log_) { |
| 4294 | return false; |
| 4295 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4296 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4300 | if (event_log_) { |
| 4301 | event_log_->StopLogging(); |
| 4302 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4303 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4304 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4305 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4306 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4307 | for (auto transceiver : transceivers_) { |
| 4308 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4309 | if (channel && channel->content_name() == content_name) { |
| 4310 | return channel; |
| 4311 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4312 | } |
| 4313 | if (rtp_data_channel() && |
| 4314 | rtp_data_channel()->content_name() == content_name) { |
| 4315 | return rtp_data_channel(); |
| 4316 | } |
| 4317 | return nullptr; |
| 4318 | } |
| 4319 | |
| 4320 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4321 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4322 | RTC_LOG(LS_INFO) |
| 4323 | << "Local and Remote descriptions must be applied to get the " |
| 4324 | << "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4325 | return false; |
| 4326 | } |
| 4327 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4328 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
| 4329 | << "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4330 | return false; |
| 4331 | } |
| 4332 | |
| 4333 | return transport_controller_->GetSslRole(*sctp_transport_name_, role); |
| 4334 | } |
| 4335 | |
| 4336 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4337 | rtc::SSLRole* role) { |
| 4338 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4339 | RTC_LOG(LS_INFO) |
| 4340 | << "Local and Remote descriptions must be applied to get the " |
| 4341 | << "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4342 | return false; |
| 4343 | } |
| 4344 | |
| 4345 | return transport_controller_->GetSslRole(GetTransportName(content_name), |
| 4346 | role); |
| 4347 | } |
| 4348 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4349 | // TODO(steveanton): Eventually it'd be nice to store the channels as a single |
| 4350 | // vector of BaseChannel pointers instead of separate voice and video channel |
| 4351 | // vectors. At that point, this will become a simple getter. |
| 4352 | std::vector<cricket::BaseChannel*> PeerConnection::Channels() const { |
| 4353 | std::vector<cricket::BaseChannel*> channels; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4354 | if (voice_channel()) { |
| 4355 | channels.push_back(voice_channel()); |
| 4356 | } |
| 4357 | if (video_channel()) { |
| 4358 | channels.push_back(video_channel()); |
| 4359 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4360 | if (rtp_data_channel_) { |
| 4361 | channels.push_back(rtp_data_channel_); |
| 4362 | } |
| 4363 | return channels; |
| 4364 | } |
| 4365 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4366 | void PeerConnection::SetSessionError(SessionError error, |
| 4367 | const std::string& error_desc) { |
| 4368 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4369 | if (error != session_error_) { |
| 4370 | session_error_ = error; |
| 4371 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4375 | RTCError PeerConnection::UpdateSessionState(SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4376 | cricket::ContentSource source) { |
| 4377 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4378 | |
| 4379 | // If there's already a pending error then no state transition should happen. |
| 4380 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4381 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4382 | |
| 4383 | // If this is an answer then we know whether to BUNDLE or not. If both the |
| 4384 | // local and remote side have agreed to BUNDLE, go ahead and enable it. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4385 | if (type == SdpType::kAnswer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4386 | const cricket::ContentGroup* local_bundle = |
| 4387 | local_description()->description()->GetGroupByName( |
| 4388 | cricket::GROUP_TYPE_BUNDLE); |
| 4389 | const cricket::ContentGroup* remote_bundle = |
| 4390 | remote_description()->description()->GetGroupByName( |
| 4391 | cricket::GROUP_TYPE_BUNDLE); |
| 4392 | if (local_bundle && remote_bundle) { |
| 4393 | // The answerer decides the transport to bundle on. |
| 4394 | const cricket::ContentGroup* answer_bundle = |
| 4395 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 4396 | if (!EnableBundle(*answer_bundle)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4397 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4398 | kEnableBundleFailed); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4399 | } |
| 4400 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4401 | } |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4402 | |
| 4403 | // Only push down the transport description after potentially enabling BUNDLE; |
| 4404 | // we don't want to push down a description on a transport about to be |
| 4405 | // destroyed. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4406 | RTCError error = PushdownTransportDescription(source, type); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4407 | if (!error.ok()) { |
| 4408 | return error; |
| 4409 | } |
| 4410 | |
| 4411 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4412 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4413 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | // Update the signaling state according to the specified state machine (see |
| 4417 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4418 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4419 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4420 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4421 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4422 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4423 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4424 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4425 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4426 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4427 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4428 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4429 | } |
| 4430 | |
| 4431 | // Update internal objects according to the session description's media |
| 4432 | // descriptions. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4433 | error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4434 | if (!error.ok()) { |
| 4435 | SetSessionError(SessionError::kContent, error.message()); |
| 4436 | } |
| 4437 | if (session_error() != SessionError::kNone) { |
| 4438 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 4439 | } |
| 4440 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4441 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4442 | } |
| 4443 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4444 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4445 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4446 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4447 | const SessionDescriptionInterface* sdesc = |
| 4448 | (source == cricket::CS_LOCAL ? local_description() |
| 4449 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4450 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4451 | |
| 4452 | // Push down the new SDP media section for each audio/video transceiver. |
| 4453 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4454 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4455 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4456 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4457 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4458 | continue; |
| 4459 | } |
| 4460 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4461 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4462 | if (!content_desc) { |
| 4463 | continue; |
| 4464 | } |
| 4465 | std::string error; |
| 4466 | bool success = |
| 4467 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4468 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4469 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4470 | if (!success) { |
| 4471 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4476 | if (rtp_data_channel_) { |
| 4477 | const ContentInfo* data_content = |
| 4478 | cricket::GetFirstDataContent(sdesc->description()); |
| 4479 | if (data_content && !data_content->rejected) { |
| 4480 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4481 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4482 | if (data_desc) { |
| 4483 | std::string error; |
| 4484 | bool success = |
| 4485 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4486 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
| 4487 | : rtp_data_channel_->SetRemoteContent(data_desc, type, |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4488 | &error); |
| 4489 | if (!success) { |
| 4490 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4491 | std::move(error)); |
| 4492 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4493 | } |
| 4494 | } |
| 4495 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4496 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4497 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4498 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4499 | if (sctp_transport_ && local_description() && remote_description() && |
| 4500 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4501 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4502 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4503 | RTC_FROM_HERE, |
| 4504 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4505 | if (!success) { |
| 4506 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4507 | "Failed to push down SCTP parameters."); |
| 4508 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4509 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4510 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4511 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4515 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4516 | RTC_DCHECK(local_description()); |
| 4517 | RTC_DCHECK(remote_description()); |
| 4518 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4519 | // When we support "max-message-size", that would also be pushed down here. |
| 4520 | return sctp_transport_->Start( |
| 4521 | GetSctpPort(local_description()->description()), |
| 4522 | GetSctpPort(remote_description()->description())); |
| 4523 | } |
| 4524 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4525 | RTCError PeerConnection::PushdownTransportDescription( |
| 4526 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4527 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4528 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4529 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4530 | const SessionDescriptionInterface* sdesc = |
| 4531 | (source == cricket::CS_LOCAL ? local_description() |
| 4532 | : remote_description()); |
| 4533 | RTC_DCHECK(sdesc); |
| 4534 | for (const cricket::TransportInfo& tinfo : |
| 4535 | sdesc->description()->transport_infos()) { |
| 4536 | std::string error; |
| 4537 | bool success; |
| 4538 | if (source == cricket::CS_LOCAL) { |
| 4539 | success = transport_controller_->SetLocalTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4540 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4541 | } else { |
| 4542 | success = transport_controller_->SetRemoteTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4543 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4544 | } |
| 4545 | if (!success) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4546 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4547 | "Failed to push down transport description for " + |
| 4548 | tinfo.content_name + ": " + error); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4549 | } |
| 4550 | } |
| 4551 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4552 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | bool PeerConnection::GetTransportDescription( |
| 4556 | const SessionDescription* description, |
| 4557 | const std::string& content_name, |
| 4558 | cricket::TransportDescription* tdesc) { |
| 4559 | if (!description || !tdesc) { |
| 4560 | return false; |
| 4561 | } |
| 4562 | const TransportInfo* transport_info = |
| 4563 | description->GetTransportInfoByName(content_name); |
| 4564 | if (!transport_info) { |
| 4565 | return false; |
| 4566 | } |
| 4567 | *tdesc = transport_info->description; |
| 4568 | return true; |
| 4569 | } |
| 4570 | |
| 4571 | bool PeerConnection::EnableBundle(const cricket::ContentGroup& bundle) { |
| 4572 | const std::string* first_content_name = bundle.FirstContentName(); |
| 4573 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4574 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4575 | return false; |
| 4576 | } |
| 4577 | const std::string& transport_name = *first_content_name; |
| 4578 | |
| 4579 | auto maybe_set_transport = [this, bundle, |
| 4580 | transport_name](cricket::BaseChannel* ch) { |
| 4581 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4582 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | std::string old_transport_name = ch->transport_name(); |
| 4586 | if (old_transport_name == transport_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4587 | RTC_LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 4588 | << " on " << transport_name << "."; |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4589 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4593 | transport_controller_->CreateDtlsTransport( |
| 4594 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4595 | bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr); |
| 4596 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4597 | if (need_rtcp) { |
| 4598 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4599 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4600 | } |
| 4601 | |
| 4602 | ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4603 | RTC_LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 4604 | << transport_name << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4605 | transport_controller_->DestroyDtlsTransport( |
| 4606 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4607 | // If the channel needs rtcp, it means that the channel used to have a |
| 4608 | // rtcp transport which needs to be deleted now. |
| 4609 | if (need_rtcp) { |
| 4610 | transport_controller_->DestroyDtlsTransport( |
| 4611 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4612 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4613 | }; |
| 4614 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4615 | for (auto transceiver : transceivers_) { |
| 4616 | maybe_set_transport(transceiver->internal()->channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4617 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4618 | maybe_set_transport(rtp_data_channel_); |
| 4619 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4620 | // For SCTP, transport creation/deletion happens here instead of in the |
| 4621 | // object itself. |
| 4622 | if (sctp_transport_) { |
| 4623 | RTC_DCHECK(sctp_transport_name_); |
| 4624 | RTC_DCHECK(sctp_content_name_); |
| 4625 | if (transport_name != *sctp_transport_name_ && |
| 4626 | bundle.HasContentName(*sctp_content_name_)) { |
| 4627 | network_thread()->Invoke<void>( |
| 4628 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::ChangeSctpTransport_n, this, |
| 4629 | transport_name)); |
| 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | return true; |
| 4634 | } |
| 4635 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4636 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4637 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4638 | cricket::ContinualGatheringPolicy gathering_policy; |
| 4639 | // TODO(honghaiz): Add the third continual gathering policy in |
| 4640 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 4641 | switch (config.continual_gathering_policy) { |
| 4642 | case PeerConnectionInterface::GATHER_ONCE: |
| 4643 | gathering_policy = cricket::GATHER_ONCE; |
| 4644 | break; |
| 4645 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4646 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4647 | break; |
| 4648 | default: |
| 4649 | RTC_NOTREACHED(); |
| 4650 | gathering_policy = cricket::GATHER_ONCE; |
| 4651 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4652 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4653 | cricket::IceConfig ice_config; |
| 4654 | ice_config.receiving_timeout = config.ice_connection_receiving_timeout; |
| 4655 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4656 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4657 | ice_config.backup_connection_ping_interval = |
| 4658 | config.ice_backup_candidate_pair_ping_interval; |
| 4659 | ice_config.continual_gathering_policy = gathering_policy; |
| 4660 | ice_config.presume_writable_when_fully_relayed = |
| 4661 | config.presume_writable_when_fully_relayed; |
| 4662 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
| 4663 | ice_config.regather_all_networks_interval_range = |
| 4664 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4665 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4666 | return ice_config; |
| 4667 | } |
| 4668 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4669 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4670 | std::string* track_id) { |
| 4671 | if (!local_description()) { |
| 4672 | return false; |
| 4673 | } |
| 4674 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4675 | track_id); |
| 4676 | } |
| 4677 | |
| 4678 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4679 | std::string* track_id) { |
| 4680 | if (!remote_description()) { |
| 4681 | return false; |
| 4682 | } |
| 4683 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 4684 | track_id); |
| 4685 | } |
| 4686 | |
| 4687 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 4688 | const rtc::CopyOnWriteBuffer& payload, |
| 4689 | cricket::SendDataResult* result) { |
| 4690 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4691 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
| 4692 | << "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4693 | return false; |
| 4694 | } |
| 4695 | return rtp_data_channel_ |
| 4696 | ? rtp_data_channel_->SendData(params, payload, result) |
| 4697 | : network_thread()->Invoke<bool>( |
| 4698 | RTC_FROM_HERE, |
| 4699 | Bind(&cricket::SctpTransportInternal::SendData, |
| 4700 | sctp_transport_.get(), params, payload, result)); |
| 4701 | } |
| 4702 | |
| 4703 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4704 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 4705 | // Don't log an error here, because DataChannels are expected to call |
| 4706 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 4707 | // whether or not the underlying transport is ready. |
| 4708 | return false; |
| 4709 | } |
| 4710 | if (rtp_data_channel_) { |
| 4711 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 4712 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 4713 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 4714 | &DataChannel::OnDataReceived); |
| 4715 | } else { |
| 4716 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 4717 | &DataChannel::OnChannelReady); |
| 4718 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 4719 | &DataChannel::OnDataReceived); |
| 4720 | SignalSctpStreamClosedRemotely.connect( |
| 4721 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 4722 | } |
| 4723 | return true; |
| 4724 | } |
| 4725 | |
| 4726 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4727 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4728 | RTC_LOG(LS_ERROR) |
| 4729 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 4730 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4731 | return; |
| 4732 | } |
| 4733 | if (rtp_data_channel_) { |
| 4734 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 4735 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 4736 | } else { |
| 4737 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 4738 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 4739 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | void PeerConnection::AddSctpDataStream(int sid) { |
| 4744 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4745 | RTC_LOG(LS_ERROR) |
| 4746 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4747 | return; |
| 4748 | } |
| 4749 | network_thread()->Invoke<void>( |
| 4750 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 4751 | sctp_transport_.get(), sid)); |
| 4752 | } |
| 4753 | |
| 4754 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 4755 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4756 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
| 4757 | << "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4758 | return; |
| 4759 | } |
| 4760 | network_thread()->Invoke<void>( |
| 4761 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 4762 | sctp_transport_.get(), sid)); |
| 4763 | } |
| 4764 | |
| 4765 | bool PeerConnection::ReadyToSendData() const { |
| 4766 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 4767 | sctp_ready_to_send_data_; |
| 4768 | } |
| 4769 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4770 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 4771 | const { |
| 4772 | std::map<std::string, std::string> transport_names_by_mid; |
| 4773 | for (auto transceiver : transceivers_) { |
| 4774 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4775 | if (channel) { |
| 4776 | transport_names_by_mid[channel->content_name()] = |
| 4777 | channel->transport_name(); |
| 4778 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4779 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4780 | if (rtp_data_channel_) { |
| 4781 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 4782 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4783 | } |
| 4784 | if (sctp_transport_) { |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4785 | transport_names_by_mid[*sctp_content_name_] = *sctp_transport_name_; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4786 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4787 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4788 | } |
| 4789 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4790 | std::map<std::string, cricket::TransportStats> |
| 4791 | PeerConnection::GetTransportStatsByNames( |
| 4792 | const std::set<std::string>& transport_names) { |
| 4793 | if (!network_thread()->IsCurrent()) { |
| 4794 | return network_thread() |
| 4795 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 4796 | RTC_FROM_HERE, |
| 4797 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4798 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4799 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 4800 | for (const std::string& transport_name : transport_names) { |
| 4801 | cricket::TransportStats transport_stats; |
| 4802 | bool success = |
| 4803 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 4804 | if (success) { |
| 4805 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 4806 | } else { |
| 4807 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 4808 | << transport_name; |
| 4809 | } |
| 4810 | } |
| 4811 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | bool PeerConnection::GetLocalCertificate( |
| 4815 | const std::string& transport_name, |
| 4816 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 4817 | return transport_controller_->GetLocalCertificate(transport_name, |
| 4818 | certificate); |
| 4819 | } |
| 4820 | |
| 4821 | std::unique_ptr<rtc::SSLCertificate> PeerConnection::GetRemoteSSLCertificate( |
| 4822 | const std::string& transport_name) { |
| 4823 | return transport_controller_->GetRemoteSSLCertificate(transport_name); |
| 4824 | } |
| 4825 | |
| 4826 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 4827 | return data_channel_type_; |
| 4828 | } |
| 4829 | |
| 4830 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 4831 | return pending_ice_restarts_.find(content_name) != |
| 4832 | pending_ice_restarts_.end(); |
| 4833 | } |
| 4834 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4835 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 4836 | return transport_controller_->NeedsIceRestart(content_name); |
| 4837 | } |
| 4838 | |
| 4839 | void PeerConnection::OnCertificateReady( |
| 4840 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 4841 | transport_controller_->SetLocalCertificate(certificate); |
| 4842 | } |
| 4843 | |
| 4844 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4845 | SetSessionError(SessionError::kTransport, |
| 4846 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | void PeerConnection::OnTransportControllerConnectionState( |
| 4850 | cricket::IceConnectionState state) { |
| 4851 | switch (state) { |
| 4852 | case cricket::kIceConnectionConnecting: |
| 4853 | // If the current state is Connected or Completed, then there were |
| 4854 | // writable channels but now there are not, so the next state must |
| 4855 | // be Disconnected. |
| 4856 | // kIceConnectionConnecting is currently used as the default, |
| 4857 | // un-connected state by the TransportController, so its only use is |
| 4858 | // detecting disconnections. |
| 4859 | if (ice_connection_state_ == |
| 4860 | PeerConnectionInterface::kIceConnectionConnected || |
| 4861 | ice_connection_state_ == |
| 4862 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 4863 | SetIceConnectionState( |
| 4864 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 4865 | } |
| 4866 | break; |
| 4867 | case cricket::kIceConnectionFailed: |
| 4868 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 4869 | break; |
| 4870 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4871 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 4872 | << "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4873 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 4874 | break; |
| 4875 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4876 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 4877 | << "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4878 | if (ice_connection_state_ != |
| 4879 | PeerConnectionInterface::kIceConnectionConnected) { |
| 4880 | // If jumping directly from "checking" to "connected", |
| 4881 | // signal "connected" first. |
| 4882 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 4883 | } |
| 4884 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 4885 | if (metrics_observer()) { |
| 4886 | ReportTransportStats(); |
| 4887 | } |
| 4888 | break; |
| 4889 | default: |
| 4890 | RTC_NOTREACHED(); |
| 4891 | } |
| 4892 | } |
| 4893 | |
| 4894 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 4895 | const std::string& transport_name, |
| 4896 | const cricket::Candidates& candidates) { |
| 4897 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 4898 | int sdp_mline_index; |
| 4899 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4900 | RTC_LOG(LS_ERROR) |
| 4901 | << "OnTransportControllerCandidatesGathered: content name " |
| 4902 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4903 | return; |
| 4904 | } |
| 4905 | |
| 4906 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 4907 | citer != candidates.end(); ++citer) { |
| 4908 | // Use transport_name as the candidate media id. |
| 4909 | std::unique_ptr<JsepIceCandidate> candidate( |
| 4910 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 4911 | if (local_description()) { |
| 4912 | mutable_local_description()->AddCandidate(candidate.get()); |
| 4913 | } |
| 4914 | OnIceCandidate(std::move(candidate)); |
| 4915 | } |
| 4916 | } |
| 4917 | |
| 4918 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 4919 | const std::vector<cricket::Candidate>& candidates) { |
| 4920 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 4921 | // Sanity check. |
| 4922 | for (const cricket::Candidate& candidate : candidates) { |
| 4923 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4924 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
| 4925 | << "empty content name in candidate " |
| 4926 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4927 | return; |
| 4928 | } |
| 4929 | } |
| 4930 | |
| 4931 | if (local_description()) { |
| 4932 | mutable_local_description()->RemoveCandidates(candidates); |
| 4933 | } |
| 4934 | OnIceCandidatesRemoved(candidates); |
| 4935 | } |
| 4936 | |
| 4937 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 4938 | rtc::SSLHandshakeError error) { |
| 4939 | if (metrics_observer()) { |
| 4940 | metrics_observer()->IncrementEnumCounter( |
| 4941 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 4942 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 4943 | } |
| 4944 | } |
| 4945 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4946 | void PeerConnection::EnableSending() { |
| 4947 | for (auto transceiver : transceivers_) { |
| 4948 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4949 | if (channel && !channel->enabled()) { |
| 4950 | channel->Enable(true); |
| 4951 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4952 | } |
| 4953 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4954 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4955 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4956 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | // Returns the media index for a local ice candidate given the content name. |
| 4960 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 4961 | const std::string& content_name, |
| 4962 | int* sdp_mline_index) { |
| 4963 | if (!local_description() || !sdp_mline_index) { |
| 4964 | return false; |
| 4965 | } |
| 4966 | |
| 4967 | bool content_found = false; |
| 4968 | const ContentInfos& contents = local_description()->description()->contents(); |
| 4969 | for (size_t index = 0; index < contents.size(); ++index) { |
| 4970 | if (contents[index].name == content_name) { |
| 4971 | *sdp_mline_index = static_cast<int>(index); |
| 4972 | content_found = true; |
| 4973 | break; |
| 4974 | } |
| 4975 | } |
| 4976 | return content_found; |
| 4977 | } |
| 4978 | |
| 4979 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 4980 | const SessionDescriptionInterface* remote_desc) { |
| 4981 | if (!remote_desc) { |
| 4982 | return true; |
| 4983 | } |
| 4984 | bool ret = true; |
| 4985 | |
| 4986 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 4987 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 4988 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 4989 | const IceCandidateInterface* candidate = candidates->at(n); |
| 4990 | bool valid = false; |
| 4991 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 4992 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4993 | RTC_LOG(LS_INFO) |
| 4994 | << "UseCandidatesInSessionDescription: Not ready to use " |
| 4995 | << "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4996 | } |
| 4997 | continue; |
| 4998 | } |
| 4999 | ret = UseCandidate(candidate); |
| 5000 | if (!ret) { |
| 5001 | break; |
| 5002 | } |
| 5003 | } |
| 5004 | } |
| 5005 | return ret; |
| 5006 | } |
| 5007 | |
| 5008 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5009 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5010 | size_t remote_content_size = |
| 5011 | remote_description()->description()->contents().size(); |
| 5012 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5013 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5014 | return false; |
| 5015 | } |
| 5016 | |
| 5017 | cricket::ContentInfo content = |
| 5018 | remote_description()->description()->contents()[mediacontent_index]; |
| 5019 | std::vector<cricket::Candidate> candidates; |
| 5020 | candidates.push_back(candidate->candidate()); |
| 5021 | // Invoking BaseSession method to handle remote candidates. |
| 5022 | std::string error; |
| 5023 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 5024 | &error)) { |
| 5025 | // Candidates successfully submitted for checking. |
| 5026 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5027 | ice_connection_state_ == |
| 5028 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5029 | // If state is New, then the session has just gotten its first remote ICE |
| 5030 | // candidates, so go to Checking. |
| 5031 | // If state is Disconnected, the session is re-using old candidates or |
| 5032 | // receiving additional ones, so go to Checking. |
| 5033 | // If state is Connected, stay Connected. |
| 5034 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5035 | // newly added transport, then the state actually _should_ move to |
| 5036 | // checking. Add a way to distinguish that case. |
| 5037 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5038 | } |
| 5039 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 5040 | } else { |
| 5041 | if (!error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5042 | RTC_LOG(LS_WARNING) << error; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5043 | } |
| 5044 | } |
| 5045 | return true; |
| 5046 | } |
| 5047 | |
| 5048 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5049 | // Destroy video channel first since it may have a pointer to the |
| 5050 | // voice channel. |
| 5051 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5052 | if (!video_info || video_info->rejected) { |
| 5053 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5054 | } |
| 5055 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5056 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5057 | if (!audio_info || audio_info->rejected) { |
| 5058 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5062 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5063 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5064 | } |
| 5065 | } |
| 5066 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5067 | std::string PeerConnection::GetTransportNameForMediaSection( |
| 5068 | const std::string& mid, |
| 5069 | const cricket::ContentGroup* bundle_group) const { |
| 5070 | if (!bundle_group) { |
| 5071 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5072 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5073 | const std::string* first_content_name = bundle_group->FirstContentName(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5074 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5075 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5076 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5077 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5078 | if (!bundle_group->HasContentName(mid)) { |
| 5079 | RTC_LOG(LS_WARNING) << mid << " is not part of any bundle group"; |
| 5080 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5081 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5082 | RTC_LOG(LS_INFO) << "Bundling " << mid << " on " << *first_content_name; |
| 5083 | return *first_content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5084 | } |
| 5085 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5086 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5087 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5088 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5089 | if (configuration_.bundle_policy == |
| 5090 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5091 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5092 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5093 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5094 | "max-bundle configured but session description " |
| 5095 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5096 | } |
| 5097 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5098 | return std::move(bundle_group); |
| 5099 | } |
| 5100 | |
| 5101 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
| 5102 | auto bundle_group_or_error = GetEarlyBundleGroup(desc); |
| 5103 | if (!bundle_group_or_error.ok()) { |
| 5104 | return bundle_group_or_error.MoveError(); |
| 5105 | } |
| 5106 | const cricket::ContentGroup* bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5107 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5108 | // Creating the media channels and transport proxies. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5109 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5110 | if (voice && !voice->rejected && |
| 5111 | !GetAudioTransceiver()->internal()->channel()) { |
| 5112 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel( |
| 5113 | voice->name, |
| 5114 | GetTransportNameForMediaSection(voice->name, bundle_group)); |
| 5115 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5116 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5117 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5118 | } |
| 5119 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5120 | } |
| 5121 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5122 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5123 | if (video && !video->rejected && |
| 5124 | !GetVideoTransceiver()->internal()->channel()) { |
| 5125 | cricket::VideoChannel* video_channel = CreateVideoChannel( |
| 5126 | video->name, |
| 5127 | GetTransportNameForMediaSection(video->name, bundle_group)); |
| 5128 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5129 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5130 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5131 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5132 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5133 | } |
| 5134 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5135 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5136 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5137 | !rtp_data_channel_ && !sctp_transport_) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5138 | if (!CreateDataChannel(data->name, GetTransportNameForMediaSection( |
| 5139 | data->name, bundle_group))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5140 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5141 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5142 | } |
| 5143 | } |
| 5144 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5145 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5146 | } |
| 5147 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5148 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5149 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
| 5150 | const std::string& mid, |
| 5151 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5152 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5153 | transport_controller_->CreateDtlsTransport( |
| 5154 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5155 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5156 | if (configuration_.rtcp_mux_policy != |
| 5157 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5158 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5159 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5160 | } |
| 5161 | |
| 5162 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
| 5163 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5164 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5165 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5166 | if (!voice_channel) { |
| 5167 | transport_controller_->DestroyDtlsTransport( |
| 5168 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5169 | if (rtcp_dtls_transport) { |
| 5170 | transport_controller_->DestroyDtlsTransport( |
| 5171 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5172 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5173 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5174 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5175 | voice_channel->SignalRtcpMuxFullyActive.connect( |
| 5176 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5177 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5178 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5179 | voice_channel->SignalSentPacket.connect(this, |
| 5180 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5181 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5182 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5183 | } |
| 5184 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5185 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5186 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
| 5187 | const std::string& mid, |
| 5188 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5189 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5190 | transport_controller_->CreateDtlsTransport( |
| 5191 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5192 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5193 | if (configuration_.rtcp_mux_policy != |
| 5194 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5195 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5196 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5197 | } |
| 5198 | |
| 5199 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
| 5200 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5201 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5202 | video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5203 | |
| 5204 | if (!video_channel) { |
| 5205 | transport_controller_->DestroyDtlsTransport( |
| 5206 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5207 | if (rtcp_dtls_transport) { |
| 5208 | transport_controller_->DestroyDtlsTransport( |
| 5209 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5210 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5211 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5212 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5213 | video_channel->SignalRtcpMuxFullyActive.connect( |
| 5214 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5215 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5216 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5217 | video_channel->SignalSentPacket.connect(this, |
| 5218 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5219 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5220 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5221 | } |
| 5222 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5223 | bool PeerConnection::CreateDataChannel(const std::string& mid, |
| 5224 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5225 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5226 | if (sctp) { |
| 5227 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5228 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5229 | << "Trying to create SCTP transport, but didn't compile with " |
| 5230 | "SCTP support (HAVE_SCTP)"; |
| 5231 | return false; |
| 5232 | } |
| 5233 | if (!network_thread()->Invoke<bool>( |
| 5234 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::CreateSctpTransport_n, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5235 | this, mid, transport_name))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5236 | return false; |
| 5237 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5238 | for (const auto& channel : sctp_data_channels_) { |
| 5239 | channel->OnTransportChannelCreated(); |
| 5240 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5241 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5242 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5243 | transport_controller_->CreateDtlsTransport( |
| 5244 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5245 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5246 | if (configuration_.rtcp_mux_policy != |
| 5247 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5248 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5249 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5250 | } |
| 5251 | |
| 5252 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 5253 | configuration_.media_config, rtp_dtls_transport, rtcp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5254 | signaling_thread(), mid, SrtpRequired()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5255 | |
| 5256 | if (!rtp_data_channel_) { |
| 5257 | transport_controller_->DestroyDtlsTransport( |
| 5258 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5259 | if (rtcp_dtls_transport) { |
| 5260 | transport_controller_->DestroyDtlsTransport( |
| 5261 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5262 | } |
| 5263 | return false; |
| 5264 | } |
| 5265 | |
| 5266 | rtp_data_channel_->SignalRtcpMuxFullyActive.connect( |
| 5267 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5268 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5269 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5270 | rtp_data_channel_->SignalSentPacket.connect( |
| 5271 | this, &PeerConnection::OnSentPacket_w); |
| 5272 | } |
| 5273 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5274 | return true; |
| 5275 | } |
| 5276 | |
| 5277 | Call::Stats PeerConnection::GetCallStats() { |
| 5278 | if (!worker_thread()->IsCurrent()) { |
| 5279 | return worker_thread()->Invoke<Call::Stats>( |
| 5280 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5281 | } |
| 5282 | if (call_) { |
| 5283 | return call_->GetStats(); |
| 5284 | } else { |
| 5285 | return Call::Stats(); |
| 5286 | } |
| 5287 | } |
| 5288 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5289 | bool PeerConnection::CreateSctpTransport_n(const std::string& content_name, |
| 5290 | const std::string& transport_name) { |
| 5291 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5292 | RTC_DCHECK(sctp_factory_); |
| 5293 | cricket::DtlsTransportInternal* tc = |
| 5294 | transport_controller_->CreateDtlsTransport_n( |
| 5295 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5296 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 5297 | RTC_DCHECK(sctp_transport_); |
| 5298 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5299 | sctp_transport_->SignalReadyToSendData.connect( |
| 5300 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5301 | sctp_transport_->SignalDataReceived.connect( |
| 5302 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 5303 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 5304 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5305 | sctp_transport_name_ = transport_name; |
| 5306 | sctp_content_name_ = content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5307 | return true; |
| 5308 | } |
| 5309 | |
| 5310 | void PeerConnection::ChangeSctpTransport_n(const std::string& transport_name) { |
| 5311 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5312 | RTC_DCHECK(sctp_transport_); |
| 5313 | RTC_DCHECK(sctp_transport_name_); |
| 5314 | std::string old_sctp_transport_name = *sctp_transport_name_; |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5315 | sctp_transport_name_ = transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5316 | cricket::DtlsTransportInternal* tc = |
| 5317 | transport_controller_->CreateDtlsTransport_n( |
| 5318 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5319 | sctp_transport_->SetTransportChannel(tc); |
| 5320 | transport_controller_->DestroyDtlsTransport_n( |
| 5321 | old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5322 | } |
| 5323 | |
| 5324 | void PeerConnection::DestroySctpTransport_n() { |
| 5325 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5326 | sctp_transport_.reset(nullptr); |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 5327 | transport_controller_->DestroyDtlsTransport_n( |
| 5328 | *sctp_transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5329 | sctp_content_name_.reset(); |
| 5330 | sctp_transport_name_.reset(); |
| 5331 | sctp_invoker_.reset(nullptr); |
| 5332 | sctp_ready_to_send_data_ = false; |
| 5333 | } |
| 5334 | |
| 5335 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5336 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5337 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5338 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5339 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5340 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5341 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5342 | // pending tasks will be cleared. |
| 5343 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5344 | OnSctpTransportReadyToSendData_s(true); |
| 5345 | }); |
| 5346 | } |
| 5347 | |
| 5348 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5349 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5350 | sctp_ready_to_send_data_ = ready; |
| 5351 | SignalSctpReadyToSendData(ready); |
| 5352 | } |
| 5353 | |
| 5354 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5355 | const cricket::ReceiveDataParams& params, |
| 5356 | const rtc::CopyOnWriteBuffer& payload) { |
| 5357 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5358 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5359 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5360 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5361 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5362 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5363 | // pending tasks will be cleared. |
| 5364 | sctp_invoker_->AsyncInvoke<void>( |
| 5365 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5366 | OnSctpTransportDataReceived_s(params, payload); |
| 5367 | }); |
| 5368 | } |
| 5369 | |
| 5370 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5371 | const cricket::ReceiveDataParams& params, |
| 5372 | const rtc::CopyOnWriteBuffer& payload) { |
| 5373 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5374 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5375 | // Received OPEN message; parse and signal that a new data channel should |
| 5376 | // be created. |
| 5377 | std::string label; |
| 5378 | InternalDataChannelInit config; |
| 5379 | config.id = params.ssrc; |
| 5380 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5381 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5382 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5383 | return; |
| 5384 | } |
| 5385 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5386 | OnDataChannelOpenMessage(label, config); |
| 5387 | } else { |
| 5388 | // Otherwise just forward the signal. |
| 5389 | SignalSctpDataReceived(params, payload); |
| 5390 | } |
| 5391 | } |
| 5392 | |
| 5393 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 5394 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5395 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5396 | sctp_invoker_->AsyncInvoke<void>( |
| 5397 | RTC_FROM_HERE, signaling_thread(), |
| 5398 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5399 | &SignalSctpStreamClosedRemotely, sid)); |
| 5400 | } |
| 5401 | |
| 5402 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5403 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5404 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5405 | if (!bundle_enabled) |
| 5406 | return true; |
| 5407 | |
| 5408 | const cricket::ContentGroup* bundle_group = |
| 5409 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5410 | RTC_DCHECK(bundle_group != NULL); |
| 5411 | |
| 5412 | const cricket::ContentInfos& contents = desc->contents(); |
| 5413 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5414 | citer != contents.end(); ++citer) { |
| 5415 | const cricket::ContentInfo* content = (&*citer); |
| 5416 | RTC_DCHECK(content != NULL); |
| 5417 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5418 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5419 | if (!HasRtcpMuxEnabled(content)) |
| 5420 | return false; |
| 5421 | } |
| 5422 | } |
| 5423 | // RTCP-MUX is enabled in all the contents. |
| 5424 | return true; |
| 5425 | } |
| 5426 | |
| 5427 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5428 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5429 | } |
| 5430 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5431 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5432 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5433 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5434 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5435 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5439 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5440 | } |
| 5441 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5442 | SdpType type = sdesc->GetType(); |
| 5443 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5444 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5445 | LOG_AND_RETURN_ERROR( |
| 5446 | RTCErrorType::INVALID_PARAMETER, |
| 5447 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | // Verify crypto settings. |
| 5451 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5452 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5453 | dtls_enabled_) { |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 5454 | RTCError crypto_error = |
| 5455 | VerifyCrypto(sdesc->description(), dtls_enabled_, uma_observer_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5456 | if (!crypto_error.ok()) { |
| 5457 | return crypto_error; |
| 5458 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | // Verify ice-ufrag and ice-pwd. |
| 5462 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5463 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5464 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5468 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5469 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5470 | } |
| 5471 | |
| 5472 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5473 | // m-lines that do not rtcp-mux enabled. |
| 5474 | |
| 5475 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5476 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5477 | const cricket::SessionDescription* offer_desc = |
| 5478 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5479 | : local_description()->description(); |
| 5480 | if (!MediaSectionsHaveSameCount(offer_desc, sdesc->description()) || |
| 5481 | !MediaSectionsInSameOrder(offer_desc, sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5482 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5483 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5484 | } |
| 5485 | } else { |
| 5486 | const cricket::SessionDescription* current_desc = nullptr; |
| 5487 | if (source == cricket::CS_LOCAL && local_description()) { |
| 5488 | current_desc = local_description()->description(); |
| 5489 | } else if (source == cricket::CS_REMOTE && remote_description()) { |
| 5490 | current_desc = remote_description()->description(); |
| 5491 | } |
| 5492 | // The re-offers should respect the order of m= sections in current |
| 5493 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
| 5494 | if (current_desc && |
| 5495 | !MediaSectionsInSameOrder(current_desc, sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5496 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5497 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5498 | } |
| 5499 | } |
| 5500 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5501 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5502 | } |
| 5503 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5504 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5505 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5506 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5507 | return (state == PeerConnectionInterface::kStable) || |
| 5508 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5509 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5510 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5511 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5512 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5513 | } |
| 5514 | } |
| 5515 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5516 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5517 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5518 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5519 | return (state == PeerConnectionInterface::kStable) || |
| 5520 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5521 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5522 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5523 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5524 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5525 | } |
| 5526 | } |
| 5527 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5528 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5529 | switch (error) { |
| 5530 | case SessionError::kNone: |
| 5531 | return "ERROR_NONE"; |
| 5532 | case SessionError::kContent: |
| 5533 | return "ERROR_CONTENT"; |
| 5534 | case SessionError::kTransport: |
| 5535 | return "ERROR_TRANSPORT"; |
| 5536 | } |
| 5537 | RTC_NOTREACHED(); |
| 5538 | return ""; |
| 5539 | } |
| 5540 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5541 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5542 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5543 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5544 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5545 | return desc.str(); |
| 5546 | } |
| 5547 | |
| 5548 | // We need to check the local/remote description for the Transport instead of |
| 5549 | // the session, because a new Transport added during renegotiation may have |
| 5550 | // them unset while the session has them set from the previous negotiation. |
| 5551 | // Not doing so may trigger the auto generation of transport description and |
| 5552 | // mess up DTLS identity information, ICE credential, etc. |
| 5553 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5554 | const IceCandidateInterface* candidate, |
| 5555 | const SessionDescriptionInterface* remote_desc, |
| 5556 | bool* valid) { |
| 5557 | *valid = true; |
| 5558 | |
| 5559 | const SessionDescriptionInterface* current_remote_desc = |
| 5560 | remote_desc ? remote_desc : remote_description(); |
| 5561 | |
| 5562 | if (!current_remote_desc) { |
| 5563 | return false; |
| 5564 | } |
| 5565 | |
| 5566 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5567 | size_t remote_content_size = |
| 5568 | current_remote_desc->description()->contents().size(); |
| 5569 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5570 | RTC_LOG(LS_ERROR) |
| 5571 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5572 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5573 | |
| 5574 | *valid = false; |
| 5575 | return false; |
| 5576 | } |
| 5577 | |
| 5578 | cricket::ContentInfo content = |
| 5579 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5580 | |
| 5581 | const std::string transport_name = GetTransportName(content.name); |
| 5582 | if (transport_name.empty()) { |
| 5583 | return false; |
| 5584 | } |
| 5585 | return transport_controller_->ReadyForRemoteCandidates(transport_name); |
| 5586 | } |
| 5587 | |
| 5588 | bool PeerConnection::SrtpRequired() const { |
| 5589 | return dtls_enabled_ || |
| 5590 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5591 | } |
| 5592 | |
| 5593 | void PeerConnection::OnTransportControllerGatheringState( |
| 5594 | cricket::IceGatheringState state) { |
| 5595 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5596 | if (state == cricket::kIceGatheringGathering) { |
| 5597 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 5598 | } else if (state == cricket::kIceGatheringComplete) { |
| 5599 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 5600 | } |
| 5601 | } |
| 5602 | |
| 5603 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5604 | std::map<std::string, std::set<cricket::MediaType>> |
| 5605 | media_types_by_transport_name; |
| 5606 | for (auto transceiver : transceivers_) { |
| 5607 | if (transceiver->internal()->channel()) { |
| 5608 | const std::string& transport_name = |
| 5609 | transceiver->internal()->channel()->transport_name(); |
| 5610 | media_types_by_transport_name[transport_name].insert( |
| 5611 | transceiver->internal()->media_type()); |
| 5612 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5613 | } |
| 5614 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5615 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 5616 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5617 | } |
| 5618 | if (sctp_transport_name_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5619 | media_types_by_transport_name[*sctp_transport_name_].insert( |
| 5620 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5621 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5622 | for (const auto& entry : media_types_by_transport_name) { |
| 5623 | const std::string& transport_name = entry.first; |
| 5624 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5625 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5626 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5627 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5628 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | } |
| 5632 | // Walk through the ConnectionInfos to gather best connection usage |
| 5633 | // for IPv4 and IPv6. |
| 5634 | void PeerConnection::ReportBestConnectionState( |
| 5635 | const cricket::TransportStats& stats) { |
| 5636 | RTC_DCHECK(metrics_observer()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5637 | for (const cricket::TransportChannelStats& channel_stats : |
| 5638 | stats.channel_stats) { |
| 5639 | for (const cricket::ConnectionInfo& connection_info : |
| 5640 | channel_stats.connection_infos) { |
| 5641 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5642 | continue; |
| 5643 | } |
| 5644 | |
| 5645 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5646 | const cricket::Candidate& local = connection_info.local_candidate; |
| 5647 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5648 | |
| 5649 | // Increment the counter for IceCandidatePairType. |
| 5650 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 5651 | (local.type() == RELAY_PORT_TYPE && |
| 5652 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 5653 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 5654 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 5655 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 5656 | } else { |
| 5657 | RTC_CHECK(0); |
| 5658 | } |
| 5659 | metrics_observer()->IncrementEnumCounter( |
| 5660 | type, GetIceCandidatePairCounter(local, remote), |
| 5661 | kIceCandidatePairMax); |
| 5662 | |
| 5663 | // Increment the counter for IP type. |
| 5664 | if (local.address().family() == AF_INET) { |
| 5665 | metrics_observer()->IncrementEnumCounter( |
| 5666 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 5667 | kPeerConnectionAddressFamilyCounter_Max); |
| 5668 | |
| 5669 | } else if (local.address().family() == AF_INET6) { |
| 5670 | metrics_observer()->IncrementEnumCounter( |
| 5671 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 5672 | kPeerConnectionAddressFamilyCounter_Max); |
| 5673 | } else { |
| 5674 | RTC_CHECK(0); |
| 5675 | } |
| 5676 | |
| 5677 | return; |
| 5678 | } |
| 5679 | } |
| 5680 | } |
| 5681 | |
| 5682 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5683 | const cricket::TransportStats& stats, |
| 5684 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5685 | RTC_DCHECK(metrics_observer()); |
| 5686 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 5687 | return; |
| 5688 | } |
| 5689 | |
| 5690 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 5691 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 5692 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 5693 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 5694 | return; |
| 5695 | } |
| 5696 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5697 | for (cricket::MediaType media_type : media_types) { |
| 5698 | PeerConnectionEnumCounterType srtp_counter_type; |
| 5699 | PeerConnectionEnumCounterType ssl_counter_type; |
| 5700 | switch (media_type) { |
| 5701 | case cricket::MEDIA_TYPE_AUDIO: |
| 5702 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 5703 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 5704 | break; |
| 5705 | case cricket::MEDIA_TYPE_VIDEO: |
| 5706 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 5707 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 5708 | break; |
| 5709 | case cricket::MEDIA_TYPE_DATA: |
| 5710 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 5711 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 5712 | break; |
| 5713 | default: |
| 5714 | RTC_NOTREACHED(); |
| 5715 | continue; |
| 5716 | } |
| 5717 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 5718 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 5719 | srtp_crypto_suite); |
| 5720 | } |
| 5721 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 5722 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 5723 | ssl_cipher_suite); |
| 5724 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 5729 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 5730 | RTC_DCHECK(call_); |
| 5731 | call_->OnSentPacket(sent_packet); |
| 5732 | } |
| 5733 | |
| 5734 | const std::string PeerConnection::GetTransportName( |
| 5735 | const std::string& content_name) { |
| 5736 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5737 | if (channel) { |
| 5738 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5739 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5740 | if (sctp_transport_) { |
| 5741 | RTC_DCHECK(sctp_content_name_); |
| 5742 | RTC_DCHECK(sctp_transport_name_); |
| 5743 | if (content_name == *sctp_content_name_) { |
| 5744 | return *sctp_transport_name_; |
| 5745 | } |
| 5746 | } |
| 5747 | // Return an empty string if failed to retrieve the transport name. |
| 5748 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | void PeerConnection::DestroyRtcpTransport_n(const std::string& transport_name) { |
| 5752 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5753 | transport_controller_->DestroyDtlsTransport_n( |
| 5754 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5755 | } |
| 5756 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5757 | void PeerConnection::DestroyTransceiverChannel( |
| 5758 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5759 | transceiver) { |
| 5760 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5761 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5762 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5763 | if (channel) { |
| 5764 | transceiver->internal()->SetChannel(nullptr); |
| 5765 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5766 | } |
| 5767 | } |
| 5768 | |
| 5769 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5770 | if (rtp_data_channel_) { |
| 5771 | OnDataChannelDestroyed(); |
| 5772 | DestroyBaseChannel(rtp_data_channel_); |
| 5773 | rtp_data_channel_ = nullptr; |
| 5774 | } |
| 5775 | |
| 5776 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 5777 | // grab a reference to this PeerConnection. If this is called from the |
| 5778 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 5779 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 5780 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 5781 | |
| 5782 | if (sctp_transport_) { |
| 5783 | OnDataChannelDestroyed(); |
| 5784 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 5785 | [this] { DestroySctpTransport_n(); }); |
| 5786 | } |
| 5787 | } |
| 5788 | |
| 5789 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 5790 | RTC_DCHECK(channel); |
| 5791 | RTC_DCHECK(channel->rtp_dtls_transport()); |
| 5792 | |
| 5793 | // Need to cache these before destroying the base channel so that we do not |
| 5794 | // access uninitialized memory. |
| 5795 | const std::string transport_name = |
| 5796 | channel->rtp_dtls_transport()->transport_name(); |
| 5797 | const bool need_to_delete_rtcp = (channel->rtcp_dtls_transport() != nullptr); |
| 5798 | |
| 5799 | switch (channel->media_type()) { |
| 5800 | case cricket::MEDIA_TYPE_AUDIO: |
| 5801 | channel_manager()->DestroyVoiceChannel( |
| 5802 | static_cast<cricket::VoiceChannel*>(channel)); |
| 5803 | break; |
| 5804 | case cricket::MEDIA_TYPE_VIDEO: |
| 5805 | channel_manager()->DestroyVideoChannel( |
| 5806 | static_cast<cricket::VideoChannel*>(channel)); |
| 5807 | break; |
| 5808 | case cricket::MEDIA_TYPE_DATA: |
| 5809 | channel_manager()->DestroyRtpDataChannel( |
| 5810 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 5811 | break; |
| 5812 | default: |
| 5813 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 5814 | break; |
| 5815 | } |
| 5816 | |
| 5817 | // |channel| can no longer be used. |
| 5818 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5819 | transport_controller_->DestroyDtlsTransport( |
| 5820 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5821 | if (need_to_delete_rtcp) { |
| 5822 | transport_controller_->DestroyDtlsTransport( |
| 5823 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5824 | } |
| 5825 | } |
| 5826 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 5827 | void PeerConnection::ClearStatsCache() { |
| 5828 | if (stats_collector_) { |
| 5829 | stats_collector_->ClearCachedStatsReport(); |
| 5830 | } |
| 5831 | } |
| 5832 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 5833 | } // namespace webrtc |