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."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 93 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 95 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 96 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 97 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 98 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 99 | // The length of RTCP CNAMEs. |
| 100 | static const int kRtcpCnameLength = 16; |
| 101 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 103 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 105 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 107 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 110 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | explicit SetSessionDescriptionMsg( |
| 112 | webrtc::SetSessionDescriptionObserver* observer) |
| 113 | : observer(observer) { |
| 114 | } |
| 115 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 116 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 117 | RTCError error; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 120 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 121 | explicit CreateSessionDescriptionMsg( |
| 122 | webrtc::CreateSessionDescriptionObserver* observer) |
| 123 | : observer(observer) {} |
| 124 | |
| 125 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 126 | RTCError error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 129 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 130 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 131 | webrtc::MediaStreamTrackInterface* track) |
| 132 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 134 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 135 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 138 | // Check if we can send |new_stream| on a PeerConnection. |
| 139 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 140 | webrtc::MediaStreamInterface* new_stream) { |
| 141 | if (!new_stream || !current_streams) { |
| 142 | return false; |
| 143 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 144 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 145 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 146 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 152 | // If the direction is "recvonly" or "inactive", treat the description |
| 153 | // as containing no streams. |
| 154 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 155 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 156 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 157 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 158 | ? desc->streams() |
| 159 | : std::vector<cricket::StreamParams>(); |
| 160 | } |
| 161 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 162 | bool IsValidOfferToReceiveMedia(int value) { |
| 163 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 164 | return (value >= Options::kUndefined) && |
| 165 | (value <= Options::kMaxOfferToReceiveMedia); |
| 166 | } |
| 167 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 168 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 169 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 170 | const std::vector<rtc::scoped_refptr< |
| 171 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 172 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 173 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 174 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 175 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 176 | if (audio_media_description_options) { |
| 177 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 178 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 179 | } |
| 180 | } else { |
| 181 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 182 | if (video_media_description_options) { |
| 183 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 184 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 187 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 188 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 189 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 190 | // Add options to |session_options| from |rtp_data_channels|. |
| 191 | void AddRtpDataChannelOptions( |
| 192 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 193 | rtp_data_channels, |
| 194 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 195 | if (!data_media_description_options) { |
| 196 | return; |
| 197 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 198 | // Check for data channels. |
| 199 | for (const auto& kv : rtp_data_channels) { |
| 200 | const DataChannel* channel = kv.second; |
| 201 | if (channel->state() == DataChannel::kConnecting || |
| 202 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 203 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 204 | // the data channel's label. |
| 205 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 206 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 211 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 212 | PeerConnectionInterface::IceTransportsType type) { |
| 213 | switch (type) { |
| 214 | case PeerConnectionInterface::kNone: |
| 215 | return cricket::CF_NONE; |
| 216 | case PeerConnectionInterface::kRelay: |
| 217 | return cricket::CF_RELAY; |
| 218 | case PeerConnectionInterface::kNoHost: |
| 219 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 220 | case PeerConnectionInterface::kAll: |
| 221 | return cricket::CF_ALL; |
| 222 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 223 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 224 | } |
| 225 | return cricket::CF_NONE; |
| 226 | } |
| 227 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 228 | // Helper to set an error and return from a method. |
| 229 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 230 | if (error) { |
| 231 | error->set_type(type); |
| 232 | } |
| 233 | return type == webrtc::RTCErrorType::NONE; |
| 234 | } |
| 235 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 236 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 237 | if (error_out) { |
| 238 | *error_out = std::move(error); |
| 239 | } |
| 240 | return error.ok(); |
| 241 | } |
| 242 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 243 | std::string GetSignalingStateString( |
| 244 | PeerConnectionInterface::SignalingState state) { |
| 245 | switch (state) { |
| 246 | case PeerConnectionInterface::kStable: |
| 247 | return "kStable"; |
| 248 | case PeerConnectionInterface::kHaveLocalOffer: |
| 249 | return "kHaveLocalOffer"; |
| 250 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 251 | return "kHavePrAnswer"; |
| 252 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 253 | return "kHaveRemoteOffer"; |
| 254 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 255 | return "kHaveRemotePrAnswer"; |
| 256 | case PeerConnectionInterface::kClosed: |
| 257 | return "kClosed"; |
| 258 | } |
| 259 | RTC_NOTREACHED(); |
| 260 | return ""; |
| 261 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 262 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 263 | IceCandidatePairType GetIceCandidatePairCounter( |
| 264 | const cricket::Candidate& local, |
| 265 | const cricket::Candidate& remote) { |
| 266 | const auto& l = local.type(); |
| 267 | const auto& r = remote.type(); |
| 268 | const auto& host = LOCAL_PORT_TYPE; |
| 269 | const auto& srflx = STUN_PORT_TYPE; |
| 270 | const auto& relay = RELAY_PORT_TYPE; |
| 271 | const auto& prflx = PRFLX_PORT_TYPE; |
| 272 | if (l == host && r == host) { |
| 273 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 274 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 275 | if (local_private) { |
| 276 | if (remote_private) { |
| 277 | return kIceCandidatePairHostPrivateHostPrivate; |
| 278 | } else { |
| 279 | return kIceCandidatePairHostPrivateHostPublic; |
| 280 | } |
| 281 | } else { |
| 282 | if (remote_private) { |
| 283 | return kIceCandidatePairHostPublicHostPrivate; |
| 284 | } else { |
| 285 | return kIceCandidatePairHostPublicHostPublic; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | if (l == host && r == srflx) |
| 290 | return kIceCandidatePairHostSrflx; |
| 291 | if (l == host && r == relay) |
| 292 | return kIceCandidatePairHostRelay; |
| 293 | if (l == host && r == prflx) |
| 294 | return kIceCandidatePairHostPrflx; |
| 295 | if (l == srflx && r == host) |
| 296 | return kIceCandidatePairSrflxHost; |
| 297 | if (l == srflx && r == srflx) |
| 298 | return kIceCandidatePairSrflxSrflx; |
| 299 | if (l == srflx && r == relay) |
| 300 | return kIceCandidatePairSrflxRelay; |
| 301 | if (l == srflx && r == prflx) |
| 302 | return kIceCandidatePairSrflxPrflx; |
| 303 | if (l == relay && r == host) |
| 304 | return kIceCandidatePairRelayHost; |
| 305 | if (l == relay && r == srflx) |
| 306 | return kIceCandidatePairRelaySrflx; |
| 307 | if (l == relay && r == relay) |
| 308 | return kIceCandidatePairRelayRelay; |
| 309 | if (l == relay && r == prflx) |
| 310 | return kIceCandidatePairRelayPrflx; |
| 311 | if (l == prflx && r == host) |
| 312 | return kIceCandidatePairPrflxHost; |
| 313 | if (l == prflx && r == srflx) |
| 314 | return kIceCandidatePairPrflxSrflx; |
| 315 | if (l == prflx && r == relay) |
| 316 | return kIceCandidatePairPrflxRelay; |
| 317 | return kIceCandidatePairMax; |
| 318 | } |
| 319 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 320 | // Logic to decide if an m= section can be recycled. This means that the new |
| 321 | // m= section is not rejected, but the old local or remote m= section is |
| 322 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 323 | // of the old remote and old local descriptions in no particular order. |
| 324 | // We need to check both the old local and remote because either |
| 325 | // could be the most current from the latest negotation. |
| 326 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 327 | const ContentInfo& content, |
| 328 | const ContentInfo* old_content_one, |
| 329 | const ContentInfo* old_content_two) { |
| 330 | return type == SdpType::kOffer && !content.rejected && |
| 331 | ((old_content_one && old_content_one->rejected) || |
| 332 | (old_content_two && old_content_two->rejected)); |
| 333 | } |
| 334 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 335 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 336 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 337 | // less than |current_desc|. In the case of checking an answer's |
| 338 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 339 | // local or remote. In the case of checking an offer's |new_desc| we |
| 340 | // check against the local and remote descriptions stored from the last |
| 341 | // negotiation, because either of these could be the most up to date for |
| 342 | // possible rejected m sections. These are the |current_desc| and |
| 343 | // |secondary_current_desc|. |
| 344 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 345 | const SessionDescription* secondary_current_desc, |
| 346 | const SessionDescription& new_desc, |
| 347 | const SdpType type) { |
| 348 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 352 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 353 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 354 | if (secondary_current_desc && |
| 355 | i < secondary_current_desc->contents().size()) { |
| 356 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 357 | } |
| 358 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 359 | ¤t_desc.contents()[i], |
| 360 | secondary_content_info)) { |
| 361 | // For new offer descriptions, if the media section can be recycled, it's |
| 362 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 363 | continue; |
| 364 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 365 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 369 | new_desc.contents()[i].media_description(); |
| 370 | const MediaContentDescription* current_desc_mdesc = |
| 371 | current_desc.contents()[i].media_description(); |
| 372 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | } |
| 376 | return true; |
| 377 | } |
| 378 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 379 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 380 | const SessionDescription& desc2) { |
| 381 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 384 | void NoteKeyProtocolAndMedia( |
| 385 | KeyExchangeProtocolType protocol_type, |
| 386 | cricket::MediaType media_type, |
| 387 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
| 388 | if (!uma_observer) |
| 389 | return; |
| 390 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 391 | protocol_type, |
| 392 | webrtc::kEnumCounterKeyProtocolMax); |
| 393 | static const std::map<std::pair<KeyExchangeProtocolType, cricket::MediaType>, |
| 394 | KeyExchangeProtocolMedia> |
| 395 | proto_media_counter_map = { |
| 396 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO}, |
| 397 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 398 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO}, |
| 399 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 400 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA}, |
| 401 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 402 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO}, |
| 403 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 404 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO}, |
| 405 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 406 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA}, |
| 407 | kEnumCounterKeyProtocolMediaTypeSdesData}}; |
| 408 | |
| 409 | auto it = proto_media_counter_map.find({protocol_type, media_type}); |
| 410 | if (it != proto_media_counter_map.end()) { |
| 411 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocolMediaType, |
| 412 | it->second, |
| 413 | kEnumCounterKeyProtocolMediaTypeMax); |
| 414 | } |
| 415 | } |
| 416 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 417 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 418 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 419 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 420 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 421 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 422 | // by Channel's |srtp_required| check. |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 423 | RTCError VerifyCrypto(const SessionDescription* desc, |
| 424 | bool dtls_enabled, |
| 425 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 426 | const cricket::ContentGroup* bundle = |
| 427 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 428 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 429 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 430 | continue; |
| 431 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 432 | // Note what media is used with each crypto protocol, for all sections. |
| 433 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 434 | : webrtc::kEnumCounterKeyProtocolSdes, |
| 435 | content_info.media_description()->type(), |
| 436 | uma_observer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 437 | const std::string& mid = content_info.name; |
| 438 | if (bundle && bundle->HasContentName(mid) && |
| 439 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 440 | // This isn't the first media section in the BUNDLE group, so it's not |
| 441 | // required to have crypto attributes, since only the crypto attributes |
| 442 | // from the first section actually get used. |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | // If the content isn't rejected or bundled into another m= section, crypto |
| 447 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 448 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 449 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 450 | if (!media || !tinfo) { |
| 451 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 452 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 453 | } |
| 454 | if (dtls_enabled) { |
| 455 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 456 | RTC_LOG(LS_WARNING) |
| 457 | << "Session description must have DTLS fingerprint if " |
| 458 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 459 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 460 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 461 | } |
| 462 | } else { |
| 463 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 464 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 465 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 466 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 470 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 474 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 475 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 476 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 477 | const cricket::ContentGroup* bundle = |
| 478 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 479 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 480 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 481 | continue; |
| 482 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 483 | const std::string& mid = content_info.name; |
| 484 | if (bundle && bundle->HasContentName(mid) && |
| 485 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 486 | // This isn't the first media section in the BUNDLE group, so it's not |
| 487 | // required to have ufrag/password, since only the ufrag/password from |
| 488 | // the first section actually get used. |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | // If the content isn't rejected or bundled into another m= section, |
| 493 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 494 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 495 | if (!tinfo) { |
| 496 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 497 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | if (tinfo->description.ice_ufrag.empty() || |
| 501 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 502 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 510 | uint32_t ssrc, |
| 511 | std::string* track_id) { |
| 512 | RTC_DCHECK(track_id != NULL); |
| 513 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 514 | const cricket::AudioContentDescription* audio_desc = |
| 515 | cricket::GetFirstAudioContentDescription(session_description); |
| 516 | if (audio_desc) { |
| 517 | const auto* found = cricket::GetStreamBySsrc(audio_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 518 | if (found) { |
| 519 | *track_id = found->id; |
| 520 | return true; |
| 521 | } |
| 522 | } |
| 523 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 524 | const cricket::VideoContentDescription* video_desc = |
| 525 | cricket::GetFirstVideoContentDescription(session_description); |
| 526 | if (video_desc) { |
| 527 | const auto* found = cricket::GetStreamBySsrc(video_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 528 | if (found) { |
| 529 | *track_id = found->id; |
| 530 | return true; |
| 531 | } |
| 532 | } |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | // Get the SCTP port out of a SessionDescription. |
| 537 | // Return -1 if not found. |
| 538 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 539 | const cricket::DataContentDescription* data_desc = |
| 540 | GetFirstDataContentDescription(session_description); |
| 541 | RTC_DCHECK(data_desc); |
| 542 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 543 | return -1; |
| 544 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 545 | std::string value; |
| 546 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 547 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 548 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 549 | if (!codec.Matches(match_pattern)) { |
| 550 | continue; |
| 551 | } |
| 552 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 553 | return rtc::FromString<int>(value); |
| 554 | } |
| 555 | } |
| 556 | return -1; |
| 557 | } |
| 558 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 559 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 560 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 561 | const SessionDescriptionInterface* new_desc, |
| 562 | const std::string& content_name) { |
| 563 | if (!old_desc) { |
| 564 | return false; |
| 565 | } |
| 566 | const SessionDescription* new_sd = new_desc->description(); |
| 567 | const SessionDescription* old_sd = old_desc->description(); |
| 568 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 569 | if (!cinfo || cinfo->rejected) { |
| 570 | return false; |
| 571 | } |
| 572 | // If the content isn't rejected, check if ufrag and password has changed. |
| 573 | const cricket::TransportDescription* new_transport_desc = |
| 574 | new_sd->GetTransportDescriptionByName(content_name); |
| 575 | const cricket::TransportDescription* old_transport_desc = |
| 576 | old_sd->GetTransportDescriptionByName(content_name); |
| 577 | if (!new_transport_desc || !old_transport_desc) { |
| 578 | // No transport description exists. This is not an ICE restart. |
| 579 | return false; |
| 580 | } |
| 581 | if (cricket::IceCredentialsChanged( |
| 582 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 583 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 584 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 585 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 591 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 592 | // from an RTCError. |
| 593 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 594 | SdpType type, |
| 595 | const RTCError& error) { |
| 596 | std::ostringstream oss; |
| 597 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 598 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
| 599 | return oss.str(); |
| 600 | } |
| 601 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 602 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 603 | std::string output = "streams=["; |
| 604 | const char* separator = ""; |
| 605 | for (const auto& stream_id : stream_ids) { |
| 606 | output.append(separator).append(stream_id); |
| 607 | separator = ", "; |
| 608 | } |
| 609 | output.append("]"); |
| 610 | return output; |
| 611 | } |
| 612 | |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 613 | rtc::Optional<int> RTCConfigurationToIceConfigOptionalInt( |
| 614 | int rtc_configuration_parameter) { |
| 615 | if (rtc_configuration_parameter == |
| 616 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
| 617 | return rtc::nullopt; |
| 618 | } |
| 619 | return rtc_configuration_parameter; |
| 620 | } |
| 621 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 622 | } // namespace |
| 623 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 624 | // Upon completion, posts a task to execute the callback of the |
| 625 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 626 | // point, the state of the peer connection might no longer reflect the effects |
| 627 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 628 | // modified during the post. |
| 629 | // TODO(hbos): Remove this class once we remove the version of |
| 630 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 631 | // SetSessionDescriptionObserver as an argument. |
| 632 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 633 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 634 | public: |
| 635 | SetRemoteDescriptionObserverAdapter( |
| 636 | rtc::scoped_refptr<PeerConnection> pc, |
| 637 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 638 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 639 | |
| 640 | // SetRemoteDescriptionObserverInterface implementation. |
| 641 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 642 | if (error.ok()) |
| 643 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 644 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 645 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | private: |
| 649 | rtc::scoped_refptr<PeerConnection> pc_; |
| 650 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 651 | }; |
| 652 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 653 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 654 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 655 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 656 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 657 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 658 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 659 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 660 | BundlePolicy bundle_policy; |
| 661 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 662 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 663 | int ice_candidate_pool_size; |
| 664 | bool disable_ipv6; |
| 665 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 666 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 667 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 668 | bool enable_rtp_data_channel; |
| 669 | rtc::Optional<int> screencast_min_bitrate; |
| 670 | rtc::Optional<bool> combined_audio_video_bwe; |
| 671 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 672 | TcpCandidatePolicy tcp_candidate_policy; |
| 673 | CandidateNetworkPolicy candidate_network_policy; |
| 674 | int audio_jitter_buffer_max_packets; |
| 675 | bool audio_jitter_buffer_fast_accelerate; |
| 676 | int ice_connection_receiving_timeout; |
| 677 | int ice_backup_candidate_pair_ping_interval; |
| 678 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 679 | bool prioritize_most_likely_ice_candidate_pairs; |
| 680 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 681 | bool prune_turn_ports; |
| 682 | bool presume_writable_when_fully_relayed; |
| 683 | bool enable_ice_renomination; |
| 684 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 685 | rtc::Optional<int> ice_check_interval_strong_connectivity; |
| 686 | rtc::Optional<int> ice_check_interval_weak_connectivity; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 687 | rtc::Optional<int> ice_check_min_interval; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 688 | rtc::Optional<int> ice_unwritable_timeout; |
| 689 | rtc::Optional<int> ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 690 | rtc::Optional<int> stun_candidate_keepalive_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 691 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 692 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 693 | SdpSemantics sdp_semantics; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 694 | rtc::Optional<rtc::AdapterType> network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 695 | }; |
| 696 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 697 | "Did you add something to RTCConfiguration and forget to " |
| 698 | "update operator==?"); |
| 699 | return type == o.type && servers == o.servers && |
| 700 | bundle_policy == o.bundle_policy && |
| 701 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 702 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 703 | candidate_network_policy == o.candidate_network_policy && |
| 704 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 705 | audio_jitter_buffer_fast_accelerate == |
| 706 | o.audio_jitter_buffer_fast_accelerate && |
| 707 | ice_connection_receiving_timeout == |
| 708 | o.ice_connection_receiving_timeout && |
| 709 | ice_backup_candidate_pair_ping_interval == |
| 710 | o.ice_backup_candidate_pair_ping_interval && |
| 711 | continual_gathering_policy == o.continual_gathering_policy && |
| 712 | certificates == o.certificates && |
| 713 | prioritize_most_likely_ice_candidate_pairs == |
| 714 | o.prioritize_most_likely_ice_candidate_pairs && |
| 715 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 716 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 717 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 718 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 719 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 720 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 721 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 722 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 723 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 724 | prune_turn_ports == o.prune_turn_ports && |
| 725 | presume_writable_when_fully_relayed == |
| 726 | o.presume_writable_when_fully_relayed && |
| 727 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 728 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 729 | ice_check_interval_strong_connectivity == |
| 730 | o.ice_check_interval_strong_connectivity && |
| 731 | ice_check_interval_weak_connectivity == |
| 732 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 733 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 734 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 735 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 736 | stun_candidate_keepalive_interval == |
| 737 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 738 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 739 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 740 | sdp_semantics == o.sdp_semantics && |
| 741 | network_preference == o.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 745 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 746 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 747 | } |
| 748 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 749 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 750 | std::string GenerateRtcpCname() { |
| 751 | std::string cname; |
| 752 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 753 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 754 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 755 | } |
| 756 | return cname; |
| 757 | } |
| 758 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 759 | bool ValidateOfferAnswerOptions( |
| 760 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 761 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 762 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 763 | } |
| 764 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 765 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 766 | // m= sections (in other words, nothing that involves a map/array). |
| 767 | void ExtractSharedMediaSessionOptions( |
| 768 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 769 | cricket::MediaSessionOptions* session_options) { |
| 770 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 771 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 772 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 773 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 774 | bool ConvertConstraintsToOfferAnswerOptions( |
| 775 | const MediaConstraintsInterface* constraints, |
| 776 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 777 | if (!constraints) { |
| 778 | return true; |
| 779 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 780 | |
| 781 | bool value = false; |
| 782 | size_t mandatory_constraints_satisfied = 0; |
| 783 | |
| 784 | if (FindConstraint(constraints, |
| 785 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 786 | &mandatory_constraints_satisfied)) { |
| 787 | offer_answer_options->offer_to_receive_audio = |
| 788 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 789 | kOfferToReceiveMediaTrue |
| 790 | : 0; |
| 791 | } |
| 792 | |
| 793 | if (FindConstraint(constraints, |
| 794 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 795 | &mandatory_constraints_satisfied)) { |
| 796 | offer_answer_options->offer_to_receive_video = |
| 797 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 798 | kOfferToReceiveMediaTrue |
| 799 | : 0; |
| 800 | } |
| 801 | if (FindConstraint(constraints, |
| 802 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 803 | &mandatory_constraints_satisfied)) { |
| 804 | offer_answer_options->voice_activity_detection = value; |
| 805 | } |
| 806 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 807 | &mandatory_constraints_satisfied)) { |
| 808 | offer_answer_options->use_rtp_mux = value; |
| 809 | } |
| 810 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 811 | &value, &mandatory_constraints_satisfied)) { |
| 812 | offer_answer_options->ice_restart = value; |
| 813 | } |
| 814 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 815 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 816 | } |
| 817 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 818 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 819 | std::unique_ptr<RtcEventLog> event_log, |
| 820 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 822 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 823 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 824 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 825 | remote_streams_(StreamCollection::Create()), |
| 826 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 827 | |
| 828 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 829 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 830 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 831 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 832 | // Need to stop transceivers before destroying the stats collector because |
| 833 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 834 | // stopping. |
| 835 | for (auto transceiver : transceivers_) { |
| 836 | transceiver->Stop(); |
| 837 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 838 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 839 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 840 | if (stats_collector_) { |
| 841 | stats_collector_->WaitForPendingRequest(); |
| 842 | stats_collector_ = nullptr; |
| 843 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 844 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 845 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 846 | // the last stats request can still read from the channels. |
| 847 | DestroyAllChannels(); |
| 848 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 849 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 850 | |
| 851 | webrtc_session_desc_factory_.reset(); |
| 852 | sctp_invoker_.reset(); |
| 853 | sctp_factory_.reset(); |
| 854 | transport_controller_.reset(); |
| 855 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 856 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 857 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 858 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 859 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 860 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 861 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 862 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 863 | event_log_.reset(); |
| 864 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 867 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 868 | // Destroy video channels first since they may have a pointer to a voice |
| 869 | // channel. |
| 870 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 871 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 872 | DestroyTransceiverChannel(transceiver); |
| 873 | } |
| 874 | } |
| 875 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 876 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 877 | DestroyTransceiverChannel(transceiver); |
| 878 | } |
| 879 | } |
| 880 | DestroyDataChannel(); |
| 881 | } |
| 882 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 883 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 884 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 885 | PeerConnectionDependencies dependencies) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 886 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 887 | |
| 888 | RTCError config_error = ValidateConfiguration(configuration); |
| 889 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 890 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 891 | return false; |
| 892 | } |
| 893 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 894 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 895 | RTC_LOG(LS_ERROR) |
| 896 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 897 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 898 | return false; |
| 899 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 900 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 901 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 902 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 903 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 904 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 905 | return false; |
| 906 | } |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 907 | observer_ = dependencies.observer; |
| 908 | port_allocator_ = std::move(dependencies.allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 909 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 910 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 911 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 912 | if (!network_thread()->Invoke<bool>( |
| 913 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 914 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 915 | return false; |
| 916 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 917 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 918 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 919 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 920 | // RFC 3264: The numeric value of the session id and version in the |
| 921 | // o line MUST be representable with a "64 bit signed integer". |
| 922 | // Due to this constraint session id |session_id_| is max limited to |
| 923 | // LLONG_MAX. |
| 924 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 925 | JsepTransportController::Config config; |
| 926 | config.redetermine_role_on_ice_restart = |
| 927 | configuration.redetermine_role_on_ice_restart; |
| 928 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 929 | config.disable_encryption = options.disable_encryption; |
| 930 | config.bundle_policy = configuration.bundle_policy; |
| 931 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
| 932 | config.crypto_options = options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 933 | config.transport_observer = this; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 934 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 935 | config.enable_external_auth = true; |
| 936 | #endif |
| 937 | transport_controller_.reset(new JsepTransportController( |
| 938 | signaling_thread(), network_thread(), port_allocator_.get(), config)); |
| 939 | transport_controller_->SignalIceConnectionState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 940 | this, &PeerConnection::OnTransportControllerConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 941 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 942 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 943 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 944 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 945 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 946 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 947 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 948 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 949 | |
| 950 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 951 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 952 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 953 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 954 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 955 | configuration_ = configuration; |
| 956 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 957 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 958 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 959 | if (!configuration.certificates.empty()) { |
| 960 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 961 | // just picking the first one. The decision should be made based on the DTLS |
| 962 | // handshake. The DTLS negotiations need to know about all certificates. |
| 963 | certificate = configuration.certificates[0]; |
| 964 | } |
| 965 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 966 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 967 | |
| 968 | if (options.disable_encryption) { |
| 969 | dtls_enabled_ = false; |
| 970 | } else { |
| 971 | // Enable DTLS by default if we have an identity store or a certificate. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 972 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 973 | // |configuration| can override the default |dtls_enabled_| value. |
| 974 | if (configuration.enable_dtls_srtp) { |
| 975 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 980 | // It takes precendence over the disable_sctp_data_channels |
| 981 | // PeerConnectionFactoryInterface::Options. |
| 982 | if (configuration.enable_rtp_data_channel) { |
| 983 | data_channel_type_ = cricket::DCT_RTP; |
| 984 | } else { |
| 985 | // DTLS has to be enabled to use SCTP. |
| 986 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 987 | data_channel_type_ = cricket::DCT_SCTP; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | video_options_.screencast_min_bitrate_kbps = |
| 992 | configuration.screencast_min_bitrate; |
| 993 | audio_options_.combined_audio_video_bwe = |
| 994 | configuration.combined_audio_video_bwe; |
| 995 | |
| 996 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 997 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 998 | |
| 999 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1000 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1001 | |
| 1002 | // Whether the certificate generator/certificate is null or not determines |
| 1003 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1004 | // the right instructions by clearing the variables if needed. |
| 1005 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1006 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1007 | certificate = nullptr; |
| 1008 | } else if (certificate) { |
| 1009 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1010 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1014 | signaling_thread(), channel_manager(), this, session_id(), |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1015 | std::move(dependencies.cert_generator), certificate)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1016 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1017 | this, &PeerConnection::OnCertificateReady); |
| 1018 | |
| 1019 | if (options.disable_encryption) { |
| 1020 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1021 | } |
| 1022 | |
| 1023 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 1024 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1025 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1026 | // Add default audio/video transceivers for Plan B SDP. |
| 1027 | if (!IsUnifiedPlan()) { |
| 1028 | transceivers_.push_back( |
| 1029 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1030 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1031 | transceivers_.push_back( |
| 1032 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1033 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1034 | } |
| 1035 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1036 | return true; |
| 1037 | } |
| 1038 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1039 | RTCError PeerConnection::ValidateConfiguration( |
| 1040 | const RTCConfiguration& config) const { |
| 1041 | if (config.ice_regather_interval_range && |
| 1042 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1043 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1044 | "ice_regather_interval_range specified but continual " |
| 1045 | "gathering policy is GATHER_ONCE"); |
| 1046 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1047 | auto result = |
| 1048 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1049 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1052 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1053 | PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1054 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1055 | "Plan SdpSemantics. Please use GetSenders " |
| 1056 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1057 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1060 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1061 | PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1062 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1063 | "Plan SdpSemantics. Please use GetReceivers " |
| 1064 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1065 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1068 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1069 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1070 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1071 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1072 | if (IsClosed()) { |
| 1073 | return false; |
| 1074 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1075 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1076 | return false; |
| 1077 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1078 | |
| 1079 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1080 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1081 | observer->SignalAudioTrackAdded.connect(this, |
| 1082 | &PeerConnection::OnAudioTrackAdded); |
| 1083 | observer->SignalAudioTrackRemoved.connect( |
| 1084 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1085 | observer->SignalVideoTrackAdded.connect(this, |
| 1086 | &PeerConnection::OnVideoTrackAdded); |
| 1087 | observer->SignalVideoTrackRemoved.connect( |
| 1088 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1089 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1090 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1091 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1092 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1093 | } |
| 1094 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1095 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1098 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1099 | observer_->OnRenegotiationNeeded(); |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
| 1103 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1104 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1105 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1106 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1107 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1108 | if (!IsClosed()) { |
| 1109 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1110 | RemoveAudioTrack(track.get(), local_stream); |
| 1111 | } |
| 1112 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1113 | RemoveVideoTrack(track.get(), local_stream); |
| 1114 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1115 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1116 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1117 | stream_observers_.erase( |
| 1118 | std::remove_if( |
| 1119 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1120 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1121 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1122 | }), |
| 1123 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1124 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1125 | if (IsClosed()) { |
| 1126 | return; |
| 1127 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1128 | observer_->OnRenegotiationNeeded(); |
| 1129 | } |
| 1130 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1131 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1132 | MediaStreamTrackInterface* track, |
| 1133 | std::vector<MediaStreamInterface*> streams) { |
| 1134 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1135 | std::vector<std::string> stream_ids; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1136 | for (auto* stream : streams) { |
| 1137 | if (!stream) { |
| 1138 | RTC_LOG(LS_ERROR) << "Stream list has null element."; |
| 1139 | return nullptr; |
| 1140 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1141 | stream_ids.push_back(stream->id()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1142 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1143 | auto sender_or_error = AddTrack(track, stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1144 | if (!sender_or_error.ok()) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1145 | return nullptr; |
| 1146 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1147 | return sender_or_error.MoveValue(); |
| 1148 | } |
| 1149 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1150 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1151 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1152 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1153 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1154 | if (!track) { |
| 1155 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1156 | } |
| 1157 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1158 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1159 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1160 | "Track has invalid kind: " + track->kind()); |
| 1161 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1162 | if (IsClosed()) { |
| 1163 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1164 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1165 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1166 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1167 | LOG_AND_RETURN_ERROR( |
| 1168 | RTCErrorType::INVALID_PARAMETER, |
| 1169 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1170 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1171 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1172 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1173 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1174 | if (sender_or_error.ok()) { |
| 1175 | observer_->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1176 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1177 | } |
| 1178 | return sender_or_error; |
| 1179 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1180 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1181 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1182 | PeerConnection::AddTrackPlanB( |
| 1183 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1184 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1185 | if (stream_ids.size() > 1u) { |
| 1186 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1187 | "AddTrack with more than one stream is not " |
| 1188 | "supported with Plan B semantics."); |
| 1189 | } |
| 1190 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1191 | if (adjusted_stream_ids.empty()) { |
| 1192 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1193 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1194 | cricket::MediaType media_type = |
| 1195 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1196 | ? cricket::MEDIA_TYPE_AUDIO |
| 1197 | : cricket::MEDIA_TYPE_VIDEO); |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1198 | auto new_sender = CreateSender(media_type, track, adjusted_stream_ids); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1199 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1200 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1201 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1202 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1203 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1204 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1205 | if (sender_info) { |
| 1206 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1207 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1208 | } else { |
| 1209 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1210 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1211 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1212 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1213 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1214 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1215 | if (sender_info) { |
| 1216 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1217 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1218 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1219 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1220 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1221 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1222 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1223 | PeerConnection::AddTrackUnifiedPlan( |
| 1224 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1225 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1226 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1227 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1228 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1229 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1230 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1231 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1232 | transceiver->internal()->set_direction( |
| 1233 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1234 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1235 | transceiver->internal()->set_direction( |
| 1236 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1237 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1238 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1239 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1240 | } else { |
| 1241 | cricket::MediaType media_type = |
| 1242 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1243 | ? cricket::MEDIA_TYPE_AUDIO |
| 1244 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1245 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1246 | << " transceiver in response to a call to AddTrack."; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1247 | auto sender = CreateSender(media_type, track, stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1248 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1249 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1250 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1251 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1252 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1253 | return transceiver->sender(); |
| 1254 | } |
| 1255 | |
| 1256 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1257 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1258 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1259 | RTC_DCHECK(track); |
| 1260 | for (auto transceiver : transceivers_) { |
| 1261 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1262 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1263 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1264 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1265 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1266 | return transceiver; |
| 1267 | } |
| 1268 | } |
| 1269 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1273 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1274 | return RemoveTrackInternal(sender).ok(); |
| 1275 | } |
| 1276 | |
| 1277 | RTCError PeerConnection::RemoveTrackInternal( |
| 1278 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1279 | if (!sender) { |
| 1280 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1281 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1282 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1283 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1284 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1285 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1286 | if (IsUnifiedPlan()) { |
| 1287 | auto transceiver = FindTransceiverBySender(sender); |
| 1288 | if (!transceiver || !sender->track()) { |
| 1289 | return RTCError::OK(); |
| 1290 | } |
| 1291 | sender->SetTrack(nullptr); |
| 1292 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1293 | transceiver->internal()->set_direction( |
| 1294 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1295 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1296 | transceiver->internal()->set_direction( |
| 1297 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1298 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1299 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1300 | bool removed; |
| 1301 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1302 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1303 | } else { |
| 1304 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1305 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1306 | } |
| 1307 | if (!removed) { |
| 1308 | LOG_AND_RETURN_ERROR( |
| 1309 | RTCErrorType::INVALID_PARAMETER, |
| 1310 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1311 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1312 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1313 | observer_->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1314 | return RTCError::OK(); |
| 1315 | } |
| 1316 | |
| 1317 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1318 | PeerConnection::FindTransceiverBySender( |
| 1319 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1320 | for (auto transceiver : transceivers_) { |
| 1321 | if (transceiver->sender() == sender) { |
| 1322 | return transceiver; |
| 1323 | } |
| 1324 | } |
| 1325 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1328 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1329 | PeerConnection::AddTransceiver( |
| 1330 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1331 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1332 | } |
| 1333 | |
| 1334 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1335 | PeerConnection::AddTransceiver( |
| 1336 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1337 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1338 | RTC_CHECK(IsUnifiedPlan()) |
| 1339 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1340 | if (!track) { |
| 1341 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1342 | } |
| 1343 | cricket::MediaType media_type; |
| 1344 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1345 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1346 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1347 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1348 | } else { |
| 1349 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1350 | "Track kind is not audio or video"); |
| 1351 | } |
| 1352 | return AddTransceiver(media_type, track, init); |
| 1353 | } |
| 1354 | |
| 1355 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1356 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1357 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1358 | } |
| 1359 | |
| 1360 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1361 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1362 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1363 | RTC_CHECK(IsUnifiedPlan()) |
| 1364 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1365 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1366 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1367 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1368 | "media type is not audio or video"); |
| 1369 | } |
| 1370 | return AddTransceiver(media_type, nullptr, init); |
| 1371 | } |
| 1372 | |
| 1373 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1374 | PeerConnection::AddTransceiver( |
| 1375 | cricket::MediaType media_type, |
| 1376 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1377 | const RtpTransceiverInit& init, |
| 1378 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1379 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1380 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1381 | if (track) { |
| 1382 | RTC_DCHECK_EQ(media_type, |
| 1383 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1384 | ? cricket::MEDIA_TYPE_AUDIO |
| 1385 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1386 | } |
| 1387 | |
| 1388 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1389 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1390 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1391 | << " transceiver in response to a call to AddTransceiver."; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1392 | auto sender = CreateSender(media_type, track, init.stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1393 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1394 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1395 | transceiver->internal()->set_direction(init.direction); |
| 1396 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1397 | if (fire_callback) { |
| 1398 | observer_->OnRenegotiationNeeded(); |
| 1399 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1400 | |
| 1401 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1402 | } |
| 1403 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1404 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1405 | PeerConnection::CreateSender( |
| 1406 | cricket::MediaType media_type, |
| 1407 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1408 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1409 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1410 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1411 | RTC_DCHECK(!track || |
| 1412 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1413 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1414 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1415 | new AudioRtpSender(worker_thread(), |
| 1416 | static_cast<AudioTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1417 | stream_ids, stats_.get())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1418 | } else { |
| 1419 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1420 | RTC_DCHECK(!track || |
| 1421 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1422 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1423 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1424 | new VideoRtpSender(worker_thread(), |
| 1425 | static_cast<VideoTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1426 | stream_ids)); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1427 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1428 | sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1429 | return sender; |
| 1430 | } |
| 1431 | |
| 1432 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1433 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1434 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1435 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1436 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1437 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1438 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 1439 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1440 | new AudioRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1441 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1442 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1443 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 1444 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1445 | new VideoRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1446 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1447 | return receiver; |
| 1448 | } |
| 1449 | |
| 1450 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1451 | PeerConnection::CreateAndAddTransceiver( |
| 1452 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1453 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1454 | receiver) { |
| 1455 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1456 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1457 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1458 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1459 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1460 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1463 | void PeerConnection::OnNegotiationNeeded() { |
| 1464 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1465 | RTC_DCHECK(!IsClosed()); |
| 1466 | observer_->OnRenegotiationNeeded(); |
| 1467 | } |
| 1468 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1469 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1470 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1471 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1472 | if (IsClosed()) { |
| 1473 | return nullptr; |
| 1474 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1475 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1476 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1477 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1478 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1479 | auto track_sender = FindSenderForTrack(track); |
| 1480 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1481 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1482 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1485 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1488 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1489 | const std::string& kind, |
| 1490 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1491 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1492 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1493 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1494 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1495 | if (IsClosed()) { |
| 1496 | return nullptr; |
| 1497 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1498 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1499 | // Internally we need to have one stream with Plan B semantics, so we |
| 1500 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1501 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1502 | if (stream_id.empty()) { |
| 1503 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1504 | RTC_LOG(LS_INFO) |
| 1505 | << "No stream_id specified for sender. Generated stream ID: " |
| 1506 | << stream_ids[0]; |
| 1507 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1508 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1511 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1512 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1513 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1514 | auto* audio_sender = |
| 1515 | new AudioRtpSender(worker_thread(), nullptr, stream_ids, stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1516 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1517 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1518 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1519 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1520 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1521 | auto* video_sender = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1522 | new VideoRtpSender(worker_thread(), nullptr, stream_ids); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1523 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1524 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1525 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1526 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1527 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1528 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1529 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1530 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1531 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1532 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1533 | } |
| 1534 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1535 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1536 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1537 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1538 | for (auto sender : GetSendersInternal()) { |
| 1539 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1540 | } |
| 1541 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1544 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1545 | PeerConnection::GetSendersInternal() const { |
| 1546 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1547 | all_senders; |
| 1548 | for (auto transceiver : transceivers_) { |
| 1549 | auto senders = transceiver->internal()->senders(); |
| 1550 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1551 | } |
| 1552 | return all_senders; |
| 1553 | } |
| 1554 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1555 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1556 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1557 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1558 | for (const auto& receiver : GetReceiversInternal()) { |
| 1559 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1560 | } |
| 1561 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1564 | std::vector< |
| 1565 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1566 | PeerConnection::GetReceiversInternal() const { |
| 1567 | std::vector< |
| 1568 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1569 | all_receivers; |
| 1570 | for (auto transceiver : transceivers_) { |
| 1571 | auto receivers = transceiver->internal()->receivers(); |
| 1572 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1573 | receivers.end()); |
| 1574 | } |
| 1575 | return all_receivers; |
| 1576 | } |
| 1577 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1578 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1579 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1580 | RTC_CHECK(IsUnifiedPlan()) |
| 1581 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1582 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1583 | for (auto transceiver : transceivers_) { |
| 1584 | all_transceivers.push_back(transceiver); |
| 1585 | } |
| 1586 | return all_transceivers; |
| 1587 | } |
| 1588 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1589 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1590 | MediaStreamTrackInterface* track, |
| 1591 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1592 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1593 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1594 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1595 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1596 | return false; |
| 1597 | } |
| 1598 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1599 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1600 | // The StatsCollector is used to tell if a track is valid because it may |
| 1601 | // remember tracks that the PeerConnection previously removed. |
| 1602 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1603 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1604 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1605 | return false; |
| 1606 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1607 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1608 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1609 | return true; |
| 1610 | } |
| 1611 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1612 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1613 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1614 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1615 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1616 | stats_collector_->GetStatsReport(callback); |
| 1617 | } |
| 1618 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1619 | void PeerConnection::GetStats( |
| 1620 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1621 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1622 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1623 | RTC_DCHECK(callback); |
| 1624 | RTC_DCHECK(stats_collector_); |
| 1625 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1626 | if (selector) { |
| 1627 | for (const auto& proxy_transceiver : transceivers_) { |
| 1628 | for (const auto& proxy_sender : |
| 1629 | proxy_transceiver->internal()->senders()) { |
| 1630 | if (proxy_sender == selector) { |
| 1631 | internal_sender = proxy_sender->internal(); |
| 1632 | break; |
| 1633 | } |
| 1634 | } |
| 1635 | if (internal_sender) |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | // If there is no |internal_sender|Â then |selector| is either null or does not |
| 1640 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1641 | // PeerConnection). This means that "all the stats objects representing the |
| 1642 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1643 | // produces an empty stats report. |
| 1644 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1645 | } |
| 1646 | |
| 1647 | void PeerConnection::GetStats( |
| 1648 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1649 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1650 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1651 | RTC_DCHECK(callback); |
| 1652 | RTC_DCHECK(stats_collector_); |
| 1653 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1654 | if (selector) { |
| 1655 | for (const auto& proxy_transceiver : transceivers_) { |
| 1656 | for (const auto& proxy_receiver : |
| 1657 | proxy_transceiver->internal()->receivers()) { |
| 1658 | if (proxy_receiver == selector) { |
| 1659 | internal_receiver = proxy_receiver->internal(); |
| 1660 | break; |
| 1661 | } |
| 1662 | } |
| 1663 | if (internal_receiver) |
| 1664 | break; |
| 1665 | } |
| 1666 | } |
| 1667 | // If there is no |internal_receiver|Â then |selector| is either null or does |
| 1668 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1669 | // the PeerConnection). This means that "all the stats objects representing |
| 1670 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1671 | // selector produces an empty stats report. |
| 1672 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1673 | } |
| 1674 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1675 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1676 | return signaling_state_; |
| 1677 | } |
| 1678 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1679 | PeerConnectionInterface::IceConnectionState |
| 1680 | PeerConnection::ice_connection_state() { |
| 1681 | return ice_connection_state_; |
| 1682 | } |
| 1683 | |
| 1684 | PeerConnectionInterface::IceGatheringState |
| 1685 | PeerConnection::ice_gathering_state() { |
| 1686 | return ice_gathering_state_; |
| 1687 | } |
| 1688 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1689 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1690 | PeerConnection::CreateDataChannel( |
| 1691 | const std::string& label, |
| 1692 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1693 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1694 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1695 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1696 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1697 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1698 | if (config) { |
| 1699 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1700 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1701 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1702 | InternalCreateDataChannel(label, internal_config.get())); |
| 1703 | if (!channel.get()) { |
| 1704 | return nullptr; |
| 1705 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1706 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1707 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1708 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1709 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1710 | observer_->OnRenegotiationNeeded(); |
| 1711 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1712 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1713 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1714 | } |
| 1715 | |
| 1716 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1717 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1718 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1719 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1720 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1721 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1722 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1723 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1724 | // |constraints| and some downstream applications might create offers with |
| 1725 | // mandatory fields which would not be parsed in the helper method. For |
| 1726 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1727 | // |constraints| as a mandatory field but it is not parsed. |
| 1728 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1729 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1730 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1734 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1735 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1736 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1737 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1738 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1739 | return; |
| 1740 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1741 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1742 | if (IsClosed()) { |
| 1743 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1744 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1745 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1746 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1747 | return; |
| 1748 | } |
| 1749 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1750 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1751 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1752 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1753 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1754 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1758 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1759 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1760 | if (IsUnifiedPlan()) { |
| 1761 | RTCError error = HandleLegacyOfferOptions(options); |
| 1762 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1763 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1764 | return; |
| 1765 | } |
| 1766 | } |
| 1767 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1768 | cricket::MediaSessionOptions session_options; |
| 1769 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1770 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1773 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1774 | const RTCOfferAnswerOptions& options) { |
| 1775 | RTC_DCHECK(IsUnifiedPlan()); |
| 1776 | |
| 1777 | if (options.offer_to_receive_audio == 0) { |
| 1778 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1779 | cricket::MEDIA_TYPE_AUDIO); |
| 1780 | } else if (options.offer_to_receive_audio == 1) { |
| 1781 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1782 | } else if (options.offer_to_receive_audio > 1) { |
| 1783 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1784 | "offer_to_receive_audio > 1 is not supported."); |
| 1785 | } |
| 1786 | |
| 1787 | if (options.offer_to_receive_video == 0) { |
| 1788 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1789 | cricket::MEDIA_TYPE_VIDEO); |
| 1790 | } else if (options.offer_to_receive_video == 1) { |
| 1791 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1792 | } else if (options.offer_to_receive_video > 1) { |
| 1793 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1794 | "offer_to_receive_video > 1 is not supported."); |
| 1795 | } |
| 1796 | |
| 1797 | return RTCError::OK(); |
| 1798 | } |
| 1799 | |
| 1800 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1801 | cricket::MediaType media_type) { |
| 1802 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1803 | RtpTransceiverDirection new_direction = |
| 1804 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 1805 | if (new_direction != transceiver->direction()) { |
| 1806 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 1807 | << " transceiver (MID=" |
| 1808 | << transceiver->mid().value_or("<not set>") << ") from " |
| 1809 | << RtpTransceiverDirectionToString( |
| 1810 | transceiver->direction()) |
| 1811 | << " to " |
| 1812 | << RtpTransceiverDirectionToString(new_direction) |
| 1813 | << " since CreateOffer specified offer_to_receive=0"; |
| 1814 | transceiver->internal()->set_direction(new_direction); |
| 1815 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1820 | cricket::MediaType media_type) { |
| 1821 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1822 | RTC_LOG(LS_INFO) |
| 1823 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 1824 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1825 | RtpTransceiverInit init; |
| 1826 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1827 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1832 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1833 | std::vector< |
| 1834 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1835 | receiving_transceivers; |
| 1836 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1837 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1838 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1839 | receiving_transceivers.push_back(transceiver); |
| 1840 | } |
| 1841 | } |
| 1842 | return receiving_transceivers; |
| 1843 | } |
| 1844 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1845 | void PeerConnection::CreateAnswer( |
| 1846 | CreateSessionDescriptionObserver* observer, |
| 1847 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1848 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1849 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1850 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1851 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1852 | return; |
| 1853 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1854 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1855 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1856 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1857 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1858 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1859 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1860 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1861 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1862 | return; |
| 1863 | } |
| 1864 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1865 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1869 | const RTCOfferAnswerOptions& options) { |
| 1870 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1871 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1872 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1873 | return; |
| 1874 | } |
| 1875 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1876 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1877 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1878 | std::string error = |
| 1879 | "PeerConnection cannot create an answer in a state other than " |
| 1880 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1881 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1882 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1883 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1884 | return; |
| 1885 | } |
| 1886 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1887 | // The remote description should be set if we're in the right state. |
| 1888 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1889 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1890 | if (IsUnifiedPlan()) { |
| 1891 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1892 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1893 | "supported with Unified Plan semantics. Use the " |
| 1894 | "RtpTransceiver API instead."; |
| 1895 | } |
| 1896 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1897 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1898 | "supported with Unified Plan semantics. Use the " |
| 1899 | "RtpTransceiver API instead."; |
| 1900 | } |
| 1901 | } |
| 1902 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1903 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1904 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1905 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1906 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | void PeerConnection::SetLocalDescription( |
| 1910 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1911 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1912 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1913 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1914 | // The SetLocalDescription contract is that we take ownership of the session |
| 1915 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1916 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1917 | // description as a unique_ptr argument to formalize this agreement. |
| 1918 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1919 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1920 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1921 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1922 | return; |
| 1923 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1924 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1925 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1926 | PostSetSessionDescriptionFailure( |
| 1927 | observer, |
| 1928 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1929 | return; |
| 1930 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1931 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1932 | // If a session error has occurred the PeerConnection is in a possibly |
| 1933 | // inconsistent state so fail right away. |
| 1934 | if (session_error() != SessionError::kNone) { |
| 1935 | std::string error_message = GetSessionErrorMsg(); |
| 1936 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1937 | PostSetSessionDescriptionFailure( |
| 1938 | observer, |
| 1939 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1940 | return; |
| 1941 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1942 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1943 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1944 | if (!error.ok()) { |
| 1945 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1946 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1947 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1948 | PostSetSessionDescriptionFailure( |
| 1949 | observer, |
| 1950 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1951 | return; |
| 1952 | } |
| 1953 | |
| 1954 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1955 | // which may destroy it before returning. |
| 1956 | const SdpType type = desc->GetType(); |
| 1957 | |
| 1958 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1959 | // |desc| may be destroyed at this point. |
| 1960 | |
| 1961 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1962 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1963 | // inconsistent state, so act conservatively here and set the session error |
| 1964 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1965 | SetSessionError(SessionError::kContent, error.message()); |
| 1966 | std::string error_message = |
| 1967 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1968 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1969 | PostSetSessionDescriptionFailure( |
| 1970 | observer, |
| 1971 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1972 | return; |
| 1973 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1974 | RTC_DCHECK(local_description()); |
| 1975 | |
| 1976 | PostSetSessionDescriptionSuccess(observer); |
| 1977 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1978 | // MaybeStartGathering needs to be called after posting |
| 1979 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1980 | // before signaling that SetLocalDescription completed. |
| 1981 | transport_controller_->MaybeStartGathering(); |
| 1982 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1983 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1984 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1985 | // MaybeStartGathering... |
| 1986 | network_thread()->Invoke<void>( |
| 1987 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1988 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 1989 | // Make UMA notes about what was agreed to. |
| 1990 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | RTCError PeerConnection::ApplyLocalDescription( |
| 1995 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1996 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1997 | RTC_DCHECK(desc); |
| 1998 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1999 | // Update stats here so that we have the most recent stats for tracks and |
| 2000 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2001 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2002 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2003 | // Take a reference to the old local description since it's used below to |
| 2004 | // compare against the new local description. When setting the new local |
| 2005 | // description, grab ownership of the replaced session description in case it |
| 2006 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2007 | // of the method. |
| 2008 | const SessionDescriptionInterface* old_local_description = |
| 2009 | local_description(); |
| 2010 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2011 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2012 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2013 | replaced_local_description = pending_local_description_ |
| 2014 | ? std::move(pending_local_description_) |
| 2015 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2016 | current_local_description_ = std::move(desc); |
| 2017 | pending_local_description_ = nullptr; |
| 2018 | current_remote_description_ = std::move(pending_remote_description_); |
| 2019 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2020 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2021 | pending_local_description_ = std::move(desc); |
| 2022 | } |
| 2023 | // The session description to apply now must be accessed by |
| 2024 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2025 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2026 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2027 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2028 | if (!error.ok()) { |
| 2029 | return error; |
| 2030 | } |
| 2031 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2032 | if (IsUnifiedPlan()) { |
| 2033 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2034 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2035 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2036 | if (!error.ok()) { |
| 2037 | return error; |
| 2038 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2039 | for (auto transceiver : transceivers_) { |
| 2040 | const ContentInfo* content = |
| 2041 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2042 | if (!content) { |
| 2043 | continue; |
| 2044 | } |
| 2045 | const MediaContentDescription* media_desc = content->media_description(); |
| 2046 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2047 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 2048 | } |
| 2049 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2050 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2051 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2052 | transceiver->Stop(); |
| 2053 | } |
| 2054 | } |
| 2055 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2056 | // Media channels will be created only when offer is set. These may use new |
| 2057 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2058 | if (type == SdpType::kOffer) { |
| 2059 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2060 | // description is applied. Restore back to old description. |
| 2061 | RTCError error = CreateChannels(*local_description()->description()); |
| 2062 | if (!error.ok()) { |
| 2063 | return error; |
| 2064 | } |
| 2065 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2066 | // Remove unused channels if MediaContentDescription is rejected. |
| 2067 | RemoveUnusedChannels(local_description()->description()); |
| 2068 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2069 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2070 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2071 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2072 | if (!error.ok()) { |
| 2073 | return error; |
| 2074 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2075 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2076 | if (remote_description()) { |
| 2077 | // Now that we have a local description, we can push down remote candidates. |
| 2078 | UseCandidatesInSessionDescription(remote_description()); |
| 2079 | } |
| 2080 | |
| 2081 | pending_ice_restarts_.clear(); |
| 2082 | if (session_error() != SessionError::kNone) { |
| 2083 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2084 | } |
| 2085 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2086 | // If setting the description decided our SSL role, allocate any necessary |
| 2087 | // SCTP sids. |
| 2088 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2089 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2090 | AllocateSctpSids(role); |
| 2091 | } |
| 2092 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2093 | if (IsUnifiedPlan()) { |
| 2094 | for (auto transceiver : transceivers_) { |
| 2095 | const ContentInfo* content = |
| 2096 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2097 | if (!content) { |
| 2098 | continue; |
| 2099 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2100 | const auto& streams = content->media_description()->streams(); |
| 2101 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2102 | transceiver->internal()->sender_internal()->set_stream_ids( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2103 | streams[0].stream_ids()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2104 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2105 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | } else { |
| 2109 | // Plan B semantics. |
| 2110 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2111 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2112 | // local session description. |
| 2113 | const cricket::ContentInfo* audio_content = |
| 2114 | GetFirstAudioContent(local_description()->description()); |
| 2115 | if (audio_content) { |
| 2116 | if (audio_content->rejected) { |
| 2117 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2118 | } else { |
| 2119 | const cricket::AudioContentDescription* audio_desc = |
| 2120 | audio_content->media_description()->as_audio(); |
| 2121 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2122 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2123 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2124 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2125 | const cricket::ContentInfo* video_content = |
| 2126 | GetFirstVideoContent(local_description()->description()); |
| 2127 | if (video_content) { |
| 2128 | if (video_content->rejected) { |
| 2129 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2130 | } else { |
| 2131 | const cricket::VideoContentDescription* video_desc = |
| 2132 | video_content->media_description()->as_video(); |
| 2133 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2134 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2135 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2139 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2140 | if (data_content) { |
| 2141 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2142 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2143 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2144 | cricket::kMediaProtocolRtpPrefix)) { |
| 2145 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2146 | } |
| 2147 | } |
| 2148 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2149 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2153 | SetSessionDescriptionObserver* observer, |
| 2154 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2155 | SetRemoteDescription( |
| 2156 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2157 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2158 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2159 | } |
| 2160 | |
| 2161 | void PeerConnection::SetRemoteDescription( |
| 2162 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2163 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2164 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2165 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2166 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2167 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2168 | return; |
| 2169 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2170 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2171 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2172 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2173 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2174 | return; |
| 2175 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2176 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2177 | // If a session error has occurred the PeerConnection is in a possibly |
| 2178 | // inconsistent state so fail right away. |
| 2179 | if (session_error() != SessionError::kNone) { |
| 2180 | std::string error_message = GetSessionErrorMsg(); |
| 2181 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2182 | observer->OnSetRemoteDescriptionComplete( |
| 2183 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2184 | return; |
| 2185 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2186 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2187 | if (desc->GetType() == SdpType::kOffer) { |
| 2188 | // Report to UMA the format of the received offer. |
| 2189 | ReportSdpFormatReceived(*desc); |
| 2190 | } |
| 2191 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2192 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2193 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2194 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2195 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2196 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2197 | observer->OnSetRemoteDescriptionComplete( |
| 2198 | RTCError(error.type(), std::move(error_message))); |
| 2199 | return; |
| 2200 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2201 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2202 | // Grab the description type before moving ownership to |
| 2203 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2204 | const SdpType type = desc->GetType(); |
| 2205 | |
| 2206 | error = ApplyRemoteDescription(std::move(desc)); |
| 2207 | // |desc| may be destroyed at this point. |
| 2208 | |
| 2209 | if (!error.ok()) { |
| 2210 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2211 | // inconsistent state, so act conservatively here and set the session error |
| 2212 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2213 | SetSessionError(SessionError::kContent, error.message()); |
| 2214 | std::string error_message = |
| 2215 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2216 | RTC_LOG(LS_ERROR) << error_message; |
| 2217 | observer->OnSetRemoteDescriptionComplete( |
| 2218 | RTCError(error.type(), std::move(error_message))); |
| 2219 | return; |
| 2220 | } |
| 2221 | RTC_DCHECK(remote_description()); |
| 2222 | |
| 2223 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2224 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2225 | // MaybeStartGathering... |
| 2226 | network_thread()->Invoke<void>( |
| 2227 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2228 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2229 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2230 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 2234 | } |
| 2235 | |
| 2236 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2237 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2238 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2239 | RTC_DCHECK(desc); |
| 2240 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2241 | // Update stats here so that we have the most recent stats for tracks and |
| 2242 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2243 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2244 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2245 | // Take a reference to the old remote description since it's used below to |
| 2246 | // compare against the new remote description. When setting the new remote |
| 2247 | // description, grab ownership of the replaced session description in case it |
| 2248 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2249 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2250 | const SessionDescriptionInterface* old_remote_description = |
| 2251 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2252 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2253 | SdpType type = desc->GetType(); |
| 2254 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2255 | replaced_remote_description = pending_remote_description_ |
| 2256 | ? std::move(pending_remote_description_) |
| 2257 | : std::move(current_remote_description_); |
| 2258 | current_remote_description_ = std::move(desc); |
| 2259 | pending_remote_description_ = nullptr; |
| 2260 | current_local_description_ = std::move(pending_local_description_); |
| 2261 | } else { |
| 2262 | replaced_remote_description = std::move(pending_remote_description_); |
| 2263 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2264 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2265 | // The session description to apply now must be accessed by |
| 2266 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2267 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2268 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2269 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2270 | if (!error.ok()) { |
| 2271 | return error; |
| 2272 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2273 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2274 | if (IsUnifiedPlan()) { |
| 2275 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2276 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2277 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2278 | if (!error.ok()) { |
| 2279 | return error; |
| 2280 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2281 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2282 | // Media channels will be created only when offer is set. These may use new |
| 2283 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2284 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2285 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2286 | // description is applied. Restore back to old description. |
| 2287 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2288 | if (!error.ok()) { |
| 2289 | return error; |
| 2290 | } |
| 2291 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2292 | // Remove unused channels if MediaContentDescription is rejected. |
| 2293 | RemoveUnusedChannels(remote_description()->description()); |
| 2294 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2295 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2296 | // NOTE: Candidates allocation will be initiated only when |
| 2297 | // SetLocalDescription is called. |
| 2298 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2299 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2300 | if (!error.ok()) { |
| 2301 | return error; |
| 2302 | } |
| 2303 | |
| 2304 | if (local_description() && |
| 2305 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2306 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2307 | } |
| 2308 | |
| 2309 | if (old_remote_description) { |
| 2310 | for (const cricket::ContentInfo& content : |
| 2311 | old_remote_description->description()->contents()) { |
| 2312 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2313 | // password that indicates the remote peer requests an ICE restart. |
| 2314 | // TODO(deadbeef): When we start storing both the current and pending |
| 2315 | // remote description, this should reset pending_ice_restarts and compare |
| 2316 | // against the current description. |
| 2317 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2318 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2319 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2320 | pending_ice_restarts_.insert(content.name); |
| 2321 | } |
| 2322 | } else { |
| 2323 | // We retain all received candidates only if ICE is not restarted. |
| 2324 | // When ICE is restarted, all previous candidates belong to an old |
| 2325 | // generation and should not be kept. |
| 2326 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2327 | // description should only contain candidates from the last set remote |
| 2328 | // description plus any candidates added since then. We should remove |
| 2329 | // this once we're sure it won't break anything. |
| 2330 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2331 | old_remote_description, content.name, mutable_remote_description()); |
| 2332 | } |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | if (session_error() != SessionError::kNone) { |
| 2337 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2338 | } |
| 2339 | |
| 2340 | // Set the the ICE connection state to connecting since the connection may |
| 2341 | // become writable with peer reflexive candidates before any remote candidate |
| 2342 | // is signaled. |
| 2343 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2344 | // is to have a new signal the indicates a change in checking state from the |
| 2345 | // transport and expose a new checking() member from transport that can be |
| 2346 | // read to determine the current checking state. The existing SignalConnecting |
| 2347 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2348 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2349 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2350 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2351 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2352 | } |
| 2353 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2354 | // If setting the description decided our SSL role, allocate any necessary |
| 2355 | // SCTP sids. |
| 2356 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2357 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2358 | AllocateSctpSids(role); |
| 2359 | } |
| 2360 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2361 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2362 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2363 | now_receiving_transceivers; |
| 2364 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2365 | no_longer_receiving_transceivers; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2366 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2367 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2368 | for (auto transceiver : transceivers_) { |
| 2369 | const ContentInfo* content = |
| 2370 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2371 | if (!content) { |
| 2372 | continue; |
| 2373 | } |
| 2374 | const MediaContentDescription* media_desc = content->media_description(); |
| 2375 | RtpTransceiverDirection local_direction = |
| 2376 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2377 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2378 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2379 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2380 | // process the addition of a remote track for the media description. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2381 | std::vector<std::string> stream_ids; |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2382 | if (!media_desc->streams().empty()) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2383 | // The remote description has signaled the stream IDs. |
| 2384 | stream_ids = media_desc->streams()[0].stream_ids(); |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2385 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2386 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
| 2387 | (!transceiver->current_direction() || |
| 2388 | !RtpTransceiverDirectionHasRecv( |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2389 | *transceiver->current_direction()))) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2390 | RTC_LOG(LS_INFO) << "Processing the addition of a new track for MID=" |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2391 | << content->name << " (added to " |
| 2392 | << GetStreamIdsString(stream_ids) << ")."; |
| 2393 | |
| 2394 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2395 | for (const std::string& stream_id : stream_ids) { |
| 2396 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2397 | remote_streams_->find(stream_id); |
| 2398 | if (!stream) { |
| 2399 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2400 | MediaStream::Create(stream_id)); |
| 2401 | remote_streams_->AddStream(stream); |
| 2402 | added_streams.push_back(stream); |
| 2403 | } |
| 2404 | media_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2405 | } |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2406 | // This will add the remote track to the streams. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2407 | transceiver->internal()->receiver_internal()->SetStreams(media_streams); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2408 | now_receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2409 | } |
| 2410 | // If direction is sendonly or inactive, and transceiver's current |
| 2411 | // direction is neither sendonly nor inactive, process the removal of a |
| 2412 | // remote track for the media description. |
| 2413 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2414 | (transceiver->current_direction() && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2415 | RtpTransceiverDirectionHasRecv(*transceiver->current_direction()))) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2416 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2417 | << content->name; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2418 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams = |
| 2419 | transceiver->internal()->receiver_internal()->streams(); |
| 2420 | // This will remove the remote track from the streams. |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2421 | transceiver->internal()->receiver_internal()->SetStreams({}); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2422 | no_longer_receiving_transceivers.push_back(transceiver); |
| 2423 | // Remove any streams that no longer have tracks. |
| 2424 | for (auto stream : media_streams) { |
| 2425 | if (stream->GetAudioTracks().empty() && |
| 2426 | stream->GetVideoTracks().empty()) { |
| 2427 | remote_streams_->RemoveStream(stream); |
| 2428 | removed_streams.push_back(stream); |
| 2429 | } |
| 2430 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2431 | } |
| 2432 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2433 | transceiver->internal()->set_current_direction(local_direction); |
| 2434 | } |
| 2435 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2436 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2437 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2438 | transceiver->Stop(); |
| 2439 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2440 | if (!content->rejected && |
| 2441 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2442 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2443 | uint32_t ssrc = 0; |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2444 | if (!media_desc->streams().empty() && |
| 2445 | media_desc->streams()[0].has_ssrcs()) { |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2446 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2447 | } |
| 2448 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2449 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2450 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2451 | // Once all processing has finished, fire off callbacks. |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2452 | for (auto transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2453 | stats_->AddTrack(transceiver->receiver()->track()); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2454 | observer_->OnTrack(transceiver); |
| 2455 | observer_->OnAddTrack(transceiver->receiver(), |
| 2456 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2457 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2458 | for (auto stream : added_streams) { |
| 2459 | observer_->OnAddStream(stream); |
| 2460 | } |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame^] | 2461 | for (auto transceiver : no_longer_receiving_transceivers) { |
| 2462 | observer_->OnRemoveTrack(transceiver->receiver()); |
| 2463 | } |
| 2464 | for (auto stream : removed_streams) { |
| 2465 | observer_->OnRemoveStream(stream); |
| 2466 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2467 | } |
| 2468 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2469 | const cricket::ContentInfo* audio_content = |
| 2470 | GetFirstAudioContent(remote_description()->description()); |
| 2471 | const cricket::ContentInfo* video_content = |
| 2472 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2473 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2474 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2475 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2476 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2477 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2478 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2479 | |
| 2480 | // Check if the descriptions include streams, just in case the peer supports |
| 2481 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2482 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2483 | (audio_desc && !audio_desc->streams().empty()) || |
| 2484 | (video_desc && !video_desc->streams().empty())) { |
| 2485 | remote_peer_supports_msid_ = true; |
| 2486 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2487 | |
| 2488 | // We wait to signal new streams until we finish processing the description, |
| 2489 | // since only at that point will new streams have all their tracks. |
| 2490 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2491 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2492 | if (!IsUnifiedPlan()) { |
| 2493 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2494 | // rejected media section, there is some cleanup logic that expects the |
| 2495 | // voice/ video channel to still be set. But in this method the voice/video |
| 2496 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2497 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2498 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2499 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2500 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2501 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2502 | // and MediaStreams. |
| 2503 | if (audio_content) { |
| 2504 | if (audio_content->rejected) { |
| 2505 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2506 | } else { |
| 2507 | bool default_audio_track_needed = |
| 2508 | !remote_peer_supports_msid_ && |
| 2509 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2510 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2511 | default_audio_track_needed, audio_desc->type(), |
| 2512 | new_streams); |
| 2513 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2514 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2515 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2516 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2517 | // and MediaStreams. |
| 2518 | if (video_content) { |
| 2519 | if (video_content->rejected) { |
| 2520 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2521 | } else { |
| 2522 | bool default_video_track_needed = |
| 2523 | !remote_peer_supports_msid_ && |
| 2524 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2525 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2526 | default_video_track_needed, video_desc->type(), |
| 2527 | new_streams); |
| 2528 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2529 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2530 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2531 | // Update the DataChannels with the information from the remote peer. |
| 2532 | if (data_desc) { |
| 2533 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2534 | cricket::kMediaProtocolRtpPrefix)) { |
| 2535 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2536 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2537 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2538 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2539 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 2540 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2541 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2542 | stats_->AddStream(new_stream); |
| 2543 | observer_->OnAddStream( |
| 2544 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2545 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2546 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2547 | UpdateEndedRemoteMediaStreams(); |
| 2548 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2549 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2550 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2553 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2554 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2555 | const SessionDescriptionInterface& new_session, |
| 2556 | const SessionDescriptionInterface* old_local_description, |
| 2557 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2558 | RTC_DCHECK(IsUnifiedPlan()); |
| 2559 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2560 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2561 | if (new_session.GetType() == SdpType::kOffer) { |
| 2562 | auto bundle_group_or_error = |
| 2563 | GetEarlyBundleGroup(*new_session.description()); |
| 2564 | if (!bundle_group_or_error.ok()) { |
| 2565 | return bundle_group_or_error.MoveError(); |
| 2566 | } |
| 2567 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2568 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2569 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2570 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2571 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2572 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2573 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2574 | seen_mids_.insert(new_content.name); |
| 2575 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2576 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2577 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2578 | if (old_local_description && |
| 2579 | i < old_local_description->description()->contents().size()) { |
| 2580 | old_local_content = |
| 2581 | &old_local_description->description()->contents()[i]; |
| 2582 | } |
| 2583 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2584 | if (old_remote_description && |
| 2585 | i < old_remote_description->description()->contents().size()) { |
| 2586 | old_remote_content = |
| 2587 | &old_remote_description->description()->contents()[i]; |
| 2588 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2589 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2590 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2591 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2592 | if (!transceiver_or_error.ok()) { |
| 2593 | return transceiver_or_error.MoveError(); |
| 2594 | } |
| 2595 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2596 | RTCError error = |
| 2597 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2598 | if (!error.ok()) { |
| 2599 | return error; |
| 2600 | } |
| 2601 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2602 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2603 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2604 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 2605 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2606 | continue; |
| 2607 | } |
| 2608 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2609 | if (!error.ok()) { |
| 2610 | return error; |
| 2611 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2612 | } else { |
| 2613 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2614 | "Unknown section type."); |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | return RTCError::OK(); |
| 2619 | } |
| 2620 | |
| 2621 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2622 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2623 | transceiver, |
| 2624 | const cricket::ContentInfo& content, |
| 2625 | const cricket::ContentGroup* bundle_group) { |
| 2626 | RTC_DCHECK(IsUnifiedPlan()); |
| 2627 | RTC_DCHECK(transceiver); |
| 2628 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2629 | if (content.rejected) { |
| 2630 | if (channel) { |
| 2631 | transceiver->internal()->SetChannel(nullptr); |
| 2632 | DestroyBaseChannel(channel); |
| 2633 | } |
| 2634 | } else { |
| 2635 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2636 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2637 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2638 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2639 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2640 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2641 | } |
| 2642 | if (!channel) { |
| 2643 | LOG_AND_RETURN_ERROR( |
| 2644 | RTCErrorType::INTERNAL_ERROR, |
| 2645 | "Failed to create channel for mid=" + content.name); |
| 2646 | } |
| 2647 | transceiver->internal()->SetChannel(channel); |
| 2648 | } |
| 2649 | } |
| 2650 | return RTCError::OK(); |
| 2651 | } |
| 2652 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2653 | RTCError PeerConnection::UpdateDataChannel( |
| 2654 | cricket::ContentSource source, |
| 2655 | const cricket::ContentInfo& content, |
| 2656 | const cricket::ContentGroup* bundle_group) { |
| 2657 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2658 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2659 | // will take care of rejecting it. |
| 2660 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2661 | } |
| 2662 | if (content.rejected) { |
| 2663 | DestroyDataChannel(); |
| 2664 | } else { |
| 2665 | if (!rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2666 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2667 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2668 | "Failed to create data channel."); |
| 2669 | } |
| 2670 | } |
| 2671 | if (source == cricket::CS_REMOTE) { |
| 2672 | const MediaContentDescription* data_desc = content.media_description(); |
| 2673 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2674 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2675 | } |
| 2676 | } |
| 2677 | } |
| 2678 | return RTCError::OK(); |
| 2679 | } |
| 2680 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2681 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2682 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2683 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2684 | size_t mline_index, |
| 2685 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2686 | const ContentInfo* old_local_content, |
| 2687 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2688 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2689 | // If this is an offer then the m= section might be recycled. If the m= |
| 2690 | // section is being recycled (defined as: rejected in the current local or |
| 2691 | // remote description and not rejected in new description), dissociate the |
| 2692 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2693 | // and discard the mapping between the transceiver and its m= section index. |
| 2694 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2695 | old_remote_content)) { |
| 2696 | // We want to dissociate the transceiver that has the rejected mid. |
| 2697 | const std::string& old_mid = |
| 2698 | (old_local_content && old_local_content->rejected) |
| 2699 | ? old_local_content->name |
| 2700 | : old_remote_content->name; |
| 2701 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2702 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2703 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 2704 | << " since the media section is being recycled."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2705 | old_transceiver->internal()->set_mid(rtc::nullopt); |
| 2706 | old_transceiver->internal()->set_mline_index(rtc::nullopt); |
| 2707 | } |
| 2708 | } |
| 2709 | const MediaContentDescription* media_desc = content.media_description(); |
| 2710 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2711 | if (source == cricket::CS_LOCAL) { |
| 2712 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2713 | // mapping between transceivers and m= section indices established when |
| 2714 | // creating the offer. |
| 2715 | if (!transceiver) { |
| 2716 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2717 | } |
| 2718 | if (!transceiver) { |
| 2719 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2720 | "Unknown transceiver"); |
| 2721 | } |
| 2722 | } else { |
| 2723 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2724 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2725 | // of the same type... |
| 2726 | if (!transceiver && |
| 2727 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2728 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2729 | } |
| 2730 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2731 | // recvonly direction. |
| 2732 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2733 | RTC_LOG(LS_INFO) << "Adding " |
| 2734 | << cricket::MediaTypeToString(media_desc->type()) |
| 2735 | << " transceiver for MID=" << content.name |
| 2736 | << " at i=" << mline_index |
| 2737 | << " in response to the remote description."; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2738 | auto sender = |
| 2739 | CreateSender(media_desc->type(), nullptr, {rtc::CreateRandomUuid()}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2740 | std::string receiver_id; |
| 2741 | if (!media_desc->streams().empty()) { |
| 2742 | receiver_id = media_desc->streams()[0].id; |
| 2743 | } else { |
| 2744 | receiver_id = rtc::CreateRandomUuid(); |
| 2745 | } |
| 2746 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2747 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2748 | transceiver->internal()->set_direction( |
| 2749 | RtpTransceiverDirection::kRecvOnly); |
| 2750 | } |
| 2751 | } |
| 2752 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2753 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2754 | LOG_AND_RETURN_ERROR( |
| 2755 | RTCErrorType::INVALID_PARAMETER, |
| 2756 | "Transceiver type does not match media description type."); |
| 2757 | } |
| 2758 | // Associate the found or created RtpTransceiver with the m= section by |
| 2759 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2760 | // section, and establish a mapping between the transceiver and the index of |
| 2761 | // the m= section. |
| 2762 | transceiver->internal()->set_mid(content.name); |
| 2763 | transceiver->internal()->set_mline_index(mline_index); |
| 2764 | return std::move(transceiver); |
| 2765 | } |
| 2766 | |
| 2767 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2768 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2769 | RTC_DCHECK(IsUnifiedPlan()); |
| 2770 | for (auto transceiver : transceivers_) { |
| 2771 | if (transceiver->mid() == mid) { |
| 2772 | return transceiver; |
| 2773 | } |
| 2774 | } |
| 2775 | return nullptr; |
| 2776 | } |
| 2777 | |
| 2778 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2779 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2780 | RTC_DCHECK(IsUnifiedPlan()); |
| 2781 | for (auto transceiver : transceivers_) { |
| 2782 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2783 | return transceiver; |
| 2784 | } |
| 2785 | } |
| 2786 | return nullptr; |
| 2787 | } |
| 2788 | |
| 2789 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2790 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2791 | cricket::MediaType media_type) const { |
| 2792 | RTC_DCHECK(IsUnifiedPlan()); |
| 2793 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2794 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2795 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2796 | // associated with any m= section and are not stopped, find the first such |
| 2797 | // RtpTransceiver. |
| 2798 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2799 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2800 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2801 | !transceiver->stopped()) { |
| 2802 | return transceiver; |
| 2803 | } |
| 2804 | } |
| 2805 | return nullptr; |
| 2806 | } |
| 2807 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2808 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2809 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2810 | transceiver, |
| 2811 | const SessionDescriptionInterface* sdesc) const { |
| 2812 | RTC_DCHECK(transceiver); |
| 2813 | RTC_DCHECK(sdesc); |
| 2814 | if (IsUnifiedPlan()) { |
| 2815 | if (!transceiver->internal()->mid()) { |
| 2816 | // This transceiver is not associated with a media section yet. |
| 2817 | return nullptr; |
| 2818 | } |
| 2819 | return sdesc->description()->GetContentByName( |
| 2820 | *transceiver->internal()->mid()); |
| 2821 | } else { |
| 2822 | // Plan B only allows at most one audio and one video section, so use the |
| 2823 | // first media section of that type. |
| 2824 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2825 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2826 | } |
| 2827 | } |
| 2828 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2829 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2830 | return configuration_; |
| 2831 | } |
| 2832 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2833 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2834 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2835 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2836 | |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2837 | if (IsClosed()) { |
| 2838 | RTC_LOG(LS_ERROR) << "SetConfiguration: PeerConnection is closed."; |
| 2839 | return SafeSetError(RTCErrorType::INVALID_STATE, error); |
| 2840 | } |
| 2841 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 2842 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 2843 | // size is not allowed, and changing the set of ICE servers will not result |
| 2844 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2845 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2846 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2847 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2848 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2849 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2850 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2851 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2852 | // The simplest (and most future-compatible) way to tell if the config was |
| 2853 | // modified in an invalid way is to copy each property we do support |
| 2854 | // modifying, then use operator==. There are far more properties we don't |
| 2855 | // support modifying than those we do, and more could be added. |
| 2856 | RTCConfiguration modified_config = configuration_; |
| 2857 | modified_config.servers = configuration.servers; |
| 2858 | modified_config.type = configuration.type; |
| 2859 | modified_config.ice_candidate_pool_size = |
| 2860 | configuration.ice_candidate_pool_size; |
| 2861 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2862 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 2863 | modified_config.ice_check_interval_strong_connectivity = |
| 2864 | configuration.ice_check_interval_strong_connectivity; |
| 2865 | modified_config.ice_check_interval_weak_connectivity = |
| 2866 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2867 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 2868 | modified_config.ice_unwritable_min_checks = |
| 2869 | configuration.ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2870 | modified_config.stun_candidate_keepalive_interval = |
| 2871 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2872 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2873 | modified_config.network_preference = configuration.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2874 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2875 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2876 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2877 | } |
| 2878 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2879 | // Validate the modified configuration. |
| 2880 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2881 | if (!validate_error.ok()) { |
| 2882 | return SafeSetError(std::move(validate_error), error); |
| 2883 | } |
| 2884 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2885 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2886 | // short in WebIDL. |
| 2887 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 2888 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2889 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2890 | } |
| 2891 | |
| 2892 | // Parse ICE servers before hopping to network thread. |
| 2893 | cricket::ServerAddresses stun_servers; |
| 2894 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2895 | RTCErrorType parse_error = |
| 2896 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2897 | if (parse_error != RTCErrorType::NONE) { |
| 2898 | return SafeSetError(parse_error, error); |
| 2899 | } |
| 2900 | |
| 2901 | // In theory this shouldn't fail. |
| 2902 | if (!network_thread()->Invoke<bool>( |
| 2903 | RTC_FROM_HERE, |
| 2904 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2905 | stun_servers, turn_servers, modified_config.type, |
| 2906 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2907 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2908 | modified_config.turn_customizer, |
| 2909 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2910 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2911 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2912 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2913 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2914 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2915 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2916 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2917 | if (modified_config.servers != configuration_.servers || |
| 2918 | modified_config.type != configuration_.type || |
| 2919 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2920 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2921 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2922 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 2923 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2924 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2925 | configuration_ = modified_config; |
| 2926 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2929 | bool PeerConnection::AddIceCandidate( |
| 2930 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2931 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2932 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2933 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2934 | return false; |
| 2935 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2936 | |
| 2937 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2938 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2939 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2940 | return false; |
| 2941 | } |
| 2942 | |
| 2943 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2944 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2945 | return false; |
| 2946 | } |
| 2947 | |
| 2948 | bool valid = false; |
| 2949 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2950 | if (!valid) { |
| 2951 | return false; |
| 2952 | } |
| 2953 | |
| 2954 | // Add this candidate to the remote session description. |
| 2955 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2956 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2957 | return false; |
| 2958 | } |
| 2959 | |
| 2960 | if (ready) { |
| 2961 | return UseCandidate(ice_candidate); |
| 2962 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2963 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2964 | return true; |
| 2965 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2968 | bool PeerConnection::RemoveIceCandidates( |
| 2969 | const std::vector<cricket::Candidate>& candidates) { |
| 2970 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2971 | if (IsClosed()) { |
| 2972 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 2973 | return false; |
| 2974 | } |
| 2975 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2976 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2977 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 2978 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2979 | return false; |
| 2980 | } |
| 2981 | |
| 2982 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2983 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2984 | return false; |
| 2985 | } |
| 2986 | |
| 2987 | size_t number_removed = |
| 2988 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2989 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2990 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2991 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2992 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2993 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2997 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 2998 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2999 | RTC_LOG(LS_ERROR) |
| 3000 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3001 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3002 | } |
| 3003 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3004 | } |
| 3005 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 3006 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3007 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3008 | network_thread()->Invoke<void>( |
| 3009 | RTC_FROM_HERE, |
| 3010 | rtc::Bind(&PeerConnection::SetMetricObserver_n, this, observer)); |
| 3011 | // Send information about IPv4/IPv6 status. |
| 3012 | if (uma_observer_) { |
| 3013 | if (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
| 3014 | uma_observer_->IncrementEnumCounter( |
| 3015 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 3016 | kPeerConnectionAddressFamilyCounter_Max); |
| 3017 | } else { |
| 3018 | uma_observer_->IncrementEnumCounter( |
| 3019 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 3020 | kPeerConnectionAddressFamilyCounter_Max); |
| 3021 | } |
| 3022 | } |
| 3023 | } |
Patrik Höglund | 3dc4106 | 2018-04-11 11:13:57 +0000 | [diff] [blame] | 3024 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3025 | void PeerConnection::SetMetricObserver_n(UMAObserver* observer) { |
| 3026 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 3027 | uma_observer_ = observer; |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3028 | if (transport_controller_) { |
| 3029 | transport_controller_->SetMetricsObserver(uma_observer_); |
Patrik Höglund | 3dc4106 | 2018-04-11 11:13:57 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | for (auto transceiver : transceivers_) { |
| 3033 | auto* channel = transceiver->internal()->channel(); |
| 3034 | if (channel) { |
| 3035 | channel->SetMetricsObserver(uma_observer_); |
| 3036 | } |
| 3037 | } |
| 3038 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3039 | if (uma_observer_) { |
Patrik Höglund | 3dc4106 | 2018-04-11 11:13:57 +0000 | [diff] [blame] | 3040 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 3041 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3044 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3045 | if (!worker_thread()->IsCurrent()) { |
| 3046 | return worker_thread()->Invoke<RTCError>( |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3047 | RTC_FROM_HERE, [&](){ return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3050 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3051 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3052 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3053 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3054 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3055 | "min_bitrate_bps <= 0"); |
| 3056 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3057 | if (has_start) { |
| 3058 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3059 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3060 | "start_bitrate_bps < min_bitrate_bps"); |
| 3061 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3062 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3063 | "curent_bitrate_bps < 0"); |
| 3064 | } |
| 3065 | } |
| 3066 | if (has_max) { |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3067 | if (has_start && |
| 3068 | *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3069 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3070 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3071 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3072 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3073 | "max_bitrate_bps < min_bitrate_bps"); |
| 3074 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3075 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3076 | "max_bitrate_bps < 0"); |
| 3077 | } |
| 3078 | } |
| 3079 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3080 | RTC_DCHECK(call_.get()); |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3081 | call_->GetTransportControllerSend()->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3082 | |
| 3083 | return RTCError::OK(); |
| 3084 | } |
| 3085 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3086 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3087 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3088 | bitrate_allocation_strategy) { |
| 3089 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3090 | if (!worker_thread->IsCurrent()) { |
| 3091 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3092 | bitrate_allocation_strategy.release(); |
| 3093 | auto functor = [this, strategy_raw]() { |
| 3094 | call_->SetBitrateAllocationStrategy( |
| 3095 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 3096 | }; |
| 3097 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3098 | return; |
| 3099 | } |
| 3100 | RTC_DCHECK(call_.get()); |
| 3101 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3102 | } |
| 3103 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3104 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3105 | if (!worker_thread()->IsCurrent()) { |
| 3106 | worker_thread()->Invoke<void>( |
| 3107 | RTC_FROM_HERE, |
| 3108 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3109 | return; |
| 3110 | } |
| 3111 | auto audio_state = |
| 3112 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3113 | audio_state->SetPlayout(playout); |
| 3114 | } |
| 3115 | |
| 3116 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3117 | if (!worker_thread()->IsCurrent()) { |
| 3118 | worker_thread()->Invoke<void>( |
| 3119 | RTC_FROM_HERE, |
| 3120 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3121 | return; |
| 3122 | } |
| 3123 | auto audio_state = |
| 3124 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3125 | audio_state->SetRecording(recording); |
| 3126 | } |
| 3127 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3128 | std::unique_ptr<rtc::SSLCertificate> |
| 3129 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3130 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3131 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3132 | return nullptr; |
| 3133 | } |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3134 | return chain->Get(0).GetUniqueReference(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3137 | std::unique_ptr<rtc::SSLCertChain> |
| 3138 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3139 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3140 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3141 | return nullptr; |
| 3142 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3143 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3144 | audio_transceiver->internal()->channel()->transport_name()); |
| 3145 | } |
| 3146 | |
| 3147 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3148 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3149 | for (auto transceiver : transceivers_) { |
| 3150 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3151 | return transceiver; |
| 3152 | } |
| 3153 | } |
| 3154 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3155 | } |
| 3156 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3157 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3158 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3159 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3160 | const size_t max_size = (max_size_bytes < 0) |
| 3161 | ? RtcEventLog::kUnlimitedOutput |
| 3162 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 3163 | return StartRtcEventLog( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3164 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size), |
| 3165 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3166 | } |
| 3167 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3168 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3169 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3170 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3171 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3172 | bool operator()() { |
| 3173 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3174 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3175 | PeerConnection* const pc; |
| 3176 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3177 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3178 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3179 | return worker_thread()->Invoke<bool>( |
| 3180 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3184 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3185 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3186 | } |
| 3187 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3188 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3189 | return pending_local_description_ ? pending_local_description_.get() |
| 3190 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3194 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3195 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3198 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3199 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3200 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3204 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3205 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3209 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3210 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3214 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3215 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3216 | } |
| 3217 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3218 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3219 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3220 | // Update stats here so that we have the most recent stats for tracks and |
| 3221 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3222 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3223 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3224 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3225 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3226 | for (auto transceiver : transceivers_) { |
| 3227 | transceiver->Stop(); |
| 3228 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 3229 | |
| 3230 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3231 | // the transport controller below. |
| 3232 | if (stats_collector_) { |
| 3233 | stats_collector_->WaitForPendingRequest(); |
| 3234 | } |
| 3235 | |
| 3236 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3237 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3238 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3239 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3240 | // The event log is used in the transport controller, which must be outlived |
| 3241 | // by the former. CreateOffer by the peer connection is implemented |
| 3242 | // asynchronously and if the peer connection is closed without resetting the |
| 3243 | // WebRTC session description factory, the session description factory would |
| 3244 | // call the transport controller. |
| 3245 | webrtc_session_desc_factory_.reset(); |
| 3246 | transport_controller_.reset(); |
| 3247 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3248 | network_thread()->Invoke<void>( |
| 3249 | RTC_FROM_HERE, |
| 3250 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3251 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3252 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3253 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3254 | call_.reset(); |
| 3255 | // The event log must outlive call (and any other object that uses it). |
| 3256 | event_log_.reset(); |
| 3257 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3260 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3261 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3262 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3263 | SetSessionDescriptionMsg* param = |
| 3264 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3265 | param->observer->OnSuccess(); |
| 3266 | delete param; |
| 3267 | break; |
| 3268 | } |
| 3269 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3270 | SetSessionDescriptionMsg* param = |
| 3271 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3272 | param->observer->OnFailure(std::move(param->error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3273 | delete param; |
| 3274 | break; |
| 3275 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3276 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3277 | CreateSessionDescriptionMsg* param = |
| 3278 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3279 | param->observer->OnFailure(std::move(param->error)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3280 | delete param; |
| 3281 | break; |
| 3282 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3283 | case MSG_GETSTATS: { |
| 3284 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 3285 | StatsReports reports; |
| 3286 | stats_->GetStats(param->track, &reports); |
| 3287 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3288 | delete param; |
| 3289 | break; |
| 3290 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 3291 | case MSG_FREE_DATACHANNELS: { |
| 3292 | sctp_data_channels_to_free_.clear(); |
| 3293 | break; |
| 3294 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3295 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3296 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3297 | break; |
| 3298 | } |
| 3299 | } |
| 3300 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3301 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3302 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3303 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3304 | GetAudioTransceiver()->internal()->channel()); |
| 3305 | if (voice_channel) { |
| 3306 | return voice_channel->media_channel(); |
| 3307 | } else { |
| 3308 | return nullptr; |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3313 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3314 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3315 | GetVideoTransceiver()->internal()->channel()); |
| 3316 | if (video_channel) { |
| 3317 | return video_channel->media_channel(); |
| 3318 | } else { |
| 3319 | return nullptr; |
| 3320 | } |
| 3321 | } |
| 3322 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3323 | void PeerConnection::CreateAudioReceiver( |
| 3324 | MediaStreamInterface* stream, |
| 3325 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3326 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3327 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3328 | auto* audio_receiver = new AudioRtpReceiver( |
| 3329 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3330 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3331 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3332 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3333 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3334 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3335 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3338 | void PeerConnection::CreateVideoReceiver( |
| 3339 | MediaStreamInterface* stream, |
| 3340 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3341 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3342 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3343 | auto* video_receiver = new VideoRtpReceiver( |
| 3344 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3345 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3346 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3347 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3348 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3349 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3350 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3353 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3354 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3355 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3356 | const RtpSenderInfo& remote_sender_info) { |
| 3357 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3358 | if (!receiver) { |
| 3359 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3360 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3361 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3362 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3363 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3364 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3365 | } else { |
| 3366 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3367 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3368 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3371 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3372 | MediaStreamInterface* stream) { |
| 3373 | RTC_DCHECK(!IsClosed()); |
| 3374 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3375 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3376 | // We already have a sender for this track, so just change the stream_id |
| 3377 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3378 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3379 | return; |
| 3380 | } |
| 3381 | |
| 3382 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3383 | auto new_sender = |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 3384 | CreateSender(cricket::MEDIA_TYPE_AUDIO, track, {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3385 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3386 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3387 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3388 | // which will connect the sender to the underlying transport. This can |
| 3389 | // occur if a local session description that contains the ID of the sender |
| 3390 | // is set before AddStream is called. It can also occur if the local |
| 3391 | // session description is not changed and RemoveStream is called, and |
| 3392 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3393 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3394 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3395 | if (sender_info) { |
| 3396 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3401 | // indefinitely, when we have unified plan SDP. |
| 3402 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3403 | MediaStreamInterface* stream) { |
| 3404 | RTC_DCHECK(!IsClosed()); |
| 3405 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3406 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3407 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3408 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3409 | return; |
| 3410 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3411 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3415 | MediaStreamInterface* stream) { |
| 3416 | RTC_DCHECK(!IsClosed()); |
| 3417 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3418 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3419 | // We already have a sender for this track, so just change the stream_id |
| 3420 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3421 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3422 | return; |
| 3423 | } |
| 3424 | |
| 3425 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3426 | auto new_sender = |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 3427 | CreateSender(cricket::MEDIA_TYPE_VIDEO, track, {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3428 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3429 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3430 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3431 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3432 | if (sender_info) { |
| 3433 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3438 | MediaStreamInterface* stream) { |
| 3439 | RTC_DCHECK(!IsClosed()); |
| 3440 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3441 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3442 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3443 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3444 | return; |
| 3445 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3446 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3447 | } |
| 3448 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3449 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3450 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3451 | if (ice_connection_state_ == new_state) { |
| 3452 | return; |
| 3453 | } |
| 3454 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3455 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3456 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3457 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3458 | return; |
| 3459 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3460 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3461 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3462 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3463 | RTC_DCHECK(ice_connection_state_ != |
| 3464 | PeerConnectionInterface::kIceConnectionClosed); |
| 3465 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3466 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3467 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | void PeerConnection::OnIceGatheringChange( |
| 3471 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3472 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3473 | if (IsClosed()) { |
| 3474 | return; |
| 3475 | } |
| 3476 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3477 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3480 | void PeerConnection::OnIceCandidate( |
| 3481 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3482 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3483 | if (IsClosed()) { |
| 3484 | return; |
| 3485 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3486 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3489 | void PeerConnection::OnIceCandidatesRemoved( |
| 3490 | const std::vector<cricket::Candidate>& candidates) { |
| 3491 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3492 | if (IsClosed()) { |
| 3493 | return; |
| 3494 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3495 | observer_->OnIceCandidatesRemoved(candidates); |
| 3496 | } |
| 3497 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3498 | void PeerConnection::ChangeSignalingState( |
| 3499 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3500 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3501 | if (signaling_state_ == signaling_state) { |
| 3502 | return; |
| 3503 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3504 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3505 | << GetSignalingStateString(signaling_state_) |
| 3506 | << " New state: " |
| 3507 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3508 | signaling_state_ = signaling_state; |
| 3509 | if (signaling_state == kClosed) { |
| 3510 | ice_connection_state_ = kIceConnectionClosed; |
| 3511 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 3512 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3513 | ice_gathering_state_ = kIceGatheringComplete; |
| 3514 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 3515 | } |
| 3516 | } |
| 3517 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3520 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3521 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3522 | if (IsClosed()) { |
| 3523 | return; |
| 3524 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3525 | AddAudioTrack(track, stream); |
| 3526 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3527 | } |
| 3528 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3529 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3530 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3531 | if (IsClosed()) { |
| 3532 | return; |
| 3533 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3534 | RemoveAudioTrack(track, stream); |
| 3535 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3539 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3540 | if (IsClosed()) { |
| 3541 | return; |
| 3542 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3543 | AddVideoTrack(track, stream); |
| 3544 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3548 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3549 | if (IsClosed()) { |
| 3550 | return; |
| 3551 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3552 | RemoveVideoTrack(track, stream); |
| 3553 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3554 | } |
| 3555 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3556 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3557 | SetSessionDescriptionObserver* observer) { |
| 3558 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3559 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3560 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 3561 | } |
| 3562 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3563 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3564 | SetSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3565 | RTCError&& error) { |
| 3566 | RTC_DCHECK(!error.ok()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3567 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3568 | msg->error = std::move(error); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3569 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3570 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3574 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3575 | RTCError error) { |
| 3576 | RTC_DCHECK(!error.ok()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3577 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3578 | msg->error = std::move(error); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3579 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3580 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3583 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3584 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3585 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3586 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3587 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3588 | if (IsUnifiedPlan()) { |
| 3589 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3590 | } else { |
| 3591 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3592 | } |
| 3593 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3594 | // Intentionally unset the data channel type for RTP data channel with the |
| 3595 | // second condition. Otherwise the RTP data channels would be successfully |
| 3596 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3597 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3598 | // people won't try to use them. |
| 3599 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3600 | session_options->data_channel_type = data_channel_type(); |
| 3601 | } |
| 3602 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3603 | // Apply ICE restart flag and renomination flag. |
| 3604 | for (auto& options : session_options->media_description_options) { |
| 3605 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3606 | options.transport_options.enable_ice_renomination = |
| 3607 | configuration_.enable_ice_renomination; |
| 3608 | } |
| 3609 | |
| 3610 | session_options->rtcp_cname = rtcp_cname_; |
| 3611 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3612 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3616 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3617 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3618 | // Figure out transceiver directional preferences. |
| 3619 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3620 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3621 | |
| 3622 | // By default, generate sendrecv/recvonly m= sections. |
| 3623 | bool recv_audio = true; |
| 3624 | bool recv_video = true; |
| 3625 | |
| 3626 | // By default, only offer a new m= section if we have media to send with it. |
| 3627 | bool offer_new_audio_description = send_audio; |
| 3628 | bool offer_new_video_description = send_video; |
| 3629 | bool offer_new_data_description = HasDataChannels(); |
| 3630 | |
| 3631 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3632 | if (offer_answer_options.offer_to_receive_audio != |
| 3633 | RTCOfferAnswerOptions::kUndefined) { |
| 3634 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3635 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3636 | offer_new_audio_description || |
| 3637 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3638 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3639 | if (offer_answer_options.offer_to_receive_video != |
| 3640 | RTCOfferAnswerOptions::kUndefined) { |
| 3641 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3642 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3643 | offer_new_video_description || |
| 3644 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | rtc::Optional<size_t> audio_index; |
| 3648 | rtc::Optional<size_t> video_index; |
| 3649 | rtc::Optional<size_t> data_index; |
| 3650 | // If a current description exists, generate m= sections in the same order, |
| 3651 | // using the first audio/video/data section that appears and rejecting |
| 3652 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3653 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3654 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3655 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3656 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3657 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3658 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3659 | } |
| 3660 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3661 | // Add audio/video/data m= sections to the end if needed. |
| 3662 | if (!audio_index && offer_new_audio_description) { |
| 3663 | session_options->media_description_options.push_back( |
| 3664 | cricket::MediaDescriptionOptions( |
| 3665 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3666 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3667 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3668 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3669 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3670 | if (!video_index && offer_new_video_description) { |
| 3671 | session_options->media_description_options.push_back( |
| 3672 | cricket::MediaDescriptionOptions( |
| 3673 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3674 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3675 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3676 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3677 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3678 | if (!data_index && offer_new_data_description) { |
| 3679 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3680 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3681 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3685 | !audio_index ? nullptr |
| 3686 | : &session_options->media_description_options[*audio_index]; |
| 3687 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3688 | !video_index ? nullptr |
| 3689 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3690 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3691 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3692 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3696 | // and return a reference to it. |
| 3697 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3698 | // the RTP packet. |
| 3699 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3700 | RTC_DCHECK(used_mids); |
| 3701 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3702 | size_t i = 0; |
| 3703 | std::set<std::string>::iterator it; |
| 3704 | bool inserted; |
| 3705 | do { |
| 3706 | std::string mid = rtc::ToString(i++); |
| 3707 | auto insert_result = used_mids->insert(mid); |
| 3708 | it = insert_result.first; |
| 3709 | inserted = insert_result.second; |
| 3710 | } while (!inserted); |
| 3711 | return *it; |
| 3712 | } |
| 3713 | |
| 3714 | static cricket::MediaDescriptionOptions |
| 3715 | GetMediaDescriptionOptionsForTransceiver( |
| 3716 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3717 | transceiver, |
| 3718 | const std::string& mid) { |
| 3719 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3720 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3721 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3722 | // This behavior is specified in JSEP. The gist is that: |
| 3723 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3724 | // sendrecv. |
| 3725 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3726 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3727 | if (!transceiver->stopped() && |
| 3728 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3729 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3730 | cricket::SenderOptions sender_options; |
| 3731 | sender_options.track_id = transceiver->sender()->id(); |
| 3732 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3733 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3734 | // set in the RTP parameters when the transceiver was added. |
| 3735 | sender_options.num_sim_layers = 1; |
| 3736 | media_description_options.sender_options.push_back(sender_options); |
| 3737 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3738 | return media_description_options; |
| 3739 | } |
| 3740 | |
| 3741 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3742 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3743 | cricket::MediaSessionOptions* session_options) { |
| 3744 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3745 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3746 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3747 | const ContentInfos& local_contents = |
| 3748 | (local_description() ? local_description()->description()->contents() |
| 3749 | : ContentInfos()); |
| 3750 | const ContentInfos& remote_contents = |
| 3751 | (remote_description() ? remote_description()->description()->contents() |
| 3752 | : ContentInfos()); |
| 3753 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3754 | // slots first. |
| 3755 | std::queue<size_t> recycleable_mline_indices; |
| 3756 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3757 | // offer so that new, unique MIDs are generated. |
| 3758 | std::set<std::string> used_mids = seen_mids_; |
| 3759 | // First, go through each media section that exists in either the local or |
| 3760 | // remote description and generate a media section in this offer for the |
| 3761 | // associated transceiver. If a media section can be recycled, generate a |
| 3762 | // default, rejected media section here that can be later overwritten. |
| 3763 | for (size_t i = 0; |
| 3764 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3765 | // Either |local_content| or |remote_content| is non-null. |
| 3766 | const ContentInfo* local_content = |
| 3767 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3768 | const ContentInfo* remote_content = |
| 3769 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3770 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3771 | (remote_content && remote_content->rejected); |
| 3772 | const std::string& mid = |
| 3773 | (local_content ? local_content->name : remote_content->name); |
| 3774 | cricket::MediaType media_type = |
| 3775 | (local_content ? local_content->media_description()->type() |
| 3776 | : remote_content->media_description()->type()); |
| 3777 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3778 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3779 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3780 | RTC_CHECK(transceiver); |
| 3781 | // A media section is considered eligible for recycling if it is marked as |
| 3782 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3783 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3784 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3785 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3786 | RtpTransceiverDirection::kInactive, |
| 3787 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3788 | recycleable_mline_indices.push(i); |
| 3789 | } else { |
| 3790 | session_options->media_description_options.push_back( |
| 3791 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3792 | // CreateOffer shouldn't really cause any state changes in |
| 3793 | // PeerConnection, but we need a way to match new transceivers to new |
| 3794 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3795 | // by recording the index of the media section generated for the |
| 3796 | // transceiver in the offer. |
| 3797 | transceiver->internal()->set_mline_index(i); |
| 3798 | } |
| 3799 | } else { |
| 3800 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3801 | RTC_CHECK(GetDataMid()); |
| 3802 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3803 | session_options->media_description_options.push_back( |
| 3804 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3805 | } else { |
| 3806 | session_options->media_description_options.push_back( |
| 3807 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3808 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3812 | // and not associated). Reuse media sections marked as recyclable first, |
| 3813 | // otherwise append to the end of the offer. New media sections should be |
| 3814 | // added in the order they were added to the PeerConnection. |
| 3815 | for (auto transceiver : transceivers_) { |
| 3816 | if (transceiver->mid() || transceiver->stopped()) { |
| 3817 | continue; |
| 3818 | } |
| 3819 | size_t mline_index; |
| 3820 | if (!recycleable_mline_indices.empty()) { |
| 3821 | mline_index = recycleable_mline_indices.front(); |
| 3822 | recycleable_mline_indices.pop(); |
| 3823 | session_options->media_description_options[mline_index] = |
| 3824 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3825 | AllocateMid(&used_mids)); |
| 3826 | } else { |
| 3827 | mline_index = session_options->media_description_options.size(); |
| 3828 | session_options->media_description_options.push_back( |
| 3829 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3830 | AllocateMid(&used_mids))); |
| 3831 | } |
| 3832 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3833 | transceiver->internal()->set_mline_index(mline_index); |
| 3834 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3835 | // Lastly, add a m-section if we have local data channels and an m section |
| 3836 | // does not already exist. |
| 3837 | if (!GetDataMid() && HasDataChannels()) { |
| 3838 | session_options->media_description_options.push_back( |
| 3839 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3840 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | void PeerConnection::GetOptionsForAnswer( |
| 3844 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3845 | cricket::MediaSessionOptions* session_options) { |
| 3846 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3847 | |
| 3848 | if (IsUnifiedPlan()) { |
| 3849 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3850 | } else { |
| 3851 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3852 | } |
| 3853 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3854 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3855 | // the RTP data channels would be successfully negotiated by default and the |
| 3856 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3857 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3858 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3859 | session_options->data_channel_type = data_channel_type(); |
| 3860 | } |
| 3861 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3862 | // Apply ICE renomination flag. |
| 3863 | for (auto& options : session_options->media_description_options) { |
| 3864 | options.transport_options.enable_ice_renomination = |
| 3865 | configuration_.enable_ice_renomination; |
| 3866 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3867 | |
| 3868 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3869 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3870 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3871 | } |
| 3872 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3873 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3874 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3875 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3876 | // Figure out transceiver directional preferences. |
| 3877 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3878 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3879 | |
| 3880 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3881 | // restricted by the direction in the offer. |
| 3882 | bool recv_audio = true; |
| 3883 | bool recv_video = true; |
| 3884 | |
| 3885 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3886 | if (offer_answer_options.offer_to_receive_audio != |
| 3887 | RTCOfferAnswerOptions::kUndefined) { |
| 3888 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3889 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3890 | if (offer_answer_options.offer_to_receive_video != |
| 3891 | RTCOfferAnswerOptions::kUndefined) { |
| 3892 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | rtc::Optional<size_t> audio_index; |
| 3896 | rtc::Optional<size_t> video_index; |
| 3897 | rtc::Optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 3898 | |
| 3899 | // Generate m= sections that match those in the offer. |
| 3900 | // Note that mediasession.cc will handle intersection our preferred |
| 3901 | // direction with the offered direction. |
| 3902 | GenerateMediaDescriptionOptions( |
| 3903 | remote_description(), |
| 3904 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3905 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3906 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3907 | |
| 3908 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3909 | !audio_index ? nullptr |
| 3910 | : &session_options->media_description_options[*audio_index]; |
| 3911 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3912 | !video_index ? nullptr |
| 3913 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3914 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3915 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3916 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3917 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3918 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3919 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3920 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3921 | cricket::MediaSessionOptions* session_options) { |
| 3922 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3923 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3924 | RTC_DCHECK(remote_description()); |
| 3925 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3926 | for (const ContentInfo& content : |
| 3927 | remote_description()->description()->contents()) { |
| 3928 | cricket::MediaType media_type = content.media_description()->type(); |
| 3929 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3930 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3931 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3932 | RTC_CHECK(transceiver); |
| 3933 | session_options->media_description_options.push_back( |
| 3934 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3935 | } else { |
| 3936 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3937 | // Reject all data sections if data channels are disabled. |
| 3938 | // Reject a data section if it has already been rejected. |
| 3939 | // Reject all data sections except for the first one. |
| 3940 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3941 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3942 | session_options->media_description_options.push_back( |
| 3943 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3944 | } else { |
| 3945 | session_options->media_description_options.push_back( |
| 3946 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3947 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3948 | } |
| 3949 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3952 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3953 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3954 | RtpTransceiverDirection audio_direction, |
| 3955 | RtpTransceiverDirection video_direction, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3956 | rtc::Optional<size_t>* audio_index, |
| 3957 | rtc::Optional<size_t>* video_index, |
| 3958 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3959 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3960 | for (const cricket::ContentInfo& content : |
| 3961 | session_desc->description()->contents()) { |
| 3962 | if (IsAudioContent(&content)) { |
| 3963 | // If we already have an audio m= section, reject this extra one. |
| 3964 | if (*audio_index) { |
| 3965 | session_options->media_description_options.push_back( |
| 3966 | cricket::MediaDescriptionOptions( |
| 3967 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3968 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3969 | } else { |
| 3970 | session_options->media_description_options.push_back( |
| 3971 | cricket::MediaDescriptionOptions( |
| 3972 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3973 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3974 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3975 | } |
| 3976 | } else if (IsVideoContent(&content)) { |
| 3977 | // If we already have an video m= section, reject this extra one. |
| 3978 | if (*video_index) { |
| 3979 | session_options->media_description_options.push_back( |
| 3980 | cricket::MediaDescriptionOptions( |
| 3981 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3982 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3983 | } else { |
| 3984 | session_options->media_description_options.push_back( |
| 3985 | cricket::MediaDescriptionOptions( |
| 3986 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3987 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3988 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3989 | } |
| 3990 | } else { |
| 3991 | RTC_DCHECK(IsDataContent(&content)); |
| 3992 | // If we already have an data m= section, reject this extra one. |
| 3993 | if (*data_index) { |
| 3994 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3995 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3996 | } else { |
| 3997 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3998 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3999 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4000 | } |
| 4001 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4002 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4005 | cricket::MediaDescriptionOptions |
| 4006 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4007 | const std::string& mid) const { |
| 4008 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4009 | // expect sendrecv. |
| 4010 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4011 | RtpTransceiverDirection::kSendRecv, |
| 4012 | /*stopped=*/false); |
| 4013 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4014 | return options; |
| 4015 | } |
| 4016 | |
| 4017 | cricket::MediaDescriptionOptions |
| 4018 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4019 | const std::string& mid) const { |
| 4020 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4021 | RtpTransceiverDirection::kInactive, |
| 4022 | /*stopped=*/true); |
| 4023 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4024 | return options; |
| 4025 | } |
| 4026 | |
| 4027 | rtc::Optional<std::string> PeerConnection::GetDataMid() const { |
| 4028 | switch (data_channel_type_) { |
| 4029 | case cricket::DCT_RTP: |
| 4030 | if (!rtp_data_channel_) { |
| 4031 | return rtc::nullopt; |
| 4032 | } |
| 4033 | return rtp_data_channel_->content_name(); |
| 4034 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4035 | return sctp_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4036 | default: |
| 4037 | return rtc::nullopt; |
| 4038 | } |
| 4039 | } |
| 4040 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4041 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4042 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4043 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4044 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4045 | } |
| 4046 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4047 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4048 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4049 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4050 | cricket::MediaType media_type, |
| 4051 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4052 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4053 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4054 | std::vector<RtpSenderInfo>* current_senders = |
| 4055 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4056 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4057 | // 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] | 4058 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4059 | for (auto sender_it = current_senders->begin(); |
| 4060 | sender_it != current_senders->end(); |
| 4061 | /* incremented manually */) { |
| 4062 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4063 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4064 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4065 | std::string params_stream_id; |
| 4066 | if (params) { |
| 4067 | params_stream_id = |
| 4068 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4069 | : kDefaultStreamId); |
| 4070 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4071 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4072 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4073 | // If this is a default track, and we still need it, don't remove it. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4074 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4075 | sender_exists) { |
| 4076 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4077 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4078 | OnRemoteSenderRemoved(info, media_type); |
| 4079 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4083 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4084 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4085 | if (!params.has_ssrcs()) { |
| 4086 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4087 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4088 | // create a default. |
| 4089 | default_sender_needed = true; |
| 4090 | break; |
| 4091 | } |
| 4092 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4093 | // |params.id| is the sender id and the stream id uses the first of |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4094 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4095 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4096 | // not supported in Plan B, we just take the first here and create the |
| 4097 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4098 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4099 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4100 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4101 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4102 | uint32_t ssrc = params.first_ssrc(); |
| 4103 | |
| 4104 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4105 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4106 | if (!stream) { |
| 4107 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4108 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4109 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4110 | remote_streams_->AddStream(stream); |
| 4111 | new_streams->AddStream(stream); |
| 4112 | } |
| 4113 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4114 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4115 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4116 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4117 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4118 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4119 | } |
| 4120 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4121 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4122 | // Add default sender if necessary. |
| 4123 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4124 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4125 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4126 | if (!default_stream) { |
| 4127 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4128 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4129 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4130 | remote_streams_->AddStream(default_stream); |
| 4131 | new_streams->AddStream(default_stream); |
| 4132 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4133 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4134 | ? kDefaultAudioSenderId |
| 4135 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4136 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4137 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4138 | if (!default_sender_info) { |
| 4139 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4140 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4141 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4142 | } |
| 4143 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4144 | } |
| 4145 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4146 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4147 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4148 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4149 | << " receiver for track_id=" << sender_info.sender_id |
| 4150 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4151 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4152 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4153 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4154 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4155 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4156 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4157 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4158 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4159 | } |
| 4160 | } |
| 4161 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4162 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4163 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4164 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 4165 | << " receiver for track_id=" << sender_info.sender_id |
| 4166 | << " and stream_id=" << sender_info.stream_id; |
| 4167 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4168 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4169 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4170 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4171 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4172 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4173 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4174 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4175 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4176 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4177 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4178 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4179 | } |
| 4180 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4181 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4182 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4183 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4184 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4185 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4186 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4187 | // There's no guarantee the track is still available, e.g. the track may |
| 4188 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4189 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4190 | } |
| 4191 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4192 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4193 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4194 | if (receiver) { |
| 4195 | observer_->OnRemoveTrack(receiver); |
| 4196 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4200 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4201 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4202 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4203 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4204 | streams_to_remove.push_back(stream); |
| 4205 | } |
| 4206 | } |
| 4207 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4208 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4209 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4210 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4211 | } |
| 4212 | } |
| 4213 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4214 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4215 | const std::vector<cricket::StreamParams>& streams, |
| 4216 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4217 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4218 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4219 | // Find removed tracks. I.e., tracks where the track id, stream id or ssrc |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4220 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4221 | for (auto sender_it = current_senders->begin(); |
| 4222 | sender_it != current_senders->end(); |
| 4223 | /* incremented manually */) { |
| 4224 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4225 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4226 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4227 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4228 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4229 | OnLocalSenderRemoved(info, media_type); |
| 4230 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4231 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4232 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4233 | } |
| 4234 | } |
| 4235 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4236 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4237 | for (const cricket::StreamParams& params : streams) { |
| 4238 | // 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] | 4239 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4240 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4241 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4242 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4243 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4244 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4245 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4246 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4247 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | } |
| 4251 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4252 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4253 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4254 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4255 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4256 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4257 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4258 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4259 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4260 | return; |
| 4261 | } |
| 4262 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4263 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4264 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4265 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4266 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4267 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4268 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4269 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4270 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4273 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4274 | cricket::MediaType media_type) { |
| 4275 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4276 | if (!sender) { |
| 4277 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4278 | // SessionDescriptions has been renegotiated. |
| 4279 | return; |
| 4280 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4281 | |
| 4282 | // A sender has been removed from the SessionDescription but it's still |
| 4283 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4284 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4285 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4286 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4287 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4288 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4289 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4290 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4291 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4295 | const cricket::StreamParamsVec& streams) { |
| 4296 | std::vector<std::string> existing_channels; |
| 4297 | |
| 4298 | // Find new and active data channels. |
| 4299 | for (const cricket::StreamParams& params : streams) { |
| 4300 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4301 | // we use the same naming of data channels as we do for |
| 4302 | // MediaStreams and Tracks. |
| 4303 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4304 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4305 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4306 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4307 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4308 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4309 | continue; |
| 4310 | } |
| 4311 | // Set the SSRC the data channel should use for sending. |
| 4312 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4313 | existing_channels.push_back(data_channel_it->first); |
| 4314 | } |
| 4315 | |
| 4316 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4317 | } |
| 4318 | |
| 4319 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4320 | const cricket::StreamParamsVec& streams) { |
| 4321 | std::vector<std::string> existing_channels; |
| 4322 | |
| 4323 | // Find new and active data channels. |
| 4324 | for (const cricket::StreamParams& params : streams) { |
| 4325 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4326 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4327 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4328 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4329 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4330 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4331 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4332 | // This is a new data channel. |
| 4333 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4334 | } else { |
| 4335 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4336 | } |
| 4337 | existing_channels.push_back(label); |
| 4338 | } |
| 4339 | |
| 4340 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4341 | } |
| 4342 | |
| 4343 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4344 | const std::vector<std::string>& active_channels, |
| 4345 | bool is_local_update) { |
| 4346 | auto it = rtp_data_channels_.begin(); |
| 4347 | while (it != rtp_data_channels_.end()) { |
| 4348 | DataChannel* data_channel = it->second; |
| 4349 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4350 | data_channel->label()) != active_channels.end()) { |
| 4351 | ++it; |
| 4352 | continue; |
| 4353 | } |
| 4354 | |
| 4355 | if (is_local_update) { |
| 4356 | data_channel->SetSendSsrc(0); |
| 4357 | } else { |
| 4358 | data_channel->RemotePeerRequestClose(); |
| 4359 | } |
| 4360 | |
| 4361 | if (data_channel->state() == DataChannel::kClosed) { |
| 4362 | rtp_data_channels_.erase(it); |
| 4363 | it = rtp_data_channels_.begin(); |
| 4364 | } else { |
| 4365 | ++it; |
| 4366 | } |
| 4367 | } |
| 4368 | } |
| 4369 | |
| 4370 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4371 | uint32_t remote_ssrc) { |
| 4372 | rtc::scoped_refptr<DataChannel> channel( |
| 4373 | InternalCreateDataChannel(label, nullptr)); |
| 4374 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4375 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4376 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4377 | return; |
| 4378 | } |
| 4379 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4380 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4381 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4382 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4386 | const std::string& label, |
| 4387 | const InternalDataChannelInit* config) { |
| 4388 | if (IsClosed()) { |
| 4389 | return nullptr; |
| 4390 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4391 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4392 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4393 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4394 | return nullptr; |
| 4395 | } |
| 4396 | InternalDataChannelInit new_config = |
| 4397 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4398 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4399 | if (new_config.id < 0) { |
| 4400 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4401 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4402 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4403 | RTC_LOG(LS_ERROR) |
| 4404 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4405 | return nullptr; |
| 4406 | } |
| 4407 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4408 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4409 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4410 | return nullptr; |
| 4411 | } |
| 4412 | } |
| 4413 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4414 | rtc::scoped_refptr<DataChannel> channel( |
| 4415 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4416 | if (!channel) { |
| 4417 | sid_allocator_.ReleaseSid(new_config.id); |
| 4418 | return nullptr; |
| 4419 | } |
| 4420 | |
| 4421 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4422 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4423 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4424 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4425 | return nullptr; |
| 4426 | } |
| 4427 | rtp_data_channels_[channel->label()] = channel; |
| 4428 | } else { |
| 4429 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4430 | sctp_data_channels_.push_back(channel); |
| 4431 | channel->SignalClosed.connect(this, |
| 4432 | &PeerConnection::OnSctpDataChannelClosed); |
| 4433 | } |
| 4434 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4435 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4436 | return channel; |
| 4437 | } |
| 4438 | |
| 4439 | bool PeerConnection::HasDataChannels() const { |
| 4440 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4441 | } |
| 4442 | |
| 4443 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4444 | for (const auto& channel : sctp_data_channels_) { |
| 4445 | if (channel->id() < 0) { |
| 4446 | int sid; |
| 4447 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4448 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4449 | continue; |
| 4450 | } |
| 4451 | channel->SetSctpSid(sid); |
| 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4457 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4458 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4459 | ++it) { |
| 4460 | if (it->get() == channel) { |
| 4461 | if (channel->id() >= 0) { |
| 4462 | sid_allocator_.ReleaseSid(channel->id()); |
| 4463 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4464 | // Since this method is triggered by a signal from the DataChannel, |
| 4465 | // we can't free it directly here; we need to free it asynchronously. |
| 4466 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4467 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 4468 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4469 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4470 | return; |
| 4471 | } |
| 4472 | } |
| 4473 | } |
| 4474 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4475 | void PeerConnection::OnDataChannelDestroyed() { |
| 4476 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4477 | // DataChannel may callback to us and try to modify the list. |
| 4478 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4479 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4480 | for (const auto& kv : temp_rtp_dcs) { |
| 4481 | kv.second->OnTransportChannelDestroyed(); |
| 4482 | } |
| 4483 | |
| 4484 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4485 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4486 | for (const auto& channel : temp_sctp_dcs) { |
| 4487 | channel->OnTransportChannelDestroyed(); |
| 4488 | } |
| 4489 | } |
| 4490 | |
| 4491 | void PeerConnection::OnDataChannelOpenMessage( |
| 4492 | const std::string& label, |
| 4493 | const InternalDataChannelInit& config) { |
| 4494 | rtc::scoped_refptr<DataChannel> channel( |
| 4495 | InternalCreateDataChannel(label, &config)); |
| 4496 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4497 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4498 | return; |
| 4499 | } |
| 4500 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4501 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4502 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4503 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4504 | } |
| 4505 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4506 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4507 | PeerConnection::GetAudioTransceiver() const { |
| 4508 | // This method only works with Plan B SDP, where there is a single |
| 4509 | // audio/video transceiver. |
| 4510 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4511 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4512 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4513 | return transceiver; |
| 4514 | } |
| 4515 | } |
| 4516 | RTC_NOTREACHED(); |
| 4517 | return nullptr; |
| 4518 | } |
| 4519 | |
| 4520 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4521 | PeerConnection::GetVideoTransceiver() const { |
| 4522 | // This method only works with Plan B SDP, where there is a single |
| 4523 | // audio/video transceiver. |
| 4524 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4525 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4526 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4527 | return transceiver; |
| 4528 | } |
| 4529 | } |
| 4530 | RTC_NOTREACHED(); |
| 4531 | return nullptr; |
| 4532 | } |
| 4533 | |
| 4534 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4535 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4536 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4537 | switch (type) { |
| 4538 | case cricket::MEDIA_TYPE_AUDIO: |
| 4539 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4540 | case cricket::MEDIA_TYPE_VIDEO: |
| 4541 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4542 | case cricket::MEDIA_TYPE_DATA: |
| 4543 | return false; |
| 4544 | } |
| 4545 | RTC_NOTREACHED(); |
| 4546 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4547 | } |
| 4548 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4549 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4550 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4551 | for (auto transceiver : transceivers_) { |
| 4552 | for (auto sender : transceiver->internal()->senders()) { |
| 4553 | if (sender->track() == track) { |
| 4554 | return sender; |
| 4555 | } |
| 4556 | } |
| 4557 | } |
| 4558 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4559 | } |
| 4560 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4561 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4562 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4563 | for (auto transceiver : transceivers_) { |
| 4564 | for (auto sender : transceiver->internal()->senders()) { |
| 4565 | if (sender->id() == sender_id) { |
| 4566 | return sender; |
| 4567 | } |
| 4568 | } |
| 4569 | } |
| 4570 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4571 | } |
| 4572 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4573 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4574 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4575 | for (auto transceiver : transceivers_) { |
| 4576 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4577 | if (receiver->id() == receiver_id) { |
| 4578 | return receiver; |
| 4579 | } |
| 4580 | } |
| 4581 | } |
| 4582 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4583 | } |
| 4584 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4585 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4586 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4587 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4588 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4589 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4590 | ? &remote_audio_sender_infos_ |
| 4591 | : &remote_video_sender_infos_; |
| 4592 | } |
| 4593 | |
| 4594 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4595 | cricket::MediaType media_type) { |
| 4596 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4597 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4598 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4599 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4600 | } |
| 4601 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4602 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4603 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4604 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4605 | const std::string sender_id) const { |
| 4606 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4607 | if (sender_info.stream_id == stream_id && |
| 4608 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4609 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4610 | } |
| 4611 | } |
| 4612 | return nullptr; |
| 4613 | } |
| 4614 | |
| 4615 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4616 | for (const auto& channel : sctp_data_channels_) { |
| 4617 | if (channel->id() == sid) { |
| 4618 | return channel; |
| 4619 | } |
| 4620 | } |
| 4621 | return nullptr; |
| 4622 | } |
| 4623 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4624 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4625 | const RTCConfiguration& configuration) { |
| 4626 | cricket::ServerAddresses stun_servers; |
| 4627 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4628 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 4629 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4630 | return false; |
| 4631 | } |
| 4632 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4633 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4634 | // To handle both internal and externally created port allocator, we will |
| 4635 | // enable BUNDLE here. |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4636 | port_allocator_flags_ = port_allocator_->flags(); |
| 4637 | port_allocator_flags_ |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 4638 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4639 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4640 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4641 | // by experiment. |
| 4642 | if (configuration.disable_ipv6) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4643 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4644 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4645 | .find("Disabled") == 0) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4646 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4647 | } |
| 4648 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4649 | if (configuration.disable_ipv6_on_wifi) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4650 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4651 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4652 | } |
| 4653 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4654 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4655 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4656 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4659 | if (configuration.candidate_network_policy == |
| 4660 | kCandidateNetworkPolicyLowCost) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4661 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4662 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4663 | } |
| 4664 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4665 | if (configuration.disable_link_local_networks) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4666 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4667 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4668 | } |
| 4669 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4670 | port_allocator_->set_flags(port_allocator_flags_); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4671 | // No step delay is used while allocating ports. |
| 4672 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4673 | port_allocator_->set_candidate_filter( |
| 4674 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4675 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4676 | |
| 4677 | // Call this last since it may create pooled allocator sessions using the |
| 4678 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4679 | port_allocator_->SetConfiguration( |
| 4680 | stun_servers, turn_servers, configuration.ice_candidate_pool_size, |
| 4681 | configuration.prune_turn_ports, configuration.turn_customizer, |
| 4682 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4683 | return true; |
| 4684 | } |
| 4685 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4686 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4687 | const cricket::ServerAddresses& stun_servers, |
| 4688 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4689 | IceTransportsType type, |
| 4690 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4691 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4692 | webrtc::TurnCustomizer* turn_customizer, |
| 4693 | rtc::Optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4694 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4695 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4696 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 4697 | // size is not allowed, and changing the set of ICE servers will not result |
| 4698 | // in new candidates being gathered. |
| 4699 | if (local_description()) { |
| 4700 | port_allocator_->FreezeCandidatePool(); |
| 4701 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4702 | // Call this last since it may create pooled allocator sessions using the |
| 4703 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4704 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4705 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4706 | turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4709 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4710 | return factory_->channel_manager(); |
| 4711 | } |
| 4712 | |
| 4713 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 4714 | return uma_observer_; |
| 4715 | } |
| 4716 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4717 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4718 | std::unique_ptr<RtcEventLogOutput> output, |
| 4719 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4720 | if (!event_log_) { |
| 4721 | return false; |
| 4722 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4723 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4727 | if (event_log_) { |
| 4728 | event_log_->StopLogging(); |
| 4729 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4730 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4731 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4732 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4733 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4734 | for (auto transceiver : transceivers_) { |
| 4735 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4736 | if (channel && channel->content_name() == content_name) { |
| 4737 | return channel; |
| 4738 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4739 | } |
| 4740 | if (rtp_data_channel() && |
| 4741 | rtp_data_channel()->content_name() == content_name) { |
| 4742 | return rtp_data_channel(); |
| 4743 | } |
| 4744 | return nullptr; |
| 4745 | } |
| 4746 | |
| 4747 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4748 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4749 | RTC_LOG(LS_INFO) |
| 4750 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4751 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4752 | return false; |
| 4753 | } |
| 4754 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4755 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4756 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4757 | return false; |
| 4758 | } |
| 4759 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4760 | auto dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 4761 | if (dtls_role) { |
| 4762 | *role = *dtls_role; |
| 4763 | return true; |
| 4764 | } |
| 4765 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4769 | rtc::SSLRole* role) { |
| 4770 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4771 | RTC_LOG(LS_INFO) |
| 4772 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4773 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4774 | return false; |
| 4775 | } |
| 4776 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4777 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 4778 | if (dtls_role) { |
| 4779 | *role = *dtls_role; |
| 4780 | return true; |
| 4781 | } |
| 4782 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4783 | } |
| 4784 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4785 | void PeerConnection::SetSessionError(SessionError error, |
| 4786 | const std::string& error_desc) { |
| 4787 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4788 | if (error != session_error_) { |
| 4789 | session_error_ = error; |
| 4790 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4791 | } |
| 4792 | } |
| 4793 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4794 | RTCError PeerConnection::UpdateSessionState( |
| 4795 | SdpType type, |
| 4796 | cricket::ContentSource source, |
| 4797 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4798 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4799 | |
| 4800 | // If there's already a pending error then no state transition should happen. |
| 4801 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4802 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4803 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4804 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4805 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4806 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | // Update the signaling state according to the specified state machine (see |
| 4810 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4811 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4812 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4813 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4814 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4815 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4816 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4817 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4818 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4819 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4820 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4821 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4822 | } |
| 4823 | |
| 4824 | // Update internal objects according to the session description's media |
| 4825 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4826 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4827 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 4828 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4829 | } |
| 4830 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4831 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4832 | } |
| 4833 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4834 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4835 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4836 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4837 | const SessionDescriptionInterface* sdesc = |
| 4838 | (source == cricket::CS_LOCAL ? local_description() |
| 4839 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4840 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4841 | |
| 4842 | // Push down the new SDP media section for each audio/video transceiver. |
| 4843 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4844 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4845 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4846 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4847 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4848 | continue; |
| 4849 | } |
| 4850 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4851 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4852 | if (!content_desc) { |
| 4853 | continue; |
| 4854 | } |
| 4855 | std::string error; |
| 4856 | bool success = |
| 4857 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4858 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4859 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4860 | if (!success) { |
| 4861 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4866 | if (rtp_data_channel_) { |
| 4867 | const ContentInfo* data_content = |
| 4868 | cricket::GetFirstDataContent(sdesc->description()); |
| 4869 | if (data_content && !data_content->rejected) { |
| 4870 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4871 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4872 | if (data_desc) { |
| 4873 | std::string error; |
| 4874 | bool success = |
| 4875 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4876 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
| 4877 | : rtp_data_channel_->SetRemoteContent(data_desc, type, |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4878 | &error); |
| 4879 | if (!success) { |
| 4880 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4881 | std::move(error)); |
| 4882 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4883 | } |
| 4884 | } |
| 4885 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4886 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4887 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4888 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4889 | if (sctp_transport_ && local_description() && remote_description() && |
| 4890 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4891 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4892 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4893 | RTC_FROM_HERE, |
| 4894 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4895 | if (!success) { |
| 4896 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4897 | "Failed to push down SCTP parameters."); |
| 4898 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4899 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4900 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4901 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4905 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4906 | RTC_DCHECK(local_description()); |
| 4907 | RTC_DCHECK(remote_description()); |
| 4908 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4909 | // When we support "max-message-size", that would also be pushed down here. |
| 4910 | return sctp_transport_->Start( |
| 4911 | GetSctpPort(local_description()->description()), |
| 4912 | GetSctpPort(remote_description()->description())); |
| 4913 | } |
| 4914 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4915 | RTCError PeerConnection::PushdownTransportDescription( |
| 4916 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4917 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4918 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4919 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4920 | if (source == cricket::CS_LOCAL) { |
| 4921 | const SessionDescriptionInterface* sdesc = local_description(); |
| 4922 | RTC_DCHECK(sdesc); |
| 4923 | return transport_controller_->SetLocalDescription(type, |
| 4924 | sdesc->description()); |
| 4925 | } else { |
| 4926 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 4927 | RTC_DCHECK(sdesc); |
| 4928 | return transport_controller_->SetRemoteDescription(type, |
| 4929 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4930 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | bool PeerConnection::GetTransportDescription( |
| 4934 | const SessionDescription* description, |
| 4935 | const std::string& content_name, |
| 4936 | cricket::TransportDescription* tdesc) { |
| 4937 | if (!description || !tdesc) { |
| 4938 | return false; |
| 4939 | } |
| 4940 | const TransportInfo* transport_info = |
| 4941 | description->GetTransportInfoByName(content_name); |
| 4942 | if (!transport_info) { |
| 4943 | return false; |
| 4944 | } |
| 4945 | *tdesc = transport_info->description; |
| 4946 | return true; |
| 4947 | } |
| 4948 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4949 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4950 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4951 | cricket::ContinualGatheringPolicy gathering_policy; |
| 4952 | // TODO(honghaiz): Add the third continual gathering policy in |
| 4953 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 4954 | switch (config.continual_gathering_policy) { |
| 4955 | case PeerConnectionInterface::GATHER_ONCE: |
| 4956 | gathering_policy = cricket::GATHER_ONCE; |
| 4957 | break; |
| 4958 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4959 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4960 | break; |
| 4961 | default: |
| 4962 | RTC_NOTREACHED(); |
| 4963 | gathering_policy = cricket::GATHER_ONCE; |
| 4964 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4965 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4966 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4967 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 4968 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4969 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4970 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4971 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4972 | RTCConfigurationToIceConfigOptionalInt( |
| 4973 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4974 | ice_config.continual_gathering_policy = gathering_policy; |
| 4975 | ice_config.presume_writable_when_fully_relayed = |
| 4976 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 4977 | ice_config.ice_check_interval_strong_connectivity = |
| 4978 | config.ice_check_interval_strong_connectivity; |
| 4979 | ice_config.ice_check_interval_weak_connectivity = |
| 4980 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4981 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4982 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4983 | ice_config.regather_all_networks_interval_range = |
| 4984 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4985 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4986 | return ice_config; |
| 4987 | } |
| 4988 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4989 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4990 | std::string* track_id) { |
| 4991 | if (!local_description()) { |
| 4992 | return false; |
| 4993 | } |
| 4994 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4995 | track_id); |
| 4996 | } |
| 4997 | |
| 4998 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4999 | std::string* track_id) { |
| 5000 | if (!remote_description()) { |
| 5001 | return false; |
| 5002 | } |
| 5003 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 5004 | track_id); |
| 5005 | } |
| 5006 | |
| 5007 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5008 | const rtc::CopyOnWriteBuffer& payload, |
| 5009 | cricket::SendDataResult* result) { |
| 5010 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5011 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5012 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5013 | return false; |
| 5014 | } |
| 5015 | return rtp_data_channel_ |
| 5016 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5017 | : network_thread()->Invoke<bool>( |
| 5018 | RTC_FROM_HERE, |
| 5019 | Bind(&cricket::SctpTransportInternal::SendData, |
| 5020 | sctp_transport_.get(), params, payload, result)); |
| 5021 | } |
| 5022 | |
| 5023 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5024 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 5025 | // Don't log an error here, because DataChannels are expected to call |
| 5026 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5027 | // whether or not the underlying transport is ready. |
| 5028 | return false; |
| 5029 | } |
| 5030 | if (rtp_data_channel_) { |
| 5031 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5032 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5033 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5034 | &DataChannel::OnDataReceived); |
| 5035 | } else { |
| 5036 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5037 | &DataChannel::OnChannelReady); |
| 5038 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5039 | &DataChannel::OnDataReceived); |
| 5040 | SignalSctpStreamClosedRemotely.connect( |
| 5041 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 5042 | } |
| 5043 | return true; |
| 5044 | } |
| 5045 | |
| 5046 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5047 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5048 | RTC_LOG(LS_ERROR) |
| 5049 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5050 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5051 | return; |
| 5052 | } |
| 5053 | if (rtp_data_channel_) { |
| 5054 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5055 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5056 | } else { |
| 5057 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5058 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 5059 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | void PeerConnection::AddSctpDataStream(int sid) { |
| 5064 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5065 | RTC_LOG(LS_ERROR) |
| 5066 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5067 | return; |
| 5068 | } |
| 5069 | network_thread()->Invoke<void>( |
| 5070 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 5071 | sctp_transport_.get(), sid)); |
| 5072 | } |
| 5073 | |
| 5074 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 5075 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5076 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5077 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5078 | return; |
| 5079 | } |
| 5080 | network_thread()->Invoke<void>( |
| 5081 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 5082 | sctp_transport_.get(), sid)); |
| 5083 | } |
| 5084 | |
| 5085 | bool PeerConnection::ReadyToSendData() const { |
| 5086 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 5087 | sctp_ready_to_send_data_; |
| 5088 | } |
| 5089 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5090 | rtc::Optional<std::string> PeerConnection::sctp_transport_name() const { |
| 5091 | if (sctp_mid_ && transport_controller_) { |
| 5092 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5093 | if (dtls_transport) { |
| 5094 | return dtls_transport->transport_name(); |
| 5095 | } |
| 5096 | return rtc::Optional<std::string>(); |
| 5097 | } |
| 5098 | return rtc::Optional<std::string>(); |
| 5099 | } |
| 5100 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5101 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5102 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5103 | network_thread()->Invoke<void>( |
| 5104 | RTC_FROM_HERE, |
| 5105 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 5106 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5107 | return candidate_states_list; |
| 5108 | } |
| 5109 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5110 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5111 | const { |
| 5112 | std::map<std::string, std::string> transport_names_by_mid; |
| 5113 | for (auto transceiver : transceivers_) { |
| 5114 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5115 | if (channel) { |
| 5116 | transport_names_by_mid[channel->content_name()] = |
| 5117 | channel->transport_name(); |
| 5118 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5119 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5120 | if (rtp_data_channel_) { |
| 5121 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5122 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5123 | } |
| 5124 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5125 | rtc::Optional<std::string> transport_name = sctp_transport_name(); |
| 5126 | RTC_DCHECK(transport_name); |
| 5127 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5128 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5129 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5130 | } |
| 5131 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5132 | std::map<std::string, cricket::TransportStats> |
| 5133 | PeerConnection::GetTransportStatsByNames( |
| 5134 | const std::set<std::string>& transport_names) { |
| 5135 | if (!network_thread()->IsCurrent()) { |
| 5136 | return network_thread() |
| 5137 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5138 | RTC_FROM_HERE, |
| 5139 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5140 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5141 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5142 | for (const std::string& transport_name : transport_names) { |
| 5143 | cricket::TransportStats transport_stats; |
| 5144 | bool success = |
| 5145 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5146 | if (success) { |
| 5147 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5148 | } else { |
| 5149 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5150 | << transport_name; |
| 5151 | } |
| 5152 | } |
| 5153 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | bool PeerConnection::GetLocalCertificate( |
| 5157 | const std::string& transport_name, |
| 5158 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5159 | if (!certificate) { |
| 5160 | return false; |
| 5161 | } |
| 5162 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 5163 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5164 | } |
| 5165 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5166 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5167 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5168 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5172 | return data_channel_type_; |
| 5173 | } |
| 5174 | |
| 5175 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5176 | return pending_ice_restarts_.find(content_name) != |
| 5177 | pending_ice_restarts_.end(); |
| 5178 | } |
| 5179 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5180 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5181 | return transport_controller_->NeedsIceRestart(content_name); |
| 5182 | } |
| 5183 | |
| 5184 | void PeerConnection::OnCertificateReady( |
| 5185 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5186 | transport_controller_->SetLocalCertificate(certificate); |
| 5187 | } |
| 5188 | |
| 5189 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5190 | SetSessionError(SessionError::kTransport, |
| 5191 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | void PeerConnection::OnTransportControllerConnectionState( |
| 5195 | cricket::IceConnectionState state) { |
| 5196 | switch (state) { |
| 5197 | case cricket::kIceConnectionConnecting: |
| 5198 | // If the current state is Connected or Completed, then there were |
| 5199 | // writable channels but now there are not, so the next state must |
| 5200 | // be Disconnected. |
| 5201 | // kIceConnectionConnecting is currently used as the default, |
| 5202 | // un-connected state by the TransportController, so its only use is |
| 5203 | // detecting disconnections. |
| 5204 | if (ice_connection_state_ == |
| 5205 | PeerConnectionInterface::kIceConnectionConnected || |
| 5206 | ice_connection_state_ == |
| 5207 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5208 | SetIceConnectionState( |
| 5209 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5210 | } |
| 5211 | break; |
| 5212 | case cricket::kIceConnectionFailed: |
| 5213 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5214 | break; |
| 5215 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5216 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5217 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5218 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5219 | break; |
| 5220 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5221 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5222 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5223 | if (ice_connection_state_ != |
| 5224 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5225 | // If jumping directly from "checking" to "connected", |
| 5226 | // signal "connected" first. |
| 5227 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5228 | } |
| 5229 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5230 | if (metrics_observer()) { |
| 5231 | ReportTransportStats(); |
| 5232 | } |
| 5233 | break; |
| 5234 | default: |
| 5235 | RTC_NOTREACHED(); |
| 5236 | } |
| 5237 | } |
| 5238 | |
| 5239 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5240 | const std::string& transport_name, |
| 5241 | const cricket::Candidates& candidates) { |
| 5242 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5243 | int sdp_mline_index; |
| 5244 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5245 | RTC_LOG(LS_ERROR) |
| 5246 | << "OnTransportControllerCandidatesGathered: content name " |
| 5247 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5248 | return; |
| 5249 | } |
| 5250 | |
| 5251 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5252 | citer != candidates.end(); ++citer) { |
| 5253 | // Use transport_name as the candidate media id. |
| 5254 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5255 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5256 | if (local_description()) { |
| 5257 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5258 | } |
| 5259 | OnIceCandidate(std::move(candidate)); |
| 5260 | } |
| 5261 | } |
| 5262 | |
| 5263 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5264 | const std::vector<cricket::Candidate>& candidates) { |
| 5265 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5266 | // Sanity check. |
| 5267 | for (const cricket::Candidate& candidate : candidates) { |
| 5268 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5269 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5270 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5271 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5272 | return; |
| 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | if (local_description()) { |
| 5277 | mutable_local_description()->RemoveCandidates(candidates); |
| 5278 | } |
| 5279 | OnIceCandidatesRemoved(candidates); |
| 5280 | } |
| 5281 | |
| 5282 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5283 | rtc::SSLHandshakeError error) { |
| 5284 | if (metrics_observer()) { |
| 5285 | metrics_observer()->IncrementEnumCounter( |
| 5286 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 5287 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 5288 | } |
| 5289 | } |
| 5290 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5291 | void PeerConnection::EnableSending() { |
| 5292 | for (auto transceiver : transceivers_) { |
| 5293 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5294 | if (channel && !channel->enabled()) { |
| 5295 | channel->Enable(true); |
| 5296 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5297 | } |
| 5298 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5299 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5300 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5301 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | // Returns the media index for a local ice candidate given the content name. |
| 5305 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5306 | const std::string& content_name, |
| 5307 | int* sdp_mline_index) { |
| 5308 | if (!local_description() || !sdp_mline_index) { |
| 5309 | return false; |
| 5310 | } |
| 5311 | |
| 5312 | bool content_found = false; |
| 5313 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5314 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5315 | if (contents[index].name == content_name) { |
| 5316 | *sdp_mline_index = static_cast<int>(index); |
| 5317 | content_found = true; |
| 5318 | break; |
| 5319 | } |
| 5320 | } |
| 5321 | return content_found; |
| 5322 | } |
| 5323 | |
| 5324 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5325 | const SessionDescriptionInterface* remote_desc) { |
| 5326 | if (!remote_desc) { |
| 5327 | return true; |
| 5328 | } |
| 5329 | bool ret = true; |
| 5330 | |
| 5331 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5332 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5333 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5334 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5335 | bool valid = false; |
| 5336 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5337 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5338 | RTC_LOG(LS_INFO) |
| 5339 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5340 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5341 | } |
| 5342 | continue; |
| 5343 | } |
| 5344 | ret = UseCandidate(candidate); |
| 5345 | if (!ret) { |
| 5346 | break; |
| 5347 | } |
| 5348 | } |
| 5349 | } |
| 5350 | return ret; |
| 5351 | } |
| 5352 | |
| 5353 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5354 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5355 | size_t remote_content_size = |
| 5356 | remote_description()->description()->contents().size(); |
| 5357 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5358 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5359 | return false; |
| 5360 | } |
| 5361 | |
| 5362 | cricket::ContentInfo content = |
| 5363 | remote_description()->description()->contents()[mediacontent_index]; |
| 5364 | std::vector<cricket::Candidate> candidates; |
| 5365 | candidates.push_back(candidate->candidate()); |
| 5366 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5367 | RTCError error = |
| 5368 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 5369 | if (error.ok()) { |
| 5370 | // Candidates successfully submitted for checking. |
| 5371 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5372 | ice_connection_state_ == |
| 5373 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5374 | // If state is New, then the session has just gotten its first remote ICE |
| 5375 | // candidates, so go to Checking. |
| 5376 | // If state is Disconnected, the session is re-using old candidates or |
| 5377 | // receiving additional ones, so go to Checking. |
| 5378 | // If state is Connected, stay Connected. |
| 5379 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5380 | // newly added transport, then the state actually _should_ move to |
| 5381 | // checking. Add a way to distinguish that case. |
| 5382 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5383 | } |
| 5384 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 5385 | } else if (error.message()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5386 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5387 | } |
| 5388 | return true; |
| 5389 | } |
| 5390 | |
| 5391 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5392 | // Destroy video channel first since it may have a pointer to the |
| 5393 | // voice channel. |
| 5394 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5395 | if (!video_info || video_info->rejected) { |
| 5396 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5397 | } |
| 5398 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5399 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5400 | if (!audio_info || audio_info->rejected) { |
| 5401 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5405 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5406 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5407 | } |
| 5408 | } |
| 5409 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5410 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5411 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5412 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5413 | if (configuration_.bundle_policy == |
| 5414 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5415 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5416 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5417 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5418 | "max-bundle configured but session description " |
| 5419 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5420 | } |
| 5421 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5422 | return std::move(bundle_group); |
| 5423 | } |
| 5424 | |
| 5425 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5426 | // Creating the media channels. Transports should already have been created |
| 5427 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5428 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5429 | if (voice && !voice->rejected && |
| 5430 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5431 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5432 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5433 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5434 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5435 | } |
| 5436 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5437 | } |
| 5438 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5439 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5440 | if (video && !video->rejected && |
| 5441 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5442 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5443 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5444 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5445 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5446 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5447 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5450 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5451 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5452 | !rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5453 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5454 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5455 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5456 | } |
| 5457 | } |
| 5458 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5459 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5460 | } |
| 5461 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5462 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5463 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5464 | const std::string& mid) { |
| 5465 | RtpTransportInternal* rtp_transport = |
| 5466 | transport_controller_->GetRtpTransport(mid); |
| 5467 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5468 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5469 | call_.get(), configuration_.media_config, rtp_transport, |
| 5470 | signaling_thread(), mid, SrtpRequired(), |
| 5471 | factory_->options().crypto_options, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5472 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5473 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5474 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5475 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5476 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5477 | voice_channel->SignalSentPacket.connect(this, |
| 5478 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5479 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5480 | if (uma_observer_) { |
| 5481 | voice_channel->SetMetricsObserver(uma_observer_); |
| 5482 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5483 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5484 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5485 | } |
| 5486 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5487 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5488 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5489 | const std::string& mid) { |
| 5490 | RtpTransportInternal* rtp_transport = |
| 5491 | transport_controller_->GetRtpTransport(mid); |
| 5492 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5493 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5494 | call_.get(), configuration_.media_config, rtp_transport, |
| 5495 | signaling_thread(), mid, SrtpRequired(), |
| 5496 | factory_->options().crypto_options, video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5497 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5498 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5499 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5500 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5501 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5502 | video_channel->SignalSentPacket.connect(this, |
| 5503 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5504 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5505 | if (uma_observer_) { |
| 5506 | video_channel->SetMetricsObserver(uma_observer_); |
| 5507 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5508 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5509 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5510 | } |
| 5511 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5512 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5513 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5514 | if (sctp) { |
| 5515 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5516 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5517 | << "Trying to create SCTP transport, but didn't compile with " |
| 5518 | "SCTP support (HAVE_SCTP)"; |
| 5519 | return false; |
| 5520 | } |
| 5521 | if (!network_thread()->Invoke<bool>( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5522 | RTC_FROM_HERE, |
| 5523 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5524 | return false; |
| 5525 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5526 | for (const auto& channel : sctp_data_channels_) { |
| 5527 | channel->OnTransportChannelCreated(); |
| 5528 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5529 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5530 | RtpTransportInternal* rtp_transport = |
| 5531 | transport_controller_->GetRtpTransport(mid); |
| 5532 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5533 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5534 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
| 5535 | SrtpRequired(), factory_->options().crypto_options); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5536 | if (!rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5537 | return false; |
| 5538 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5539 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5540 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5541 | rtp_data_channel_->SignalSentPacket.connect( |
| 5542 | this, &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5543 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5544 | if (uma_observer_) { |
| 5545 | rtp_data_channel_->SetMetricsObserver(uma_observer_); |
| 5546 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5547 | } |
| 5548 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5549 | return true; |
| 5550 | } |
| 5551 | |
| 5552 | Call::Stats PeerConnection::GetCallStats() { |
| 5553 | if (!worker_thread()->IsCurrent()) { |
| 5554 | return worker_thread()->Invoke<Call::Stats>( |
| 5555 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5556 | } |
| 5557 | if (call_) { |
| 5558 | return call_->GetStats(); |
| 5559 | } else { |
| 5560 | return Call::Stats(); |
| 5561 | } |
| 5562 | } |
| 5563 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5564 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5565 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5566 | RTC_DCHECK(sctp_factory_); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5567 | cricket::DtlsTransportInternal* dtls_transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5568 | transport_controller_->GetDtlsTransport(mid); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5569 | RTC_DCHECK(dtls_transport); |
| 5570 | sctp_transport_ = sctp_factory_->CreateSctpTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5571 | RTC_DCHECK(sctp_transport_); |
| 5572 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5573 | sctp_transport_->SignalReadyToSendData.connect( |
| 5574 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5575 | sctp_transport_->SignalDataReceived.connect( |
| 5576 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 5577 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 5578 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5579 | sctp_mid_ = mid; |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5580 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5581 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | void PeerConnection::DestroySctpTransport_n() { |
| 5585 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5586 | sctp_transport_.reset(nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5587 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5588 | sctp_invoker_.reset(nullptr); |
| 5589 | sctp_ready_to_send_data_ = false; |
| 5590 | } |
| 5591 | |
| 5592 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5593 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5594 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5595 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5596 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5597 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5598 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5599 | // pending tasks will be cleared. |
| 5600 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5601 | OnSctpTransportReadyToSendData_s(true); |
| 5602 | }); |
| 5603 | } |
| 5604 | |
| 5605 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5606 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5607 | sctp_ready_to_send_data_ = ready; |
| 5608 | SignalSctpReadyToSendData(ready); |
| 5609 | } |
| 5610 | |
| 5611 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5612 | const cricket::ReceiveDataParams& params, |
| 5613 | const rtc::CopyOnWriteBuffer& payload) { |
| 5614 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5615 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5616 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5617 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5618 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5619 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5620 | // pending tasks will be cleared. |
| 5621 | sctp_invoker_->AsyncInvoke<void>( |
| 5622 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5623 | OnSctpTransportDataReceived_s(params, payload); |
| 5624 | }); |
| 5625 | } |
| 5626 | |
| 5627 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5628 | const cricket::ReceiveDataParams& params, |
| 5629 | const rtc::CopyOnWriteBuffer& payload) { |
| 5630 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5631 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5632 | // Received OPEN message; parse and signal that a new data channel should |
| 5633 | // be created. |
| 5634 | std::string label; |
| 5635 | InternalDataChannelInit config; |
| 5636 | config.id = params.ssrc; |
| 5637 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5638 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5639 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5640 | return; |
| 5641 | } |
| 5642 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5643 | OnDataChannelOpenMessage(label, config); |
| 5644 | } else { |
| 5645 | // Otherwise just forward the signal. |
| 5646 | SignalSctpDataReceived(params, payload); |
| 5647 | } |
| 5648 | } |
| 5649 | |
| 5650 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 5651 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5652 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5653 | sctp_invoker_->AsyncInvoke<void>( |
| 5654 | RTC_FROM_HERE, signaling_thread(), |
| 5655 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5656 | &SignalSctpStreamClosedRemotely, sid)); |
| 5657 | } |
| 5658 | |
| 5659 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5660 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5661 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5662 | if (!bundle_enabled) |
| 5663 | return true; |
| 5664 | |
| 5665 | const cricket::ContentGroup* bundle_group = |
| 5666 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5667 | RTC_DCHECK(bundle_group != NULL); |
| 5668 | |
| 5669 | const cricket::ContentInfos& contents = desc->contents(); |
| 5670 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5671 | citer != contents.end(); ++citer) { |
| 5672 | const cricket::ContentInfo* content = (&*citer); |
| 5673 | RTC_DCHECK(content != NULL); |
| 5674 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5675 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5676 | if (!HasRtcpMuxEnabled(content)) |
| 5677 | return false; |
| 5678 | } |
| 5679 | } |
| 5680 | // RTCP-MUX is enabled in all the contents. |
| 5681 | return true; |
| 5682 | } |
| 5683 | |
| 5684 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5685 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5686 | } |
| 5687 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5688 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5689 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5690 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5691 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5692 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5696 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5697 | } |
| 5698 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5699 | SdpType type = sdesc->GetType(); |
| 5700 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5701 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5702 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 5703 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5704 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | // Verify crypto settings. |
| 5708 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5709 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5710 | dtls_enabled_) { |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 5711 | RTCError crypto_error = |
| 5712 | VerifyCrypto(sdesc->description(), dtls_enabled_, uma_observer_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5713 | if (!crypto_error.ok()) { |
| 5714 | return crypto_error; |
| 5715 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | // Verify ice-ufrag and ice-pwd. |
| 5719 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5720 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5721 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5725 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5726 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5730 | // m-lines that do not rtcp-mux enabled. |
| 5731 | |
| 5732 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5733 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5734 | // With an answer we want to compare the new answer session description with |
| 5735 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5736 | const cricket::SessionDescription* offer_desc = |
| 5737 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5738 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5739 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5740 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5741 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5742 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5743 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5744 | } |
| 5745 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5746 | // The re-offers should respect the order of m= sections in current |
| 5747 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5748 | // With a re-offer, either the current local or current remote descriptions |
| 5749 | // could be the most up to date, so we would like to check against both of |
| 5750 | // them if they exist. It could be the case that one of them has a 0 port |
| 5751 | // for a media section, but the other does not. This is important to check |
| 5752 | // against in the case that we are recycling an m= section. |
| 5753 | const cricket::SessionDescription* current_desc = nullptr; |
| 5754 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5755 | if (local_description()) { |
| 5756 | current_desc = local_description()->description(); |
| 5757 | if (remote_description()) { |
| 5758 | secondary_current_desc = remote_description()->description(); |
| 5759 | } |
| 5760 | } else if (remote_description()) { |
| 5761 | current_desc = remote_description()->description(); |
| 5762 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5763 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5764 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5765 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5766 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5767 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5768 | } |
| 5769 | } |
| 5770 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 5771 | if (IsUnifiedPlan()) { |
| 5772 | // Ensure that each audio and video media section has at most one |
| 5773 | // "StreamParams". This will return an error if receiving a session |
| 5774 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 5775 | // same type. With Unified Plan, there can only be at most one track per |
| 5776 | // media section. |
| 5777 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 5778 | const MediaContentDescription& desc = *content.description; |
| 5779 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 5780 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 5781 | desc.streams().size() > 1u) { |
| 5782 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5783 | "Media section has more than one track specified " |
| 5784 | "with a=ssrc lines which is not supported with " |
| 5785 | "Unified Plan."); |
| 5786 | } |
| 5787 | } |
| 5788 | } |
| 5789 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5790 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5791 | } |
| 5792 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5793 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5794 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5795 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5796 | return (state == PeerConnectionInterface::kStable) || |
| 5797 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5798 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5799 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5800 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5801 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5802 | } |
| 5803 | } |
| 5804 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5805 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5806 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5807 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5808 | return (state == PeerConnectionInterface::kStable) || |
| 5809 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5810 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5811 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5812 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5813 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5814 | } |
| 5815 | } |
| 5816 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5817 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5818 | switch (error) { |
| 5819 | case SessionError::kNone: |
| 5820 | return "ERROR_NONE"; |
| 5821 | case SessionError::kContent: |
| 5822 | return "ERROR_CONTENT"; |
| 5823 | case SessionError::kTransport: |
| 5824 | return "ERROR_TRANSPORT"; |
| 5825 | } |
| 5826 | RTC_NOTREACHED(); |
| 5827 | return ""; |
| 5828 | } |
| 5829 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5830 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5831 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5832 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5833 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5834 | return desc.str(); |
| 5835 | } |
| 5836 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5837 | void PeerConnection::ReportSdpFormatReceived( |
| 5838 | const SessionDescriptionInterface& remote_offer) { |
| 5839 | if (!uma_observer_) { |
| 5840 | return; |
| 5841 | } |
| 5842 | int num_audio_mlines = 0; |
| 5843 | int num_video_mlines = 0; |
| 5844 | int num_audio_tracks = 0; |
| 5845 | int num_video_tracks = 0; |
| 5846 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 5847 | cricket::MediaType media_type = content.media_description()->type(); |
| 5848 | int num_tracks = std::max( |
| 5849 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 5850 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 5851 | num_audio_mlines += 1; |
| 5852 | num_audio_tracks += num_tracks; |
| 5853 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5854 | num_video_mlines += 1; |
| 5855 | num_video_tracks += num_tracks; |
| 5856 | } |
| 5857 | } |
| 5858 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 5859 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 5860 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 5861 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 5862 | format = kSdpFormatReceivedComplexPlanB; |
| 5863 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 5864 | format = kSdpFormatReceivedSimple; |
| 5865 | } |
| 5866 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpFormatReceived, format, |
| 5867 | kSdpFormatReceivedMax); |
| 5868 | } |
| 5869 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5870 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 5871 | const SessionDescriptionInterface& answer) { |
| 5872 | if (!uma_observer_) { |
| 5873 | return; |
| 5874 | } |
| 5875 | switch (answer.description()->msid_signaling()) { |
| 5876 | case 0: |
| 5877 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5878 | kSdpSemanticNegotiatedNone, |
| 5879 | kSdpSemanticNegotiatedMax); |
| 5880 | break; |
| 5881 | case cricket::kMsidSignalingMediaSection: |
| 5882 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5883 | kSdpSemanticNegotiatedUnifiedPlan, |
| 5884 | kSdpSemanticNegotiatedMax); |
| 5885 | break; |
| 5886 | case cricket::kMsidSignalingSsrcAttribute: |
| 5887 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5888 | kSdpSemanticNegotiatedPlanB, |
| 5889 | kSdpSemanticNegotiatedMax); |
| 5890 | break; |
| 5891 | case cricket::kMsidSignalingMediaSection | |
| 5892 | cricket::kMsidSignalingSsrcAttribute: |
| 5893 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5894 | kSdpSemanticNegotiatedMixed, |
| 5895 | kSdpSemanticNegotiatedMax); |
| 5896 | break; |
| 5897 | default: |
| 5898 | RTC_NOTREACHED(); |
| 5899 | } |
| 5900 | } |
| 5901 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5902 | // We need to check the local/remote description for the Transport instead of |
| 5903 | // the session, because a new Transport added during renegotiation may have |
| 5904 | // them unset while the session has them set from the previous negotiation. |
| 5905 | // Not doing so may trigger the auto generation of transport description and |
| 5906 | // mess up DTLS identity information, ICE credential, etc. |
| 5907 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5908 | const IceCandidateInterface* candidate, |
| 5909 | const SessionDescriptionInterface* remote_desc, |
| 5910 | bool* valid) { |
| 5911 | *valid = true; |
| 5912 | |
| 5913 | const SessionDescriptionInterface* current_remote_desc = |
| 5914 | remote_desc ? remote_desc : remote_description(); |
| 5915 | |
| 5916 | if (!current_remote_desc) { |
| 5917 | return false; |
| 5918 | } |
| 5919 | |
| 5920 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5921 | size_t remote_content_size = |
| 5922 | current_remote_desc->description()->contents().size(); |
| 5923 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5924 | RTC_LOG(LS_ERROR) |
| 5925 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5926 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5927 | |
| 5928 | *valid = false; |
| 5929 | return false; |
| 5930 | } |
| 5931 | |
| 5932 | cricket::ContentInfo content = |
| 5933 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5934 | |
| 5935 | const std::string transport_name = GetTransportName(content.name); |
| 5936 | if (transport_name.empty()) { |
| 5937 | return false; |
| 5938 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5939 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5940 | } |
| 5941 | |
| 5942 | bool PeerConnection::SrtpRequired() const { |
| 5943 | return dtls_enabled_ || |
| 5944 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5945 | } |
| 5946 | |
| 5947 | void PeerConnection::OnTransportControllerGatheringState( |
| 5948 | cricket::IceGatheringState state) { |
| 5949 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5950 | if (state == cricket::kIceGatheringGathering) { |
| 5951 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 5952 | } else if (state == cricket::kIceGatheringComplete) { |
| 5953 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5958 | std::map<std::string, std::set<cricket::MediaType>> |
| 5959 | media_types_by_transport_name; |
| 5960 | for (auto transceiver : transceivers_) { |
| 5961 | if (transceiver->internal()->channel()) { |
| 5962 | const std::string& transport_name = |
| 5963 | transceiver->internal()->channel()->transport_name(); |
| 5964 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5965 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5966 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5967 | } |
| 5968 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5969 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 5970 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5971 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5972 | |
| 5973 | rtc::Optional<std::string> transport_name = sctp_transport_name(); |
| 5974 | if (transport_name) { |
| 5975 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5976 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5977 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5978 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5979 | for (const auto& entry : media_types_by_transport_name) { |
| 5980 | const std::string& transport_name = entry.first; |
| 5981 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5982 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5983 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5984 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5985 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5986 | } |
| 5987 | } |
| 5988 | } |
| 5989 | // Walk through the ConnectionInfos to gather best connection usage |
| 5990 | // for IPv4 and IPv6. |
| 5991 | void PeerConnection::ReportBestConnectionState( |
| 5992 | const cricket::TransportStats& stats) { |
| 5993 | RTC_DCHECK(metrics_observer()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5994 | for (const cricket::TransportChannelStats& channel_stats : |
| 5995 | stats.channel_stats) { |
| 5996 | for (const cricket::ConnectionInfo& connection_info : |
| 5997 | channel_stats.connection_infos) { |
| 5998 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5999 | continue; |
| 6000 | } |
| 6001 | |
| 6002 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6003 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6004 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6005 | |
| 6006 | // Increment the counter for IceCandidatePairType. |
| 6007 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6008 | (local.type() == RELAY_PORT_TYPE && |
| 6009 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 6010 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 6011 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 6012 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 6013 | } else { |
| 6014 | RTC_CHECK(0); |
| 6015 | } |
| 6016 | metrics_observer()->IncrementEnumCounter( |
| 6017 | type, GetIceCandidatePairCounter(local, remote), |
| 6018 | kIceCandidatePairMax); |
| 6019 | |
| 6020 | // Increment the counter for IP type. |
| 6021 | if (local.address().family() == AF_INET) { |
| 6022 | metrics_observer()->IncrementEnumCounter( |
| 6023 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 6024 | kPeerConnectionAddressFamilyCounter_Max); |
| 6025 | |
| 6026 | } else if (local.address().family() == AF_INET6) { |
| 6027 | metrics_observer()->IncrementEnumCounter( |
| 6028 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 6029 | kPeerConnectionAddressFamilyCounter_Max); |
| 6030 | } else { |
| 6031 | RTC_CHECK(0); |
| 6032 | } |
| 6033 | |
| 6034 | return; |
| 6035 | } |
| 6036 | } |
| 6037 | } |
| 6038 | |
| 6039 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6040 | const cricket::TransportStats& stats, |
| 6041 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6042 | RTC_DCHECK(metrics_observer()); |
| 6043 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6044 | return; |
| 6045 | } |
| 6046 | |
| 6047 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6048 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6049 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6050 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6051 | return; |
| 6052 | } |
| 6053 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6054 | for (cricket::MediaType media_type : media_types) { |
| 6055 | PeerConnectionEnumCounterType srtp_counter_type; |
| 6056 | PeerConnectionEnumCounterType ssl_counter_type; |
| 6057 | switch (media_type) { |
| 6058 | case cricket::MEDIA_TYPE_AUDIO: |
| 6059 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 6060 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 6061 | break; |
| 6062 | case cricket::MEDIA_TYPE_VIDEO: |
| 6063 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 6064 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 6065 | break; |
| 6066 | case cricket::MEDIA_TYPE_DATA: |
| 6067 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 6068 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 6069 | break; |
| 6070 | default: |
| 6071 | RTC_NOTREACHED(); |
| 6072 | continue; |
| 6073 | } |
| 6074 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6075 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 6076 | srtp_crypto_suite); |
| 6077 | } |
| 6078 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6079 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 6080 | ssl_cipher_suite); |
| 6081 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6086 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6087 | RTC_DCHECK(call_); |
| 6088 | call_->OnSentPacket(sent_packet); |
| 6089 | } |
| 6090 | |
| 6091 | const std::string PeerConnection::GetTransportName( |
| 6092 | const std::string& content_name) { |
| 6093 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6094 | if (channel) { |
| 6095 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6096 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6097 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6098 | RTC_DCHECK(sctp_mid_); |
| 6099 | if (content_name == *sctp_mid_) { |
| 6100 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6101 | } |
| 6102 | } |
| 6103 | // Return an empty string if failed to retrieve the transport name. |
| 6104 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6105 | } |
| 6106 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6107 | void PeerConnection::DestroyTransceiverChannel( |
| 6108 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6109 | transceiver) { |
| 6110 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6111 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6112 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 6113 | if (channel) { |
| 6114 | transceiver->internal()->SetChannel(nullptr); |
| 6115 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6116 | } |
| 6117 | } |
| 6118 | |
| 6119 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6120 | if (rtp_data_channel_) { |
| 6121 | OnDataChannelDestroyed(); |
| 6122 | DestroyBaseChannel(rtp_data_channel_); |
| 6123 | rtp_data_channel_ = nullptr; |
| 6124 | } |
| 6125 | |
| 6126 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 6127 | // grab a reference to this PeerConnection. If this is called from the |
| 6128 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 6129 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 6130 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 6131 | |
| 6132 | if (sctp_transport_) { |
| 6133 | OnDataChannelDestroyed(); |
| 6134 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 6135 | [this] { DestroySctpTransport_n(); }); |
| 6136 | } |
| 6137 | } |
| 6138 | |
| 6139 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 6140 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6141 | switch (channel->media_type()) { |
| 6142 | case cricket::MEDIA_TYPE_AUDIO: |
| 6143 | channel_manager()->DestroyVoiceChannel( |
| 6144 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6145 | break; |
| 6146 | case cricket::MEDIA_TYPE_VIDEO: |
| 6147 | channel_manager()->DestroyVideoChannel( |
| 6148 | static_cast<cricket::VideoChannel*>(channel)); |
| 6149 | break; |
| 6150 | case cricket::MEDIA_TYPE_DATA: |
| 6151 | channel_manager()->DestroyRtpDataChannel( |
| 6152 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6153 | break; |
| 6154 | default: |
| 6155 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6156 | break; |
| 6157 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6158 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6159 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6160 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6161 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6162 | RtpTransportInternal* rtp_transport, |
| 6163 | cricket::DtlsTransportInternal* dtls_transport) { |
| 6164 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6165 | auto base_channel = GetChannel(mid); |
| 6166 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6167 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6168 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6169 | if (sctp_transport_ && mid == sctp_mid_) { |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6170 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6171 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6172 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6173 | } |
| 6174 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6175 | void PeerConnection::ClearStatsCache() { |
| 6176 | if (stats_collector_) { |
| 6177 | stats_collector_->ClearCachedStatsReport(); |
| 6178 | } |
| 6179 | } |
| 6180 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6181 | } // namespace webrtc |