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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/peer_connection.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> |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 14 | #include <limits> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 15 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 16 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 17 | #include <utility> |
| 18 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 20 | #include "absl/memory/memory.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 21:02:22 +0100 | [diff] [blame] | 21 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "api/jsep_ice_candidate.h" |
| 23 | #include "api/jsep_session_description.h" |
| 24 | #include "api/media_stream_proxy.h" |
| 25 | #include "api/media_stream_track_proxy.h" |
| 26 | #include "api/uma_metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "call/call.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "logging/rtc_event_log/ice_logger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 29 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "logging/rtc_event_log/rtc_event_log.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "media/sctp/sctp_transport.h" |
| 32 | #include "pc/audio_track.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 33 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "pc/channel_manager.h" |
| 35 | #include "pc/dtmf_sender.h" |
| 36 | #include "pc/media_stream.h" |
| 37 | #include "pc/media_stream_observer.h" |
| 38 | #include "pc/remote_audio_source.h" |
| 39 | #include "pc/rtp_media_utils.h" |
| 40 | #include "pc/rtp_receiver.h" |
| 41 | #include "pc/rtp_sender.h" |
| 42 | #include "pc/sctp_utils.h" |
| 43 | #include "pc/sdp_utils.h" |
| 44 | #include "pc/stream_collection.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 45 | #include "pc/video_capturer_track_source.h" |
| 46 | #include "pc/video_track.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 47 | #include "rtc_base/bind.h" |
| 48 | #include "rtc_base/checks.h" |
| 49 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 50 | #include "rtc_base/numerics/safe_conversions.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 51 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 52 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 53 | #include "rtc_base/trace_event.h" |
| 54 | #include "system_wrappers/include/clock.h" |
| 55 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 56 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 58 | using cricket::ContentInfo; |
| 59 | using cricket::ContentInfos; |
| 60 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 61 | using cricket::MediaProtocolType; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 62 | using cricket::SessionDescription; |
| 63 | using cricket::SimulcastLayerList; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 64 | using cricket::TransportInfo; |
| 65 | |
| 66 | using cricket::LOCAL_PORT_TYPE; |
| 67 | using cricket::STUN_PORT_TYPE; |
| 68 | using cricket::RELAY_PORT_TYPE; |
| 69 | using cricket::PRFLX_PORT_TYPE; |
| 70 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 71 | namespace webrtc { |
| 72 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 73 | // Error messages |
| 74 | const char kBundleWithoutRtcpMux[] = |
| 75 | "rtcp-mux must be enabled when BUNDLE " |
| 76 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 77 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 78 | const char kInvalidSdp[] = "Invalid session description."; |
| 79 | const char kMlineMismatchInAnswer[] = |
| 80 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 81 | "answer."; |
| 82 | const char kMlineMismatchInSubsequentOffer[] = |
| 83 | "The order of m-lines in subsequent offer doesn't match order from " |
| 84 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 85 | const char kSdpWithoutDtlsFingerprint[] = |
| 86 | "Called with SDP without DTLS fingerprint."; |
| 87 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 88 | const char kSdpWithoutIceUfragPwd[] = |
| 89 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 90 | const char kSessionError[] = "Session error code: "; |
| 91 | const char kSessionErrorDesc[] = "Session error description: "; |
| 92 | const char kDtlsSrtpSetupFailureRtp[] = |
| 93 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 94 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 95 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 97 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 99 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 100 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 101 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 102 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 103 | // The length of RTCP CNAMEs. |
| 104 | static const int kRtcpCnameLength = 16; |
| 105 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 106 | enum { |
| 107 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 108 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 109 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 110 | MSG_GETSTATS, |
| 111 | MSG_FREE_DATACHANNELS, |
| 112 | MSG_REPORT_USAGE_PATTERN, |
| 113 | }; |
| 114 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 115 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 116 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 117 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 118 | explicit SetSessionDescriptionMsg( |
| 119 | webrtc::SetSessionDescriptionObserver* observer) |
| 120 | : observer(observer) {} |
| 121 | |
| 122 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 123 | RTCError error; |
| 124 | }; |
| 125 | |
| 126 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 127 | explicit CreateSessionDescriptionMsg( |
| 128 | webrtc::CreateSessionDescriptionObserver* observer) |
| 129 | : observer(observer) {} |
| 130 | |
| 131 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 132 | RTCError error; |
| 133 | }; |
| 134 | |
| 135 | struct GetStatsMsg : public rtc::MessageData { |
| 136 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 137 | webrtc::MediaStreamTrackInterface* track) |
| 138 | : observer(observer), track(track) {} |
| 139 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 140 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 141 | }; |
| 142 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 143 | // Check if we can send |new_stream| on a PeerConnection. |
| 144 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 145 | webrtc::MediaStreamInterface* new_stream) { |
| 146 | if (!new_stream || !current_streams) { |
| 147 | return false; |
| 148 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 149 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 150 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 151 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 157 | // If the direction is "recvonly" or "inactive", treat the description |
| 158 | // as containing no streams. |
| 159 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 160 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 161 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 162 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 163 | ? desc->streams() |
| 164 | : std::vector<cricket::StreamParams>(); |
| 165 | } |
| 166 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 167 | bool IsValidOfferToReceiveMedia(int value) { |
| 168 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 169 | return (value >= Options::kUndefined) && |
| 170 | (value <= Options::kMaxOfferToReceiveMedia); |
| 171 | } |
| 172 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 173 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 174 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 175 | const std::vector<rtc::scoped_refptr< |
| 176 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 177 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 178 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 179 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 180 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 181 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 182 | if (audio_media_description_options) { |
| 183 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 184 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 185 | } |
| 186 | } else { |
| 187 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 188 | if (video_media_description_options) { |
| 189 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 190 | sender->id(), sender->internal()->stream_ids(), {}, |
| 191 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 194 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 195 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 196 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 197 | // Add options to |session_options| from |rtp_data_channels|. |
| 198 | void AddRtpDataChannelOptions( |
| 199 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 200 | rtp_data_channels, |
| 201 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 202 | if (!data_media_description_options) { |
| 203 | return; |
| 204 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 205 | // Check for data channels. |
| 206 | for (const auto& kv : rtp_data_channels) { |
| 207 | const DataChannel* channel = kv.second; |
| 208 | if (channel->state() == DataChannel::kConnecting || |
| 209 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 210 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 211 | // the data channel's label. |
| 212 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 213 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 218 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 219 | PeerConnectionInterface::IceTransportsType type) { |
| 220 | switch (type) { |
| 221 | case PeerConnectionInterface::kNone: |
| 222 | return cricket::CF_NONE; |
| 223 | case PeerConnectionInterface::kRelay: |
| 224 | return cricket::CF_RELAY; |
| 225 | case PeerConnectionInterface::kNoHost: |
| 226 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 227 | case PeerConnectionInterface::kAll: |
| 228 | return cricket::CF_ALL; |
| 229 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 230 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 231 | } |
| 232 | return cricket::CF_NONE; |
| 233 | } |
| 234 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 235 | // Helper to set an error and return from a method. |
| 236 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 237 | if (error) { |
| 238 | error->set_type(type); |
| 239 | } |
| 240 | return type == webrtc::RTCErrorType::NONE; |
| 241 | } |
| 242 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 243 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 244 | bool ok = error.ok(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 245 | if (error_out) { |
| 246 | *error_out = std::move(error); |
| 247 | } |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 248 | return ok; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 251 | std::string GetSignalingStateString( |
| 252 | PeerConnectionInterface::SignalingState state) { |
| 253 | switch (state) { |
| 254 | case PeerConnectionInterface::kStable: |
| 255 | return "kStable"; |
| 256 | case PeerConnectionInterface::kHaveLocalOffer: |
| 257 | return "kHaveLocalOffer"; |
| 258 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 259 | return "kHavePrAnswer"; |
| 260 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 261 | return "kHaveRemoteOffer"; |
| 262 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 263 | return "kHaveRemotePrAnswer"; |
| 264 | case PeerConnectionInterface::kClosed: |
| 265 | return "kClosed"; |
| 266 | } |
| 267 | RTC_NOTREACHED(); |
| 268 | return ""; |
| 269 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 270 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 271 | IceCandidatePairType GetIceCandidatePairCounter( |
| 272 | const cricket::Candidate& local, |
| 273 | const cricket::Candidate& remote) { |
| 274 | const auto& l = local.type(); |
| 275 | const auto& r = remote.type(); |
| 276 | const auto& host = LOCAL_PORT_TYPE; |
| 277 | const auto& srflx = STUN_PORT_TYPE; |
| 278 | const auto& relay = RELAY_PORT_TYPE; |
| 279 | const auto& prflx = PRFLX_PORT_TYPE; |
| 280 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 281 | bool local_hostname = |
| 282 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 283 | bool remote_hostname = !remote.address().hostname().empty() && |
| 284 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 285 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 286 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 287 | if (local_hostname) { |
| 288 | if (remote_hostname) { |
| 289 | return kIceCandidatePairHostNameHostName; |
| 290 | } else if (remote_private) { |
| 291 | return kIceCandidatePairHostNameHostPrivate; |
| 292 | } else { |
| 293 | return kIceCandidatePairHostNameHostPublic; |
| 294 | } |
| 295 | } else if (local_private) { |
| 296 | if (remote_hostname) { |
| 297 | return kIceCandidatePairHostPrivateHostName; |
| 298 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 299 | return kIceCandidatePairHostPrivateHostPrivate; |
| 300 | } else { |
| 301 | return kIceCandidatePairHostPrivateHostPublic; |
| 302 | } |
| 303 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 304 | if (remote_hostname) { |
| 305 | return kIceCandidatePairHostPublicHostName; |
| 306 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 307 | return kIceCandidatePairHostPublicHostPrivate; |
| 308 | } else { |
| 309 | return kIceCandidatePairHostPublicHostPublic; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | if (l == host && r == srflx) |
| 314 | return kIceCandidatePairHostSrflx; |
| 315 | if (l == host && r == relay) |
| 316 | return kIceCandidatePairHostRelay; |
| 317 | if (l == host && r == prflx) |
| 318 | return kIceCandidatePairHostPrflx; |
| 319 | if (l == srflx && r == host) |
| 320 | return kIceCandidatePairSrflxHost; |
| 321 | if (l == srflx && r == srflx) |
| 322 | return kIceCandidatePairSrflxSrflx; |
| 323 | if (l == srflx && r == relay) |
| 324 | return kIceCandidatePairSrflxRelay; |
| 325 | if (l == srflx && r == prflx) |
| 326 | return kIceCandidatePairSrflxPrflx; |
| 327 | if (l == relay && r == host) |
| 328 | return kIceCandidatePairRelayHost; |
| 329 | if (l == relay && r == srflx) |
| 330 | return kIceCandidatePairRelaySrflx; |
| 331 | if (l == relay && r == relay) |
| 332 | return kIceCandidatePairRelayRelay; |
| 333 | if (l == relay && r == prflx) |
| 334 | return kIceCandidatePairRelayPrflx; |
| 335 | if (l == prflx && r == host) |
| 336 | return kIceCandidatePairPrflxHost; |
| 337 | if (l == prflx && r == srflx) |
| 338 | return kIceCandidatePairPrflxSrflx; |
| 339 | if (l == prflx && r == relay) |
| 340 | return kIceCandidatePairPrflxRelay; |
| 341 | return kIceCandidatePairMax; |
| 342 | } |
| 343 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 344 | // Logic to decide if an m= section can be recycled. This means that the new |
| 345 | // m= section is not rejected, but the old local or remote m= section is |
| 346 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 347 | // of the old remote and old local descriptions in no particular order. |
| 348 | // We need to check both the old local and remote because either |
| 349 | // could be the most current from the latest negotation. |
| 350 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 351 | const ContentInfo& content, |
| 352 | const ContentInfo* old_content_one, |
| 353 | const ContentInfo* old_content_two) { |
| 354 | return type == SdpType::kOffer && !content.rejected && |
| 355 | ((old_content_one && old_content_one->rejected) || |
| 356 | (old_content_two && old_content_two->rejected)); |
| 357 | } |
| 358 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 359 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 360 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 361 | // less than |current_desc|. In the case of checking an answer's |
| 362 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 363 | // local or remote. In the case of checking an offer's |new_desc| we |
| 364 | // check against the local and remote descriptions stored from the last |
| 365 | // negotiation, because either of these could be the most up to date for |
| 366 | // possible rejected m sections. These are the |current_desc| and |
| 367 | // |secondary_current_desc|. |
| 368 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 369 | const SessionDescription* secondary_current_desc, |
| 370 | const SessionDescription& new_desc, |
| 371 | const SdpType type) { |
| 372 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 376 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 377 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 378 | if (secondary_current_desc && |
| 379 | i < secondary_current_desc->contents().size()) { |
| 380 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 381 | } |
| 382 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 383 | ¤t_desc.contents()[i], |
| 384 | secondary_content_info)) { |
| 385 | // For new offer descriptions, if the media section can be recycled, it's |
| 386 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 387 | continue; |
| 388 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 389 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 393 | new_desc.contents()[i].media_description(); |
| 394 | const MediaContentDescription* current_desc_mdesc = |
| 395 | current_desc.contents()[i].media_description(); |
| 396 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 403 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 404 | const SessionDescription& desc2) { |
| 405 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 408 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 409 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 410 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 411 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 412 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 413 | static constexpr struct { |
| 414 | KeyExchangeProtocolType protocol_type; |
| 415 | cricket::MediaType media_type; |
| 416 | KeyExchangeProtocolMedia protocol_media; |
| 417 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 418 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 419 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 420 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 421 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 422 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 423 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 424 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 425 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 426 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 427 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 428 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 429 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 430 | }; |
| 431 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 432 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 433 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 434 | |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 435 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 436 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 437 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 438 | i.protocol_media, |
| 439 | kEnumCounterKeyProtocolMediaTypeMax); |
| 440 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 444 | void NoteAddIceCandidateResult(int result) { |
| 445 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 446 | kAddIceCandidateMax); |
| 447 | } |
| 448 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 449 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 450 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 451 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 452 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 453 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 454 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 455 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 456 | const cricket::ContentGroup* bundle = |
| 457 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 458 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 459 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 460 | continue; |
| 461 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 462 | // Note what media is used with each crypto protocol, for all sections. |
| 463 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 464 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 465 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 466 | const std::string& mid = content_info.name; |
| 467 | if (bundle && bundle->HasContentName(mid) && |
| 468 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 469 | // This isn't the first media section in the BUNDLE group, so it's not |
| 470 | // required to have crypto attributes, since only the crypto attributes |
| 471 | // from the first section actually get used. |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | // If the content isn't rejected or bundled into another m= section, crypto |
| 476 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 477 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 478 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 479 | if (!media || !tinfo) { |
| 480 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 481 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 482 | } |
| 483 | if (dtls_enabled) { |
| 484 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 485 | RTC_LOG(LS_WARNING) |
| 486 | << "Session description must have DTLS fingerprint if " |
| 487 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 488 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 489 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 490 | } |
| 491 | } else { |
| 492 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 493 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 494 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 495 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 499 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 503 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 504 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 505 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 506 | const cricket::ContentGroup* bundle = |
| 507 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 508 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 509 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 510 | continue; |
| 511 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 512 | const std::string& mid = content_info.name; |
| 513 | if (bundle && bundle->HasContentName(mid) && |
| 514 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 515 | // This isn't the first media section in the BUNDLE group, so it's not |
| 516 | // required to have ufrag/password, since only the ufrag/password from |
| 517 | // the first section actually get used. |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | // If the content isn't rejected or bundled into another m= section, |
| 522 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 523 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 524 | if (!tinfo) { |
| 525 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 526 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 527 | return false; |
| 528 | } |
| 529 | if (tinfo->description.ice_ufrag.empty() || |
| 530 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 531 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 532 | return false; |
| 533 | } |
| 534 | } |
| 535 | return true; |
| 536 | } |
| 537 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 538 | // Get the SCTP port out of a SessionDescription. |
| 539 | // Return -1 if not found. |
| 540 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 541 | const cricket::DataContentDescription* data_desc = |
| 542 | GetFirstDataContentDescription(session_description); |
| 543 | RTC_DCHECK(data_desc); |
| 544 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 545 | return -1; |
| 546 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 547 | std::string value; |
| 548 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 549 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 550 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 551 | if (!codec.Matches(match_pattern)) { |
| 552 | continue; |
| 553 | } |
| 554 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 555 | return rtc::FromString<int>(value); |
| 556 | } |
| 557 | } |
| 558 | return -1; |
| 559 | } |
| 560 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 561 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 562 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 563 | const SessionDescriptionInterface* new_desc, |
| 564 | const std::string& content_name) { |
| 565 | if (!old_desc) { |
| 566 | return false; |
| 567 | } |
| 568 | const SessionDescription* new_sd = new_desc->description(); |
| 569 | const SessionDescription* old_sd = old_desc->description(); |
| 570 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 571 | if (!cinfo || cinfo->rejected) { |
| 572 | return false; |
| 573 | } |
| 574 | // If the content isn't rejected, check if ufrag and password has changed. |
| 575 | const cricket::TransportDescription* new_transport_desc = |
| 576 | new_sd->GetTransportDescriptionByName(content_name); |
| 577 | const cricket::TransportDescription* old_transport_desc = |
| 578 | old_sd->GetTransportDescriptionByName(content_name); |
| 579 | if (!new_transport_desc || !old_transport_desc) { |
| 580 | // No transport description exists. This is not an ICE restart. |
| 581 | return false; |
| 582 | } |
| 583 | if (cricket::IceCredentialsChanged( |
| 584 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 585 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 586 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 587 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 588 | return true; |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 593 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 594 | // from an RTCError. |
| 595 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 596 | SdpType type, |
| 597 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 598 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 599 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 600 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 601 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 604 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 605 | std::string output = "streams=["; |
| 606 | const char* separator = ""; |
| 607 | for (const auto& stream_id : stream_ids) { |
| 608 | output.append(separator).append(stream_id); |
| 609 | separator = ", "; |
| 610 | } |
| 611 | output.append("]"); |
| 612 | return output; |
| 613 | } |
| 614 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 615 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 616 | int rtc_configuration_parameter) { |
| 617 | if (rtc_configuration_parameter == |
| 618 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 619 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 620 | } |
| 621 | return rtc_configuration_parameter; |
| 622 | } |
| 623 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 624 | cricket::DataMessageType ToCricketDataMessageType(DataMessageType type) { |
| 625 | switch (type) { |
| 626 | case DataMessageType::kText: |
| 627 | return cricket::DMT_TEXT; |
| 628 | case DataMessageType::kBinary: |
| 629 | return cricket::DMT_BINARY; |
| 630 | case DataMessageType::kControl: |
| 631 | return cricket::DMT_CONTROL; |
| 632 | default: |
| 633 | return cricket::DMT_NONE; |
| 634 | } |
| 635 | return cricket::DMT_NONE; |
| 636 | } |
| 637 | |
| 638 | DataMessageType ToWebrtcDataMessageType(cricket::DataMessageType type) { |
| 639 | switch (type) { |
| 640 | case cricket::DMT_TEXT: |
| 641 | return DataMessageType::kText; |
| 642 | case cricket::DMT_BINARY: |
| 643 | return DataMessageType::kBinary; |
| 644 | case cricket::DMT_CONTROL: |
| 645 | return DataMessageType::kControl; |
| 646 | case cricket::DMT_NONE: |
| 647 | default: |
| 648 | RTC_NOTREACHED(); |
| 649 | } |
| 650 | return DataMessageType::kControl; |
| 651 | } |
| 652 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 653 | } // namespace |
| 654 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 655 | // Upon completion, posts a task to execute the callback of the |
| 656 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 657 | // point, the state of the peer connection might no longer reflect the effects |
| 658 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 659 | // modified during the post. |
| 660 | // TODO(hbos): Remove this class once we remove the version of |
| 661 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 662 | // SetSessionDescriptionObserver as an argument. |
| 663 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 664 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 665 | public: |
| 666 | SetRemoteDescriptionObserverAdapter( |
| 667 | rtc::scoped_refptr<PeerConnection> pc, |
| 668 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 669 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 670 | |
| 671 | // SetRemoteDescriptionObserverInterface implementation. |
| 672 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 673 | if (error.ok()) |
| 674 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 675 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 676 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | private: |
| 680 | rtc::scoped_refptr<PeerConnection> pc_; |
| 681 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 682 | }; |
| 683 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 684 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 685 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 686 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 687 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 688 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 689 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 690 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 691 | BundlePolicy bundle_policy; |
| 692 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 693 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 694 | int ice_candidate_pool_size; |
| 695 | bool disable_ipv6; |
| 696 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 697 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 698 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 699 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 700 | absl::optional<int> screencast_min_bitrate; |
| 701 | absl::optional<bool> combined_audio_video_bwe; |
| 702 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 703 | TcpCandidatePolicy tcp_candidate_policy; |
| 704 | CandidateNetworkPolicy candidate_network_policy; |
| 705 | int audio_jitter_buffer_max_packets; |
| 706 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 707 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 708 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 709 | int ice_connection_receiving_timeout; |
| 710 | int ice_backup_candidate_pair_ping_interval; |
| 711 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 712 | bool prioritize_most_likely_ice_candidate_pairs; |
| 713 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 714 | bool prune_turn_ports; |
| 715 | bool presume_writable_when_fully_relayed; |
| 716 | bool enable_ice_renomination; |
| 717 | bool redetermine_role_on_ice_restart; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 718 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 719 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 720 | absl::optional<int> ice_check_min_interval; |
| 721 | absl::optional<int> ice_unwritable_timeout; |
| 722 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 723 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 724 | absl::optional<int> stun_candidate_keepalive_interval; |
| 725 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 726 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 727 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 728 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 729 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 730 | bool use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 731 | bool use_media_transport_for_data_channels; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 732 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 733 | bool offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 734 | }; |
| 735 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 736 | "Did you add something to RTCConfiguration and forget to " |
| 737 | "update operator==?"); |
| 738 | return type == o.type && servers == o.servers && |
| 739 | bundle_policy == o.bundle_policy && |
| 740 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 741 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 742 | candidate_network_policy == o.candidate_network_policy && |
| 743 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 744 | audio_jitter_buffer_fast_accelerate == |
| 745 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 746 | audio_jitter_buffer_min_delay_ms == |
| 747 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 748 | audio_jitter_buffer_enable_rtx_handling == |
| 749 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 750 | ice_connection_receiving_timeout == |
| 751 | o.ice_connection_receiving_timeout && |
| 752 | ice_backup_candidate_pair_ping_interval == |
| 753 | o.ice_backup_candidate_pair_ping_interval && |
| 754 | continual_gathering_policy == o.continual_gathering_policy && |
| 755 | certificates == o.certificates && |
| 756 | prioritize_most_likely_ice_candidate_pairs == |
| 757 | o.prioritize_most_likely_ice_candidate_pairs && |
| 758 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 759 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 760 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 761 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 762 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 763 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 764 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 765 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 766 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 767 | prune_turn_ports == o.prune_turn_ports && |
| 768 | presume_writable_when_fully_relayed == |
| 769 | o.presume_writable_when_fully_relayed && |
| 770 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 771 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 772 | ice_check_interval_strong_connectivity == |
| 773 | o.ice_check_interval_strong_connectivity && |
| 774 | ice_check_interval_weak_connectivity == |
| 775 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 776 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 777 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 778 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 779 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 780 | stun_candidate_keepalive_interval == |
| 781 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 782 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 783 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 784 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 785 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 786 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 787 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 788 | use_media_transport_for_data_channels == |
| 789 | o.use_media_transport_for_data_channels && |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 790 | crypto_options == o.crypto_options && |
| 791 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 795 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 796 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 797 | } |
| 798 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 799 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 800 | std::string GenerateRtcpCname() { |
| 801 | std::string cname; |
| 802 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 803 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 804 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 805 | } |
| 806 | return cname; |
| 807 | } |
| 808 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 809 | bool ValidateOfferAnswerOptions( |
| 810 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 811 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 812 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 813 | } |
| 814 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 815 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 816 | // m= sections (in other words, nothing that involves a map/array). |
| 817 | void ExtractSharedMediaSessionOptions( |
| 818 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 819 | cricket::MediaSessionOptions* session_options) { |
| 820 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 821 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 822 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 823 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 824 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 825 | std::unique_ptr<RtcEventLog> event_log, |
| 826 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 827 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 828 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 829 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 830 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 831 | remote_streams_(StreamCollection::Create()), |
| 832 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 833 | |
| 834 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 835 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 836 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 837 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 838 | // Need to stop transceivers before destroying the stats collector because |
| 839 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 840 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 841 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 842 | transceiver->Stop(); |
| 843 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 844 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 845 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 846 | if (stats_collector_) { |
| 847 | stats_collector_->WaitForPendingRequest(); |
| 848 | stats_collector_ = nullptr; |
| 849 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 850 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 851 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 852 | // the last stats request can still read from the channels. |
| 853 | DestroyAllChannels(); |
| 854 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 855 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 856 | |
| 857 | webrtc_session_desc_factory_.reset(); |
| 858 | sctp_invoker_.reset(); |
| 859 | sctp_factory_.reset(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 860 | media_transport_invoker_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 861 | transport_controller_.reset(); |
| 862 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 863 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 864 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 865 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 866 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 867 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 868 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 869 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 870 | event_log_.reset(); |
| 871 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 874 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 875 | // Destroy video channels first since they may have a pointer to a voice |
| 876 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 877 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 878 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 879 | DestroyTransceiverChannel(transceiver); |
| 880 | } |
| 881 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 882 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 883 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 884 | DestroyTransceiverChannel(transceiver); |
| 885 | } |
| 886 | } |
| 887 | DestroyDataChannel(); |
| 888 | } |
| 889 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 890 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 891 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 892 | PeerConnectionDependencies dependencies) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 893 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 894 | |
| 895 | RTCError config_error = ValidateConfiguration(configuration); |
| 896 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 897 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 901 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 902 | RTC_LOG(LS_ERROR) |
| 903 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 904 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 905 | return false; |
| 906 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 907 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 908 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 909 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 910 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 911 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 912 | return false; |
| 913 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 914 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 915 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 916 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 917 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 918 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 919 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 920 | cricket::ServerAddresses stun_servers; |
| 921 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 922 | |
| 923 | RTCErrorType parse_error = |
| 924 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 925 | if (parse_error != RTCErrorType::NONE) { |
| 926 | return false; |
| 927 | } |
| 928 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 929 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 930 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 931 | if (!network_thread()->Invoke<bool>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 932 | RTC_FROM_HERE, |
| 933 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
| 934 | stun_servers, turn_servers, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 935 | return false; |
| 936 | } |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 937 | // If initialization was successful, note if STUN or TURN servers |
| 938 | // were supplied. |
| 939 | if (!stun_servers.empty()) { |
| 940 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 941 | } |
| 942 | if (!turn_servers.empty()) { |
| 943 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 944 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 945 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 946 | // Send information about IPv4/IPv6 status. |
| 947 | PeerConnectionAddressFamilyCounter address_family; |
| 948 | if (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
| 949 | address_family = kPeerConnection_IPv6; |
| 950 | } else { |
| 951 | address_family = kPeerConnection_IPv4; |
| 952 | } |
| 953 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 954 | kPeerConnectionAddressFamilyCounter_Max); |
| 955 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 956 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 957 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 958 | // RFC 3264: The numeric value of the session id and version in the |
| 959 | // o line MUST be representable with a "64 bit signed integer". |
| 960 | // Due to this constraint session id |session_id_| is max limited to |
| 961 | // LLONG_MAX. |
| 962 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 963 | JsepTransportController::Config config; |
| 964 | config.redetermine_role_on_ice_restart = |
| 965 | configuration.redetermine_role_on_ice_restart; |
| 966 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 967 | config.disable_encryption = options.disable_encryption; |
| 968 | config.bundle_policy = configuration.bundle_policy; |
| 969 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 970 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 971 | // stub. |
| 972 | config.crypto_options = configuration.crypto_options.has_value() |
| 973 | ? *configuration.crypto_options |
| 974 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 975 | config.transport_observer = this; |
Qingsi Wang | 7685e86 | 2018-06-11 20:15:46 -0700 | [diff] [blame] | 976 | config.event_log = event_log_.get(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 977 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 978 | config.enable_external_auth = true; |
| 979 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 980 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 981 | |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 982 | if (configuration.use_media_transport || |
| 983 | configuration.use_media_transport_for_data_channels) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 984 | if (!factory_->media_transport_factory()) { |
| 985 | RTC_DCHECK(false) |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 986 | << "PeerConnecton is initialized with use_media_transport = true or " |
| 987 | << "use_media_transport_for_data_channels = true " |
| 988 | << "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 989 | return false; |
| 990 | } |
| 991 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 992 | config.use_media_transport_for_media = configuration.use_media_transport; |
| 993 | config.use_media_transport_for_data_channels = |
| 994 | configuration.use_media_transport_for_data_channels; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 995 | config.media_transport_factory = factory_->media_transport_factory(); |
| 996 | } |
| 997 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 998 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 999 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1000 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1001 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1002 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1003 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1004 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1005 | transport_controller_->SignalConnectionState.connect( |
| 1006 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1007 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1008 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1009 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1010 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1011 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1012 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1013 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1014 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 1015 | |
| 1016 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1017 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1018 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1019 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1020 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1021 | configuration_ = configuration; |
| 1022 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1023 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1024 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1025 | if (!configuration.certificates.empty()) { |
| 1026 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1027 | // just picking the first one. The decision should be made based on the DTLS |
| 1028 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1029 | certificate = configuration.certificates[0]; |
| 1030 | } |
| 1031 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1032 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1033 | |
| 1034 | if (options.disable_encryption) { |
| 1035 | dtls_enabled_ = false; |
| 1036 | } else { |
| 1037 | // 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] | 1038 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1039 | // |configuration| can override the default |dtls_enabled_| value. |
| 1040 | if (configuration.enable_dtls_srtp) { |
| 1041 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1042 | } |
| 1043 | } |
| 1044 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 1045 | if (configuration.use_media_transport_for_data_channels) { |
| 1046 | if (configuration.enable_rtp_data_channel) { |
| 1047 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1048 | "use_media_transport_for_data_channels are " |
| 1049 | "incompatible and cannot both be set to true"; |
| 1050 | return false; |
| 1051 | } |
| 1052 | data_channel_type_ = cricket::DCT_MEDIA_TRANSPORT; |
| 1053 | } else if (configuration.enable_rtp_data_channel) { |
| 1054 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1055 | // set. It takes precendence over the disable_sctp_data_channels |
| 1056 | // PeerConnectionFactoryInterface::Options. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1057 | data_channel_type_ = cricket::DCT_RTP; |
| 1058 | } else { |
| 1059 | // DTLS has to be enabled to use SCTP. |
| 1060 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1061 | data_channel_type_ = cricket::DCT_SCTP; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | video_options_.screencast_min_bitrate_kbps = |
| 1066 | configuration.screencast_min_bitrate; |
| 1067 | audio_options_.combined_audio_video_bwe = |
| 1068 | configuration.combined_audio_video_bwe; |
| 1069 | |
| 1070 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1071 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1072 | |
| 1073 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1074 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1075 | |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 1076 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1077 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1078 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 1079 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1080 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1081 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1082 | // Whether the certificate generator/certificate is null or not determines |
| 1083 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1084 | // the right instructions by clearing the variables if needed. |
| 1085 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1086 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1087 | certificate = nullptr; |
| 1088 | } else if (certificate) { |
| 1089 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1090 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1094 | signaling_thread(), channel_manager(), this, session_id(), |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1095 | std::move(dependencies.cert_generator), certificate)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1096 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1097 | this, &PeerConnection::OnCertificateReady); |
| 1098 | |
| 1099 | if (options.disable_encryption) { |
| 1100 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1101 | } |
| 1102 | |
| 1103 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1104 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1105 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1106 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1107 | // Add default audio/video transceivers for Plan B SDP. |
| 1108 | if (!IsUnifiedPlan()) { |
| 1109 | transceivers_.push_back( |
| 1110 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1111 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1112 | transceivers_.push_back( |
| 1113 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1114 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1115 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1116 | int delay_ms = |
| 1117 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1118 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1119 | MSG_REPORT_USAGE_PATTERN, nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1120 | return true; |
| 1121 | } |
| 1122 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1123 | RTCError PeerConnection::ValidateConfiguration( |
| 1124 | const RTCConfiguration& config) const { |
| 1125 | if (config.ice_regather_interval_range && |
| 1126 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1127 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1128 | "ice_regather_interval_range specified but continual " |
| 1129 | "gathering policy is GATHER_ONCE"); |
| 1130 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1131 | auto result = |
| 1132 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1133 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1136 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1137 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1138 | "Plan SdpSemantics. Please use GetSenders " |
| 1139 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1140 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1143 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1144 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1145 | "Plan SdpSemantics. Please use GetReceivers " |
| 1146 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1147 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1150 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1151 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1152 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1153 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1154 | if (IsClosed()) { |
| 1155 | return false; |
| 1156 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1157 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1158 | return false; |
| 1159 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1160 | |
| 1161 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1162 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1163 | observer->SignalAudioTrackAdded.connect(this, |
| 1164 | &PeerConnection::OnAudioTrackAdded); |
| 1165 | observer->SignalAudioTrackRemoved.connect( |
| 1166 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1167 | observer->SignalVideoTrackAdded.connect(this, |
| 1168 | &PeerConnection::OnVideoTrackAdded); |
| 1169 | observer->SignalVideoTrackRemoved.connect( |
| 1170 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1171 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1172 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1173 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1174 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1175 | } |
| 1176 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1177 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1180 | stats_->AddStream(local_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1181 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1182 | return true; |
| 1183 | } |
| 1184 | |
| 1185 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1186 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1187 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1188 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1189 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1190 | if (!IsClosed()) { |
| 1191 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1192 | RemoveAudioTrack(track.get(), local_stream); |
| 1193 | } |
| 1194 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1195 | RemoveVideoTrack(track.get(), local_stream); |
| 1196 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1197 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1198 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1199 | stream_observers_.erase( |
| 1200 | std::remove_if( |
| 1201 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1202 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1203 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1204 | }), |
| 1205 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1206 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1207 | if (IsClosed()) { |
| 1208 | return; |
| 1209 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1210 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1213 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1214 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1215 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1216 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1217 | if (!track) { |
| 1218 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1219 | } |
| 1220 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1221 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1222 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1223 | "Track has invalid kind: " + track->kind()); |
| 1224 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1225 | if (IsClosed()) { |
| 1226 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1227 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1228 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1229 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1230 | LOG_AND_RETURN_ERROR( |
| 1231 | RTCErrorType::INVALID_PARAMETER, |
| 1232 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1233 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1234 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1235 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1236 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1237 | if (sender_or_error.ok()) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1238 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1239 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1240 | } |
| 1241 | return sender_or_error; |
| 1242 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1243 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1244 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1245 | PeerConnection::AddTrackPlanB( |
| 1246 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1247 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1248 | if (stream_ids.size() > 1u) { |
| 1249 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1250 | "AddTrack with more than one stream is not " |
| 1251 | "supported with Plan B semantics."); |
| 1252 | } |
| 1253 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1254 | if (adjusted_stream_ids.empty()) { |
| 1255 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1256 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1257 | cricket::MediaType media_type = |
| 1258 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1259 | ? cricket::MEDIA_TYPE_AUDIO |
| 1260 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1261 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1262 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1263 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1264 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1265 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1266 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1267 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1268 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1269 | if (sender_info) { |
| 1270 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1271 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1272 | } else { |
| 1273 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1274 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1275 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1276 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1277 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1278 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1279 | if (sender_info) { |
| 1280 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1281 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1282 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1283 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1284 | } |
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 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1287 | PeerConnection::AddTrackUnifiedPlan( |
| 1288 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1289 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1290 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1291 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1292 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1293 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1294 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1295 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1296 | transceiver->internal()->set_direction( |
| 1297 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1298 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1299 | transceiver->internal()->set_direction( |
| 1300 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1301 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1302 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1303 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1304 | } else { |
| 1305 | cricket::MediaType media_type = |
| 1306 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1307 | ? cricket::MEDIA_TYPE_AUDIO |
| 1308 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1309 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1310 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1311 | std::string sender_id = track->id(); |
| 1312 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1313 | // This can happen if this is the second time AddTrack has created a sender |
| 1314 | // for this track. |
| 1315 | if (FindSenderById(sender_id)) { |
| 1316 | sender_id = rtc::CreateRandomUuid(); |
| 1317 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1318 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1319 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1320 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1321 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1322 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1323 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1324 | return transceiver->sender(); |
| 1325 | } |
| 1326 | |
| 1327 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1328 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1329 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1330 | RTC_DCHECK(track); |
| 1331 | for (auto transceiver : transceivers_) { |
| 1332 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1333 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1334 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1335 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1336 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1337 | return transceiver; |
| 1338 | } |
| 1339 | } |
| 1340 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1344 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1345 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1348 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1349 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1350 | if (!sender) { |
| 1351 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1352 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1353 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1354 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1355 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1356 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1357 | if (IsUnifiedPlan()) { |
| 1358 | auto transceiver = FindTransceiverBySender(sender); |
| 1359 | if (!transceiver || !sender->track()) { |
| 1360 | return RTCError::OK(); |
| 1361 | } |
| 1362 | sender->SetTrack(nullptr); |
| 1363 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1364 | transceiver->internal()->set_direction( |
| 1365 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1366 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1367 | transceiver->internal()->set_direction( |
| 1368 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1369 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1370 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1371 | bool removed; |
| 1372 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1373 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1374 | } else { |
| 1375 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1376 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1377 | } |
| 1378 | if (!removed) { |
| 1379 | LOG_AND_RETURN_ERROR( |
| 1380 | RTCErrorType::INVALID_PARAMETER, |
| 1381 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1382 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1383 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1384 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1385 | return RTCError::OK(); |
| 1386 | } |
| 1387 | |
| 1388 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1389 | PeerConnection::FindTransceiverBySender( |
| 1390 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1391 | for (auto transceiver : transceivers_) { |
| 1392 | if (transceiver->sender() == sender) { |
| 1393 | return transceiver; |
| 1394 | } |
| 1395 | } |
| 1396 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1399 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1400 | PeerConnection::AddTransceiver( |
| 1401 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1402 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1403 | } |
| 1404 | |
| 1405 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1406 | PeerConnection::AddTransceiver( |
| 1407 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1408 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1409 | RTC_CHECK(IsUnifiedPlan()) |
| 1410 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1411 | if (!track) { |
| 1412 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1413 | } |
| 1414 | cricket::MediaType media_type; |
| 1415 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1416 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1417 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1418 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1419 | } else { |
| 1420 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1421 | "Track kind is not audio or video"); |
| 1422 | } |
| 1423 | return AddTransceiver(media_type, track, init); |
| 1424 | } |
| 1425 | |
| 1426 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1427 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1428 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1429 | } |
| 1430 | |
| 1431 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1432 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1433 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1434 | RTC_CHECK(IsUnifiedPlan()) |
| 1435 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1436 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1437 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1438 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1439 | "media type is not audio or video"); |
| 1440 | } |
| 1441 | return AddTransceiver(media_type, nullptr, init); |
| 1442 | } |
| 1443 | |
| 1444 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1445 | PeerConnection::AddTransceiver( |
| 1446 | cricket::MediaType media_type, |
| 1447 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1448 | const RtpTransceiverInit& init, |
| 1449 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1450 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1451 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1452 | if (track) { |
| 1453 | RTC_DCHECK_EQ(media_type, |
| 1454 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1455 | ? cricket::MEDIA_TYPE_AUDIO |
| 1456 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1457 | } |
| 1458 | |
| 1459 | // TODO(bugs.webrtc.org/7600): Verify init. |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1460 | if (init.send_encodings.size() > 1) { |
| 1461 | LOG_AND_RETURN_ERROR( |
| 1462 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1463 | "Attempted to create an encoder with more than 1 encoding parameter."); |
| 1464 | } |
| 1465 | |
| 1466 | for (const auto& encoding : init.send_encodings) { |
| 1467 | if (encoding.ssrc.has_value()) { |
| 1468 | LOG_AND_RETURN_ERROR( |
| 1469 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1470 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | RtpParameters parameters; |
| 1475 | parameters.encodings = init.send_encodings; |
| 1476 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1477 | LOG_AND_RETURN_ERROR( |
| 1478 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1479 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1480 | } |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1481 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1482 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1483 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1484 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1485 | // that sender ID is already in use. |
| 1486 | std::string sender_id = |
| 1487 | (track && !FindSenderById(track->id()) ? track->id() |
| 1488 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1489 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
| 1490 | init.send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1491 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1492 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1493 | transceiver->internal()->set_direction(init.direction); |
| 1494 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1495 | if (fire_callback) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1496 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1497 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1498 | |
| 1499 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1500 | } |
| 1501 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1502 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1503 | PeerConnection::CreateSender( |
| 1504 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1505 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1506 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1507 | const std::vector<std::string>& stream_ids, |
| 1508 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1509 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1510 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1511 | RTC_DCHECK(!track || |
| 1512 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1513 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1514 | signaling_thread(), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1515 | new AudioRtpSender(worker_thread(), id, stats_.get())); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1516 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1517 | } else { |
| 1518 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1519 | RTC_DCHECK(!track || |
| 1520 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1521 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1522 | signaling_thread(), new VideoRtpSender(worker_thread(), id)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1523 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1524 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1525 | bool set_track_succeeded = sender->SetTrack(track); |
| 1526 | RTC_DCHECK(set_track_succeeded); |
| 1527 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1528 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1529 | return sender; |
| 1530 | } |
| 1531 | |
| 1532 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1533 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1534 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1535 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1536 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1537 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1538 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1539 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1540 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1541 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1542 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1543 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1544 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1545 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1546 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1547 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1548 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1549 | return receiver; |
| 1550 | } |
| 1551 | |
| 1552 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1553 | PeerConnection::CreateAndAddTransceiver( |
| 1554 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1555 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1556 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1557 | // Ensure that the new sender does not have an ID that is already in use by |
| 1558 | // another sender. |
| 1559 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1560 | // could be invalid, but should not cause a crash). |
| 1561 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1562 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1563 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1564 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1565 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1566 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1567 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1568 | } |
| 1569 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1570 | void PeerConnection::OnNegotiationNeeded() { |
| 1571 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1572 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1573 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1574 | } |
| 1575 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1576 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1577 | const std::string& kind, |
| 1578 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1579 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1580 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1581 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1582 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1583 | if (IsClosed()) { |
| 1584 | return nullptr; |
| 1585 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1586 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1587 | // Internally we need to have one stream with Plan B semantics, so we |
| 1588 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1589 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1590 | if (stream_id.empty()) { |
| 1591 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1592 | RTC_LOG(LS_INFO) |
| 1593 | << "No stream_id specified for sender. Generated stream ID: " |
| 1594 | << stream_ids[0]; |
| 1595 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1596 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1597 | } |
| 1598 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1599 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1600 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1601 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1602 | auto* audio_sender = new AudioRtpSender( |
| 1603 | worker_thread(), rtc::CreateRandomUuid(), stats_.get()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1604 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1605 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1606 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1607 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1608 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1609 | auto* video_sender = |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1610 | new VideoRtpSender(worker_thread(), rtc::CreateRandomUuid()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1611 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1612 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1613 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1614 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1615 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1616 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1617 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1618 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1619 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1620 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1621 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1622 | } |
| 1623 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1624 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1625 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1626 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 1627 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1628 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1629 | } |
| 1630 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1633 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1634 | PeerConnection::GetSendersInternal() const { |
| 1635 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1636 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 1637 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1638 | auto senders = transceiver->internal()->senders(); |
| 1639 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1640 | } |
| 1641 | return all_senders; |
| 1642 | } |
| 1643 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1644 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1645 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1646 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1647 | for (const auto& receiver : GetReceiversInternal()) { |
| 1648 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1649 | } |
| 1650 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1653 | std::vector< |
| 1654 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1655 | PeerConnection::GetReceiversInternal() const { |
| 1656 | std::vector< |
| 1657 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1658 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 1659 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1660 | auto receivers = transceiver->internal()->receivers(); |
| 1661 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1662 | receivers.end()); |
| 1663 | } |
| 1664 | return all_receivers; |
| 1665 | } |
| 1666 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1667 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1668 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1669 | RTC_CHECK(IsUnifiedPlan()) |
| 1670 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1671 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 1672 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1673 | all_transceivers.push_back(transceiver); |
| 1674 | } |
| 1675 | return all_transceivers; |
| 1676 | } |
| 1677 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1678 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1679 | MediaStreamTrackInterface* track, |
| 1680 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1681 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1682 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1683 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1684 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1685 | return false; |
| 1686 | } |
| 1687 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1688 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1689 | // The StatsCollector is used to tell if a track is valid because it may |
| 1690 | // remember tracks that the PeerConnection previously removed. |
| 1691 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1692 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1693 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1694 | return false; |
| 1695 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1696 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1697 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1698 | return true; |
| 1699 | } |
| 1700 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1701 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1702 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1703 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1704 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1705 | stats_collector_->GetStatsReport(callback); |
| 1706 | } |
| 1707 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1708 | void PeerConnection::GetStats( |
| 1709 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1710 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1711 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1712 | RTC_DCHECK(callback); |
| 1713 | RTC_DCHECK(stats_collector_); |
| 1714 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1715 | if (selector) { |
| 1716 | for (const auto& proxy_transceiver : transceivers_) { |
| 1717 | for (const auto& proxy_sender : |
| 1718 | proxy_transceiver->internal()->senders()) { |
| 1719 | if (proxy_sender == selector) { |
| 1720 | internal_sender = proxy_sender->internal(); |
| 1721 | break; |
| 1722 | } |
| 1723 | } |
| 1724 | if (internal_sender) |
| 1725 | break; |
| 1726 | } |
| 1727 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1728 | // If there is no |internal_sender| then |selector| is either null or does not |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1729 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1730 | // PeerConnection). This means that "all the stats objects representing the |
| 1731 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1732 | // produces an empty stats report. |
| 1733 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1734 | } |
| 1735 | |
| 1736 | void PeerConnection::GetStats( |
| 1737 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1738 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1739 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1740 | RTC_DCHECK(callback); |
| 1741 | RTC_DCHECK(stats_collector_); |
| 1742 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1743 | if (selector) { |
| 1744 | for (const auto& proxy_transceiver : transceivers_) { |
| 1745 | for (const auto& proxy_receiver : |
| 1746 | proxy_transceiver->internal()->receivers()) { |
| 1747 | if (proxy_receiver == selector) { |
| 1748 | internal_receiver = proxy_receiver->internal(); |
| 1749 | break; |
| 1750 | } |
| 1751 | } |
| 1752 | if (internal_receiver) |
| 1753 | break; |
| 1754 | } |
| 1755 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1756 | // If there is no |internal_receiver| then |selector| is either null or does |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1757 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1758 | // the PeerConnection). This means that "all the stats objects representing |
| 1759 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1760 | // selector produces an empty stats report. |
| 1761 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1762 | } |
| 1763 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1764 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1765 | return signaling_state_; |
| 1766 | } |
| 1767 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1768 | PeerConnectionInterface::IceConnectionState |
| 1769 | PeerConnection::ice_connection_state() { |
| 1770 | return ice_connection_state_; |
| 1771 | } |
| 1772 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1773 | PeerConnectionInterface::IceConnectionState |
| 1774 | PeerConnection::standardized_ice_connection_state() { |
| 1775 | return standardized_ice_connection_state_; |
| 1776 | } |
| 1777 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1778 | PeerConnectionInterface::PeerConnectionState |
| 1779 | PeerConnection::peer_connection_state() { |
| 1780 | return connection_state_; |
| 1781 | } |
| 1782 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1783 | PeerConnectionInterface::IceGatheringState |
| 1784 | PeerConnection::ice_gathering_state() { |
| 1785 | return ice_gathering_state_; |
| 1786 | } |
| 1787 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1788 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1789 | const std::string& label, |
| 1790 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1791 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1792 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1793 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1794 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1795 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1796 | if (config) { |
| 1797 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1798 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1799 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1800 | InternalCreateDataChannel(label, internal_config.get())); |
| 1801 | if (!channel.get()) { |
| 1802 | return nullptr; |
| 1803 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1804 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1805 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1806 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1807 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1808 | Observer()->OnRenegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1809 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1810 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1811 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1812 | } |
| 1813 | |
| 1814 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1815 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1816 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1817 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1818 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1819 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1820 | return; |
| 1821 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1822 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1823 | if (IsClosed()) { |
| 1824 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1825 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1826 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1827 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1828 | return; |
| 1829 | } |
| 1830 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1831 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1832 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1833 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1834 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1835 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1836 | return; |
| 1837 | } |
| 1838 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1839 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1840 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1841 | if (IsUnifiedPlan()) { |
| 1842 | RTCError error = HandleLegacyOfferOptions(options); |
| 1843 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1844 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1845 | return; |
| 1846 | } |
| 1847 | } |
| 1848 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1849 | cricket::MediaSessionOptions session_options; |
| 1850 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1851 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1854 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1855 | const RTCOfferAnswerOptions& options) { |
| 1856 | RTC_DCHECK(IsUnifiedPlan()); |
| 1857 | |
| 1858 | if (options.offer_to_receive_audio == 0) { |
| 1859 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1860 | cricket::MEDIA_TYPE_AUDIO); |
| 1861 | } else if (options.offer_to_receive_audio == 1) { |
| 1862 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1863 | } else if (options.offer_to_receive_audio > 1) { |
| 1864 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1865 | "offer_to_receive_audio > 1 is not supported."); |
| 1866 | } |
| 1867 | |
| 1868 | if (options.offer_to_receive_video == 0) { |
| 1869 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1870 | cricket::MEDIA_TYPE_VIDEO); |
| 1871 | } else if (options.offer_to_receive_video == 1) { |
| 1872 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1873 | } else if (options.offer_to_receive_video > 1) { |
| 1874 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1875 | "offer_to_receive_video > 1 is not supported."); |
| 1876 | } |
| 1877 | |
| 1878 | return RTCError::OK(); |
| 1879 | } |
| 1880 | |
| 1881 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1882 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 1883 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1884 | RtpTransceiverDirection new_direction = |
| 1885 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 1886 | if (new_direction != transceiver->direction()) { |
| 1887 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 1888 | << " transceiver (MID=" |
| 1889 | << transceiver->mid().value_or("<not set>") << ") from " |
| 1890 | << RtpTransceiverDirectionToString( |
| 1891 | transceiver->direction()) |
| 1892 | << " to " |
| 1893 | << RtpTransceiverDirectionToString(new_direction) |
| 1894 | << " since CreateOffer specified offer_to_receive=0"; |
| 1895 | transceiver->internal()->set_direction(new_direction); |
| 1896 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1901 | cricket::MediaType media_type) { |
| 1902 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1903 | RTC_LOG(LS_INFO) |
| 1904 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 1905 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1906 | RtpTransceiverInit init; |
| 1907 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1908 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1913 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1914 | std::vector< |
| 1915 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1916 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 1917 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1918 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1919 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1920 | receiving_transceivers.push_back(transceiver); |
| 1921 | } |
| 1922 | } |
| 1923 | return receiving_transceivers; |
| 1924 | } |
| 1925 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1926 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1927 | const RTCOfferAnswerOptions& options) { |
| 1928 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1929 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1930 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1931 | return; |
| 1932 | } |
| 1933 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1934 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1935 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1936 | std::string error = |
| 1937 | "PeerConnection cannot create an answer in a state other than " |
| 1938 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1939 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1940 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1941 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1942 | return; |
| 1943 | } |
| 1944 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1945 | // The remote description should be set if we're in the right state. |
| 1946 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1947 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1948 | if (IsUnifiedPlan()) { |
| 1949 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1950 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1951 | "supported with Unified Plan semantics. Use the " |
| 1952 | "RtpTransceiver API instead."; |
| 1953 | } |
| 1954 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1955 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1956 | "supported with Unified Plan semantics. Use the " |
| 1957 | "RtpTransceiver API instead."; |
| 1958 | } |
| 1959 | } |
| 1960 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1961 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1962 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1963 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1964 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | void PeerConnection::SetLocalDescription( |
| 1968 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1969 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1970 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1971 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1972 | // The SetLocalDescription contract is that we take ownership of the session |
| 1973 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1974 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1975 | // description as a unique_ptr argument to formalize this agreement. |
| 1976 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1977 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1978 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1979 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1982 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1983 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1984 | PostSetSessionDescriptionFailure( |
| 1985 | observer, |
| 1986 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1987 | return; |
| 1988 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1989 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1990 | // If a session error has occurred the PeerConnection is in a possibly |
| 1991 | // inconsistent state so fail right away. |
| 1992 | if (session_error() != SessionError::kNone) { |
| 1993 | std::string error_message = GetSessionErrorMsg(); |
| 1994 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1995 | PostSetSessionDescriptionFailure( |
| 1996 | observer, |
| 1997 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1998 | return; |
| 1999 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2000 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2001 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2002 | if (!error.ok()) { |
| 2003 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2004 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2005 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2006 | PostSetSessionDescriptionFailure( |
| 2007 | observer, |
| 2008 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2009 | return; |
| 2010 | } |
| 2011 | |
| 2012 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2013 | // which may destroy it before returning. |
| 2014 | const SdpType type = desc->GetType(); |
| 2015 | |
| 2016 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2017 | // |desc| may be destroyed at this point. |
| 2018 | |
| 2019 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2020 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2021 | // inconsistent state, so act conservatively here and set the session error |
| 2022 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2023 | SetSessionError(SessionError::kContent, error.message()); |
| 2024 | std::string error_message = |
| 2025 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2026 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2027 | PostSetSessionDescriptionFailure( |
| 2028 | observer, |
| 2029 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2030 | return; |
| 2031 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2032 | RTC_DCHECK(local_description()); |
| 2033 | |
| 2034 | PostSetSessionDescriptionSuccess(observer); |
| 2035 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 2036 | // MaybeStartGathering needs to be called after posting |
| 2037 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2038 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2039 | transport_controller_->MaybeStartGathering(); |
| 2040 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2041 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2042 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2043 | // MaybeStartGathering... |
| 2044 | network_thread()->Invoke<void>( |
| 2045 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2046 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2047 | // Make UMA notes about what was agreed to. |
| 2048 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2049 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2050 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | RTCError PeerConnection::ApplyLocalDescription( |
| 2054 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2055 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2056 | RTC_DCHECK(desc); |
| 2057 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2058 | // Update stats here so that we have the most recent stats for tracks and |
| 2059 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2060 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2061 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2062 | // Take a reference to the old local description since it's used below to |
| 2063 | // compare against the new local description. When setting the new local |
| 2064 | // description, grab ownership of the replaced session description in case it |
| 2065 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2066 | // of the method. |
| 2067 | const SessionDescriptionInterface* old_local_description = |
| 2068 | local_description(); |
| 2069 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2070 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2071 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2072 | replaced_local_description = pending_local_description_ |
| 2073 | ? std::move(pending_local_description_) |
| 2074 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2075 | current_local_description_ = std::move(desc); |
| 2076 | pending_local_description_ = nullptr; |
| 2077 | current_remote_description_ = std::move(pending_remote_description_); |
| 2078 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2079 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2080 | pending_local_description_ = std::move(desc); |
| 2081 | } |
| 2082 | // The session description to apply now must be accessed by |
| 2083 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2084 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2085 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2086 | if (!is_caller_) { |
| 2087 | if (remote_description()) { |
| 2088 | // Remote description was applied first, so this PC is the callee. |
| 2089 | is_caller_ = false; |
| 2090 | } else { |
| 2091 | // Local description is applied first, so this PC is the caller. |
| 2092 | is_caller_ = true; |
| 2093 | } |
| 2094 | } |
| 2095 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2096 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2097 | if (!error.ok()) { |
| 2098 | return error; |
| 2099 | } |
| 2100 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2101 | if (IsUnifiedPlan()) { |
| 2102 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2103 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2104 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2105 | if (!error.ok()) { |
| 2106 | return error; |
| 2107 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2108 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2109 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2110 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2111 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2112 | // Note that code paths that don't set MID won't be able to use |
| 2113 | // information about DTLS transports. |
| 2114 | if (transceiver->mid()) { |
| 2115 | auto dtls_transport = |
| 2116 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2117 | transceiver->internal()->sender_internal()->set_transport( |
| 2118 | dtls_transport); |
| 2119 | transceiver->internal()->receiver_internal()->set_transport( |
| 2120 | dtls_transport); |
| 2121 | } |
| 2122 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2123 | const ContentInfo* content = |
| 2124 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2125 | if (!content) { |
| 2126 | continue; |
| 2127 | } |
| 2128 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2129 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2130 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2131 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2132 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2133 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2134 | // "recvonly", process the removal of a remote track for the media |
| 2135 | // description, given transceiver, removeList, and muteTracks. |
| 2136 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2137 | (transceiver->internal()->fired_direction() && |
| 2138 | RtpTransceiverDirectionHasRecv( |
| 2139 | *transceiver->internal()->fired_direction()))) { |
| 2140 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2141 | &removed_streams); |
| 2142 | } |
| 2143 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2144 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2145 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2146 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2147 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2148 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2149 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2150 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2151 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2152 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2153 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2154 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2155 | } |
| 2156 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2157 | // Media channels will be created only when offer is set. These may use new |
| 2158 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2159 | if (type == SdpType::kOffer) { |
| 2160 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2161 | // description is applied. Restore back to old description. |
| 2162 | RTCError error = CreateChannels(*local_description()->description()); |
| 2163 | if (!error.ok()) { |
| 2164 | return error; |
| 2165 | } |
| 2166 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2167 | // Remove unused channels if MediaContentDescription is rejected. |
| 2168 | RemoveUnusedChannels(local_description()->description()); |
| 2169 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2170 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2171 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2172 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2173 | if (!error.ok()) { |
| 2174 | return error; |
| 2175 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2176 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2177 | if (remote_description()) { |
| 2178 | // Now that we have a local description, we can push down remote candidates. |
| 2179 | UseCandidatesInSessionDescription(remote_description()); |
| 2180 | } |
| 2181 | |
| 2182 | pending_ice_restarts_.clear(); |
| 2183 | if (session_error() != SessionError::kNone) { |
| 2184 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2185 | } |
| 2186 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2187 | // If setting the description decided our SSL role, allocate any necessary |
| 2188 | // SCTP sids. |
| 2189 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2190 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2191 | AllocateSctpSids(role); |
| 2192 | } |
| 2193 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2194 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2195 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2196 | const ContentInfo* content = |
| 2197 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2198 | if (!content) { |
| 2199 | continue; |
| 2200 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2201 | const auto& streams = content->media_description()->streams(); |
| 2202 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2203 | transceiver->internal()->sender_internal()->set_stream_ids( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2204 | streams[0].stream_ids()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2205 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2206 | streams[0].first_ssrc()); |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2207 | } else { |
| 2208 | // 0 is a special value meaning "this sender has no associated send |
| 2209 | // stream". Need to call this so the sender won't attempt to configure |
| 2210 | // a no longer existing stream and run into DCHECKs in the lower |
| 2211 | // layers. |
| 2212 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | } else { |
| 2216 | // Plan B semantics. |
| 2217 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2218 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2219 | // local session description. |
| 2220 | const cricket::ContentInfo* audio_content = |
| 2221 | GetFirstAudioContent(local_description()->description()); |
| 2222 | if (audio_content) { |
| 2223 | if (audio_content->rejected) { |
| 2224 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2225 | } else { |
| 2226 | const cricket::AudioContentDescription* audio_desc = |
| 2227 | audio_content->media_description()->as_audio(); |
| 2228 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2229 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2230 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2231 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2232 | const cricket::ContentInfo* video_content = |
| 2233 | GetFirstVideoContent(local_description()->description()); |
| 2234 | if (video_content) { |
| 2235 | if (video_content->rejected) { |
| 2236 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2237 | } else { |
| 2238 | const cricket::VideoContentDescription* video_desc = |
| 2239 | video_content->media_description()->as_video(); |
| 2240 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2241 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2242 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2246 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2247 | if (data_content) { |
| 2248 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2249 | data_content->media_description()->as_data(); |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 2250 | if (absl::StartsWith(data_desc->protocol(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2251 | cricket::kMediaProtocolRtpPrefix)) { |
| 2252 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2253 | } |
| 2254 | } |
| 2255 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2256 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2259 | // The SDP parser used to populate these values by default for the 'content |
| 2260 | // name' if an a=mid line was absent. |
| 2261 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2262 | switch (media_type) { |
| 2263 | case cricket::MEDIA_TYPE_AUDIO: |
| 2264 | return cricket::CN_AUDIO; |
| 2265 | case cricket::MEDIA_TYPE_VIDEO: |
| 2266 | return cricket::CN_VIDEO; |
| 2267 | case cricket::MEDIA_TYPE_DATA: |
| 2268 | return cricket::CN_DATA; |
| 2269 | } |
| 2270 | RTC_NOTREACHED(); |
| 2271 | return ""; |
| 2272 | } |
| 2273 | |
| 2274 | void PeerConnection::FillInMissingRemoteMids( |
| 2275 | cricket::SessionDescription* remote_description) { |
| 2276 | RTC_DCHECK(remote_description); |
| 2277 | const cricket::ContentInfos& local_contents = |
| 2278 | (local_description() ? local_description()->description()->contents() |
| 2279 | : cricket::ContentInfos()); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2280 | for (size_t i = 0; i < remote_description->contents().size(); ++i) { |
| 2281 | cricket::ContentInfo& content = remote_description->contents()[i]; |
| 2282 | if (!content.name.empty()) { |
| 2283 | continue; |
| 2284 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2285 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2286 | absl::string_view source_explanation; |
| 2287 | if (IsUnifiedPlan()) { |
| 2288 | if (i < local_contents.size()) { |
| 2289 | new_mid = local_contents[i].name; |
| 2290 | source_explanation = "from the matching local media section"; |
| 2291 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2292 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2293 | source_explanation = "generated just now"; |
| 2294 | } |
| 2295 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2296 | new_mid = std::string( |
| 2297 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2298 | source_explanation = "to match pre-existing behavior"; |
| 2299 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2300 | content.name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2301 | remote_description->transport_infos()[i].content_name = |
| 2302 | std::string(new_mid); |
| 2303 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2304 | << " is missing an a=mid line. Filling in the value '" |
| 2305 | << new_mid << "' " << source_explanation << "."; |
| 2306 | } |
| 2307 | } |
| 2308 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2309 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2310 | SetSessionDescriptionObserver* observer, |
| 2311 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2312 | SetRemoteDescription( |
| 2313 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2314 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2315 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2316 | } |
| 2317 | |
| 2318 | void PeerConnection::SetRemoteDescription( |
| 2319 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2320 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2321 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2322 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2323 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2324 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2325 | return; |
| 2326 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2327 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2328 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2329 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2330 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2331 | return; |
| 2332 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2333 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2334 | // If a session error has occurred the PeerConnection is in a possibly |
| 2335 | // inconsistent state so fail right away. |
| 2336 | if (session_error() != SessionError::kNone) { |
| 2337 | std::string error_message = GetSessionErrorMsg(); |
| 2338 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2339 | observer->OnSetRemoteDescriptionComplete( |
| 2340 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2341 | return; |
| 2342 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2343 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2344 | if (desc->GetType() == SdpType::kOffer) { |
| 2345 | // Report to UMA the format of the received offer. |
| 2346 | ReportSdpFormatReceived(*desc); |
| 2347 | } |
| 2348 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2349 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 2350 | // points. |
| 2351 | FillInMissingRemoteMids(desc->description()); |
| 2352 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2353 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2354 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2355 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2356 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2357 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2358 | observer->OnSetRemoteDescriptionComplete( |
| 2359 | RTCError(error.type(), std::move(error_message))); |
| 2360 | return; |
| 2361 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2362 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2363 | // Grab the description type before moving ownership to |
| 2364 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2365 | const SdpType type = desc->GetType(); |
| 2366 | |
| 2367 | error = ApplyRemoteDescription(std::move(desc)); |
| 2368 | // |desc| may be destroyed at this point. |
| 2369 | |
| 2370 | if (!error.ok()) { |
| 2371 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2372 | // inconsistent state, so act conservatively here and set the session error |
| 2373 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2374 | SetSessionError(SessionError::kContent, error.message()); |
| 2375 | std::string error_message = |
| 2376 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2377 | RTC_LOG(LS_ERROR) << error_message; |
| 2378 | observer->OnSetRemoteDescriptionComplete( |
| 2379 | RTCError(error.type(), std::move(error_message))); |
| 2380 | return; |
| 2381 | } |
| 2382 | RTC_DCHECK(remote_description()); |
| 2383 | |
| 2384 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2385 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2386 | // MaybeStartGathering... |
| 2387 | network_thread()->Invoke<void>( |
| 2388 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2389 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2390 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2391 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2395 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2399 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2400 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2401 | RTC_DCHECK(desc); |
| 2402 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2403 | // Update stats here so that we have the most recent stats for tracks and |
| 2404 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2405 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2406 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2407 | // Take a reference to the old remote description since it's used below to |
| 2408 | // compare against the new remote description. When setting the new remote |
| 2409 | // description, grab ownership of the replaced session description in case it |
| 2410 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2411 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2412 | const SessionDescriptionInterface* old_remote_description = |
| 2413 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2414 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2415 | SdpType type = desc->GetType(); |
| 2416 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2417 | replaced_remote_description = pending_remote_description_ |
| 2418 | ? std::move(pending_remote_description_) |
| 2419 | : std::move(current_remote_description_); |
| 2420 | current_remote_description_ = std::move(desc); |
| 2421 | pending_remote_description_ = nullptr; |
| 2422 | current_local_description_ = std::move(pending_local_description_); |
| 2423 | } else { |
| 2424 | replaced_remote_description = std::move(pending_remote_description_); |
| 2425 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2426 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2427 | // The session description to apply now must be accessed by |
| 2428 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2429 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2430 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2431 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2432 | if (!error.ok()) { |
| 2433 | return error; |
| 2434 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2435 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2436 | if (IsUnifiedPlan()) { |
| 2437 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2438 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2439 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2440 | if (!error.ok()) { |
| 2441 | return error; |
| 2442 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2443 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2444 | // Media channels will be created only when offer is set. These may use new |
| 2445 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2446 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2447 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2448 | // description is applied. Restore back to old description. |
| 2449 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2450 | if (!error.ok()) { |
| 2451 | return error; |
| 2452 | } |
| 2453 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2454 | // Remove unused channels if MediaContentDescription is rejected. |
| 2455 | RemoveUnusedChannels(remote_description()->description()); |
| 2456 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2457 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2458 | // NOTE: Candidates allocation will be initiated only when |
| 2459 | // SetLocalDescription is called. |
| 2460 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2461 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2462 | if (!error.ok()) { |
| 2463 | return error; |
| 2464 | } |
| 2465 | |
| 2466 | if (local_description() && |
| 2467 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2468 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2469 | } |
| 2470 | |
| 2471 | if (old_remote_description) { |
| 2472 | for (const cricket::ContentInfo& content : |
| 2473 | old_remote_description->description()->contents()) { |
| 2474 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2475 | // password that indicates the remote peer requests an ICE restart. |
| 2476 | // TODO(deadbeef): When we start storing both the current and pending |
| 2477 | // remote description, this should reset pending_ice_restarts and compare |
| 2478 | // against the current description. |
| 2479 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2480 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2481 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2482 | pending_ice_restarts_.insert(content.name); |
| 2483 | } |
| 2484 | } else { |
| 2485 | // We retain all received candidates only if ICE is not restarted. |
| 2486 | // When ICE is restarted, all previous candidates belong to an old |
| 2487 | // generation and should not be kept. |
| 2488 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2489 | // description should only contain candidates from the last set remote |
| 2490 | // description plus any candidates added since then. We should remove |
| 2491 | // this once we're sure it won't break anything. |
| 2492 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2493 | old_remote_description, content.name, mutable_remote_description()); |
| 2494 | } |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | if (session_error() != SessionError::kNone) { |
| 2499 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2500 | } |
| 2501 | |
| 2502 | // Set the the ICE connection state to connecting since the connection may |
| 2503 | // become writable with peer reflexive candidates before any remote candidate |
| 2504 | // is signaled. |
| 2505 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2506 | // is to have a new signal the indicates a change in checking state from the |
| 2507 | // transport and expose a new checking() member from transport that can be |
| 2508 | // read to determine the current checking state. The existing SignalConnecting |
| 2509 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2510 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2511 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2512 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2513 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2514 | } |
| 2515 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2516 | // If setting the description decided our SSL role, allocate any necessary |
| 2517 | // SCTP sids. |
| 2518 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2519 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2520 | AllocateSctpSids(role); |
| 2521 | } |
| 2522 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2523 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2524 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2525 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2526 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2527 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2528 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2529 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2530 | const ContentInfo* content = |
| 2531 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2532 | if (!content) { |
| 2533 | continue; |
| 2534 | } |
| 2535 | const MediaContentDescription* media_desc = content->media_description(); |
| 2536 | RtpTransceiverDirection local_direction = |
| 2537 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2538 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 2539 | // RTCSessionDescription: Set the associated remote streams given |
| 2540 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 2541 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 2542 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2543 | std::vector<std::string> stream_ids; |
| 2544 | if (!media_desc->streams().empty()) { |
| 2545 | // The remote description has signaled the stream IDs. |
| 2546 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2547 | } |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2548 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 2549 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 2550 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 2551 | stream_ids, &added_streams, |
| 2552 | &removed_streams); |
| 2553 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 2554 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 2555 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 2556 | // process the addition of a remote track for the media description. |
| 2557 | if (!transceiver->fired_direction() || |
| 2558 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 2559 | RTC_LOG(LS_INFO) |
| 2560 | << "Processing the addition of a remote track for MID=" |
| 2561 | << content->name << "."; |
| 2562 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 11:25:05 +0100 | [diff] [blame] | 2563 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2564 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2565 | // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2566 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2567 | // removal of a remote track for the media description, given transceiver, |
| 2568 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2569 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2570 | (transceiver->fired_direction() && |
| 2571 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2572 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2573 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2574 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2575 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2576 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2577 | // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2578 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2579 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2580 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2581 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2582 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2583 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 2584 | if (transceiver->mid()) { |
| 2585 | auto dtls_transport = |
| 2586 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2587 | transceiver->internal()->sender_internal()->set_transport( |
| 2588 | dtls_transport); |
| 2589 | transceiver->internal()->receiver_internal()->set_transport( |
| 2590 | dtls_transport); |
| 2591 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2592 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2593 | // 2.2.8.1.12: If the media description is rejected, and transceiver is |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2594 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2595 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2596 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2597 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2598 | transceiver->Stop(); |
| 2599 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2600 | if (!content->rejected && |
| 2601 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2602 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2603 | uint32_t ssrc = 0; |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2604 | if (!media_desc->streams().empty() && |
| 2605 | media_desc->streams()[0].has_ssrcs()) { |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2606 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2607 | } |
| 2608 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2609 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2610 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2611 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2612 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2613 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2614 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2615 | observer->OnTrack(transceiver); |
| 2616 | observer->OnAddTrack(transceiver->receiver(), |
| 2617 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2618 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2619 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2620 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2621 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2622 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2623 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2624 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2625 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2626 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2627 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2628 | } |
| 2629 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2630 | const cricket::ContentInfo* audio_content = |
| 2631 | GetFirstAudioContent(remote_description()->description()); |
| 2632 | const cricket::ContentInfo* video_content = |
| 2633 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2634 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2635 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2636 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2637 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2638 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2639 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2640 | |
| 2641 | // Check if the descriptions include streams, just in case the peer supports |
| 2642 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2643 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2644 | (audio_desc && !audio_desc->streams().empty()) || |
| 2645 | (video_desc && !video_desc->streams().empty())) { |
| 2646 | remote_peer_supports_msid_ = true; |
| 2647 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2648 | |
| 2649 | // We wait to signal new streams until we finish processing the description, |
| 2650 | // since only at that point will new streams have all their tracks. |
| 2651 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2652 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2653 | if (!IsUnifiedPlan()) { |
| 2654 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2655 | // rejected media section, there is some cleanup logic that expects the |
| 2656 | // voice/ video channel to still be set. But in this method the voice/video |
| 2657 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2658 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2659 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2660 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2661 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2662 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2663 | // and MediaStreams. |
| 2664 | if (audio_content) { |
| 2665 | if (audio_content->rejected) { |
| 2666 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2667 | } else { |
| 2668 | bool default_audio_track_needed = |
| 2669 | !remote_peer_supports_msid_ && |
| 2670 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2671 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2672 | default_audio_track_needed, audio_desc->type(), |
| 2673 | new_streams); |
| 2674 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2675 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2676 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2677 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2678 | // and MediaStreams. |
| 2679 | if (video_content) { |
| 2680 | if (video_content->rejected) { |
| 2681 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2682 | } else { |
| 2683 | bool default_video_track_needed = |
| 2684 | !remote_peer_supports_msid_ && |
| 2685 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2686 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2687 | default_video_track_needed, video_desc->type(), |
| 2688 | new_streams); |
| 2689 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2690 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2691 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2692 | // Update the DataChannels with the information from the remote peer. |
| 2693 | if (data_desc) { |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 2694 | if (absl::StartsWith(data_desc->protocol(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2695 | cricket::kMediaProtocolRtpPrefix)) { |
| 2696 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2697 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2698 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2699 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2700 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2701 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2702 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2703 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2704 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2705 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2706 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2707 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2708 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2709 | UpdateEndedRemoteMediaStreams(); |
| 2710 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2711 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2712 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2715 | void PeerConnection::SetAssociatedRemoteStreams( |
| 2716 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 2717 | const std::vector<std::string>& stream_ids, |
| 2718 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 2719 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2720 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2721 | for (const std::string& stream_id : stream_ids) { |
| 2722 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2723 | remote_streams_->find(stream_id); |
| 2724 | if (!stream) { |
| 2725 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2726 | MediaStream::Create(stream_id)); |
| 2727 | remote_streams_->AddStream(stream); |
| 2728 | added_streams->push_back(stream); |
| 2729 | } |
| 2730 | media_streams.push_back(stream); |
| 2731 | } |
| 2732 | // Special case: "a=msid" missing, use random stream ID. |
| 2733 | if (media_streams.empty() && |
| 2734 | !(remote_description()->description()->msid_signaling() & |
| 2735 | cricket::kMsidSignalingMediaSection)) { |
| 2736 | if (!missing_msid_default_stream_) { |
| 2737 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 2738 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 2739 | added_streams->push_back(missing_msid_default_stream_); |
| 2740 | } |
| 2741 | media_streams.push_back(missing_msid_default_stream_); |
| 2742 | } |
| 2743 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 2744 | receiver->streams(); |
| 2745 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 2746 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 2747 | // update the stream-track relationships in a later step. We do this earlier, |
| 2748 | // changing the order of things, but the end-result is the same. |
| 2749 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 2750 | // instead. https://crbug.com/webrtc/9480 |
| 2751 | receiver->SetStreams(media_streams); |
| 2752 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2753 | } |
| 2754 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2755 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 2756 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2757 | transceiver, |
| 2758 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 2759 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2760 | RTC_DCHECK(transceiver->mid()); |
| 2761 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2762 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2763 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2764 | transceiver->internal()->receiver_internal()->streams(); |
| 2765 | // This will remove the remote track from the streams. |
| 2766 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 2767 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2768 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2769 | } |
| 2770 | |
| 2771 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 2772 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 2773 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2774 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 2775 | // streams, see if the stream was removed by checking if this was the last |
| 2776 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 2777 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2778 | if (remote_stream->GetAudioTracks().empty() && |
| 2779 | remote_stream->GetVideoTracks().empty()) { |
| 2780 | remote_streams_->RemoveStream(remote_stream); |
| 2781 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2782 | } |
| 2783 | } |
| 2784 | } |
| 2785 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2786 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2787 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2788 | const SessionDescriptionInterface& new_session, |
| 2789 | const SessionDescriptionInterface* old_local_description, |
| 2790 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2791 | RTC_DCHECK(IsUnifiedPlan()); |
| 2792 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2793 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2794 | if (new_session.GetType() == SdpType::kOffer) { |
| 2795 | auto bundle_group_or_error = |
| 2796 | GetEarlyBundleGroup(*new_session.description()); |
| 2797 | if (!bundle_group_or_error.ok()) { |
| 2798 | return bundle_group_or_error.MoveError(); |
| 2799 | } |
| 2800 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2801 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2802 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2803 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2804 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2805 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2806 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2807 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2808 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2809 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2810 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2811 | if (old_local_description && |
| 2812 | i < old_local_description->description()->contents().size()) { |
| 2813 | old_local_content = |
| 2814 | &old_local_description->description()->contents()[i]; |
| 2815 | } |
| 2816 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2817 | if (old_remote_description && |
| 2818 | i < old_remote_description->description()->contents().size()) { |
| 2819 | old_remote_content = |
| 2820 | &old_remote_description->description()->contents()[i]; |
| 2821 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2822 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2823 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2824 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2825 | if (!transceiver_or_error.ok()) { |
| 2826 | return transceiver_or_error.MoveError(); |
| 2827 | } |
| 2828 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2829 | RTCError error = |
| 2830 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2831 | if (!error.ok()) { |
| 2832 | return error; |
| 2833 | } |
| 2834 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2835 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2836 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2837 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 2838 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2839 | continue; |
| 2840 | } |
| 2841 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2842 | if (!error.ok()) { |
| 2843 | return error; |
| 2844 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2845 | } else { |
| 2846 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2847 | "Unknown section type."); |
| 2848 | } |
| 2849 | } |
| 2850 | |
| 2851 | return RTCError::OK(); |
| 2852 | } |
| 2853 | |
| 2854 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2855 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2856 | transceiver, |
| 2857 | const cricket::ContentInfo& content, |
| 2858 | const cricket::ContentGroup* bundle_group) { |
| 2859 | RTC_DCHECK(IsUnifiedPlan()); |
| 2860 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 2861 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2862 | if (content.rejected) { |
| 2863 | if (channel) { |
| 2864 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 2865 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2866 | } |
| 2867 | } else { |
| 2868 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2869 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2870 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2871 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2872 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2873 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2874 | } |
| 2875 | if (!channel) { |
| 2876 | LOG_AND_RETURN_ERROR( |
| 2877 | RTCErrorType::INTERNAL_ERROR, |
| 2878 | "Failed to create channel for mid=" + content.name); |
| 2879 | } |
| 2880 | transceiver->internal()->SetChannel(channel); |
| 2881 | } |
| 2882 | } |
| 2883 | return RTCError::OK(); |
| 2884 | } |
| 2885 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2886 | RTCError PeerConnection::UpdateDataChannel( |
| 2887 | cricket::ContentSource source, |
| 2888 | const cricket::ContentInfo& content, |
| 2889 | const cricket::ContentGroup* bundle_group) { |
| 2890 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2891 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2892 | // will take care of rejecting it. |
| 2893 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2894 | } |
| 2895 | if (content.rejected) { |
| 2896 | DestroyDataChannel(); |
| 2897 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2898 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2899 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2900 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2901 | "Failed to create data channel."); |
| 2902 | } |
| 2903 | } |
| 2904 | if (source == cricket::CS_REMOTE) { |
| 2905 | const MediaContentDescription* data_desc = content.media_description(); |
| 2906 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2907 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2908 | } |
| 2909 | } |
| 2910 | } |
| 2911 | return RTCError::OK(); |
| 2912 | } |
| 2913 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2914 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2915 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2916 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2917 | size_t mline_index, |
| 2918 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2919 | const ContentInfo* old_local_content, |
| 2920 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2921 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2922 | // If this is an offer then the m= section might be recycled. If the m= |
| 2923 | // section is being recycled (defined as: rejected in the current local or |
| 2924 | // remote description and not rejected in new description), dissociate the |
| 2925 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2926 | // and discard the mapping between the transceiver and its m= section index. |
| 2927 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2928 | old_remote_content)) { |
| 2929 | // We want to dissociate the transceiver that has the rejected mid. |
| 2930 | const std::string& old_mid = |
| 2931 | (old_local_content && old_local_content->rejected) |
| 2932 | ? old_local_content->name |
| 2933 | : old_remote_content->name; |
| 2934 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2935 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2936 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 2937 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 2938 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 2939 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2940 | } |
| 2941 | } |
| 2942 | const MediaContentDescription* media_desc = content.media_description(); |
| 2943 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2944 | if (source == cricket::CS_LOCAL) { |
| 2945 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2946 | // mapping between transceivers and m= section indices established when |
| 2947 | // creating the offer. |
| 2948 | if (!transceiver) { |
| 2949 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2950 | } |
| 2951 | if (!transceiver) { |
| 2952 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2953 | "Unknown transceiver"); |
| 2954 | } |
| 2955 | } else { |
| 2956 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2957 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2958 | // of the same type... |
| 2959 | if (!transceiver && |
| 2960 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2961 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2962 | } |
| 2963 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2964 | // recvonly direction. |
| 2965 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2966 | RTC_LOG(LS_INFO) << "Adding " |
| 2967 | << cricket::MediaTypeToString(media_desc->type()) |
| 2968 | << " transceiver for MID=" << content.name |
| 2969 | << " at i=" << mline_index |
| 2970 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 2971 | std::string sender_id = rtc::CreateRandomUuid(); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 2972 | auto sender = |
| 2973 | CreateSender(media_desc->type(), sender_id, nullptr, {}, {}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2974 | std::string receiver_id; |
| 2975 | if (!media_desc->streams().empty()) { |
| 2976 | receiver_id = media_desc->streams()[0].id; |
| 2977 | } else { |
| 2978 | receiver_id = rtc::CreateRandomUuid(); |
| 2979 | } |
| 2980 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2981 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2982 | transceiver->internal()->set_direction( |
| 2983 | RtpTransceiverDirection::kRecvOnly); |
| 2984 | } |
| 2985 | } |
| 2986 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2987 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2988 | LOG_AND_RETURN_ERROR( |
| 2989 | RTCErrorType::INVALID_PARAMETER, |
| 2990 | "Transceiver type does not match media description type."); |
| 2991 | } |
| 2992 | // Associate the found or created RtpTransceiver with the m= section by |
| 2993 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2994 | // section, and establish a mapping between the transceiver and the index of |
| 2995 | // the m= section. |
| 2996 | transceiver->internal()->set_mid(content.name); |
| 2997 | transceiver->internal()->set_mline_index(mline_index); |
| 2998 | return std::move(transceiver); |
| 2999 | } |
| 3000 | |
| 3001 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3002 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3003 | RTC_DCHECK(IsUnifiedPlan()); |
| 3004 | for (auto transceiver : transceivers_) { |
| 3005 | if (transceiver->mid() == mid) { |
| 3006 | return transceiver; |
| 3007 | } |
| 3008 | } |
| 3009 | return nullptr; |
| 3010 | } |
| 3011 | |
| 3012 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3013 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3014 | RTC_DCHECK(IsUnifiedPlan()); |
| 3015 | for (auto transceiver : transceivers_) { |
| 3016 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3017 | return transceiver; |
| 3018 | } |
| 3019 | } |
| 3020 | return nullptr; |
| 3021 | } |
| 3022 | |
| 3023 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3024 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3025 | cricket::MediaType media_type) const { |
| 3026 | RTC_DCHECK(IsUnifiedPlan()); |
| 3027 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3028 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3029 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3030 | // associated with any m= section and are not stopped, find the first such |
| 3031 | // RtpTransceiver. |
| 3032 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3033 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3034 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3035 | !transceiver->stopped()) { |
| 3036 | return transceiver; |
| 3037 | } |
| 3038 | } |
| 3039 | return nullptr; |
| 3040 | } |
| 3041 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3042 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3043 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3044 | transceiver, |
| 3045 | const SessionDescriptionInterface* sdesc) const { |
| 3046 | RTC_DCHECK(transceiver); |
| 3047 | RTC_DCHECK(sdesc); |
| 3048 | if (IsUnifiedPlan()) { |
| 3049 | if (!transceiver->internal()->mid()) { |
| 3050 | // This transceiver is not associated with a media section yet. |
| 3051 | return nullptr; |
| 3052 | } |
| 3053 | return sdesc->description()->GetContentByName( |
| 3054 | *transceiver->internal()->mid()); |
| 3055 | } else { |
| 3056 | // Plan B only allows at most one audio and one video section, so use the |
| 3057 | // first media section of that type. |
| 3058 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3059 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3060 | } |
| 3061 | } |
| 3062 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3063 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 3064 | return configuration_; |
| 3065 | } |
| 3066 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3067 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 3068 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3069 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3070 | if (IsClosed()) { |
| 3071 | RTC_LOG(LS_ERROR) << "SetConfiguration: PeerConnection is closed."; |
| 3072 | return SafeSetError(RTCErrorType::INVALID_STATE, error); |
| 3073 | } |
| 3074 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3075 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3076 | // size is not allowed, and changing the set of ICE servers will not result |
| 3077 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3078 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3079 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3080 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 3081 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3082 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3083 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3084 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3085 | if (local_description() && |
| 3086 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3087 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3088 | "SetLocalDescription."; |
| 3089 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3090 | } |
| 3091 | |
| 3092 | if (remote_description() && |
| 3093 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3094 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3095 | "SetRemoteDescription."; |
| 3096 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3097 | } |
| 3098 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3099 | if (local_description() && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3100 | configuration.use_media_transport_for_data_channels != |
| 3101 | configuration_.use_media_transport_for_data_channels) { |
| 3102 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3103 | "after calling SetLocalDescription."; |
| 3104 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3105 | } |
| 3106 | |
| 3107 | if (remote_description() && |
| 3108 | configuration.use_media_transport_for_data_channels != |
| 3109 | configuration_.use_media_transport_for_data_channels) { |
| 3110 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3111 | "after calling SetRemoteDescription."; |
| 3112 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3113 | } |
| 3114 | |
| 3115 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3116 | configuration.crypto_options != configuration_.crypto_options) { |
| 3117 | RTC_LOG(LS_ERROR) << "Can't change crypto_options after calling " |
| 3118 | "SetLocalDescription."; |
| 3119 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3120 | } |
| 3121 | |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3122 | if (configuration.use_media_transport_for_data_channels || |
| 3123 | configuration.use_media_transport) { |
| 3124 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 3125 | << "Media transport requires MaxBundle policy."; |
| 3126 | } |
| 3127 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3128 | // The simplest (and most future-compatible) way to tell if the config was |
| 3129 | // modified in an invalid way is to copy each property we do support |
| 3130 | // modifying, then use operator==. There are far more properties we don't |
| 3131 | // support modifying than those we do, and more could be added. |
| 3132 | RTCConfiguration modified_config = configuration_; |
| 3133 | modified_config.servers = configuration.servers; |
| 3134 | modified_config.type = configuration.type; |
| 3135 | modified_config.ice_candidate_pool_size = |
| 3136 | configuration.ice_candidate_pool_size; |
| 3137 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3138 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 3139 | modified_config.ice_check_interval_strong_connectivity = |
| 3140 | configuration.ice_check_interval_strong_connectivity; |
| 3141 | modified_config.ice_check_interval_weak_connectivity = |
| 3142 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 3143 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 3144 | modified_config.ice_unwritable_min_checks = |
| 3145 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 3146 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3147 | modified_config.stun_candidate_keepalive_interval = |
| 3148 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3149 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 3150 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3151 | modified_config.active_reset_srtp_params = |
| 3152 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3153 | modified_config.use_media_transport = configuration.use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3154 | modified_config.use_media_transport_for_data_channels = |
| 3155 | configuration.use_media_transport_for_data_channels; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3156 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3157 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3158 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3159 | } |
| 3160 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 3161 | // Validate the modified configuration. |
| 3162 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 3163 | if (!validate_error.ok()) { |
| 3164 | return SafeSetError(std::move(validate_error), error); |
| 3165 | } |
| 3166 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3167 | // Note that this isn't possible through chromium, since it's an unsigned |
| 3168 | // short in WebIDL. |
| 3169 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 3170 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3171 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 3172 | } |
| 3173 | |
| 3174 | // Parse ICE servers before hopping to network thread. |
| 3175 | cricket::ServerAddresses stun_servers; |
| 3176 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 3177 | RTCErrorType parse_error = |
| 3178 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 3179 | if (parse_error != RTCErrorType::NONE) { |
| 3180 | return SafeSetError(parse_error, error); |
| 3181 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3182 | // Note if STUN or TURN servers were supplied. |
| 3183 | if (!stun_servers.empty()) { |
| 3184 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 3185 | } |
| 3186 | if (!turn_servers.empty()) { |
| 3187 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 3188 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3189 | |
| 3190 | // In theory this shouldn't fail. |
| 3191 | if (!network_thread()->Invoke<bool>( |
| 3192 | RTC_FROM_HERE, |
| 3193 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 3194 | stun_servers, turn_servers, modified_config.type, |
| 3195 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3196 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3197 | modified_config.turn_customizer, |
| 3198 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3199 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3200 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 3201 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3202 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3203 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 3204 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 3205 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3206 | if (modified_config.servers != configuration_.servers || |
| 3207 | modified_config.type != configuration_.type || |
| 3208 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3209 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3210 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3211 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 3212 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 3213 | transport_controller_->SetMediaTransportSettings( |
| 3214 | modified_config.use_media_transport, |
| 3215 | modified_config.use_media_transport_for_data_channels); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3216 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3217 | if (configuration_.active_reset_srtp_params != |
| 3218 | modified_config.active_reset_srtp_params) { |
| 3219 | transport_controller_->SetActiveResetSrtpParams( |
| 3220 | modified_config.active_reset_srtp_params); |
| 3221 | } |
| 3222 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3223 | configuration_ = modified_config; |
| 3224 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3227 | bool PeerConnection::AddIceCandidate( |
| 3228 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3229 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3230 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3231 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3232 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3233 | return false; |
| 3234 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3235 | |
| 3236 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3237 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3238 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3239 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3240 | return false; |
| 3241 | } |
| 3242 | |
| 3243 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3244 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3245 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3246 | return false; |
| 3247 | } |
| 3248 | |
| 3249 | bool valid = false; |
| 3250 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 3251 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3252 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3253 | return false; |
| 3254 | } |
| 3255 | |
| 3256 | // Add this candidate to the remote session description. |
| 3257 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3258 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3259 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3260 | return false; |
| 3261 | } |
| 3262 | |
| 3263 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3264 | bool result = UseCandidate(ice_candidate); |
| 3265 | if (result) { |
| 3266 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 3267 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 3268 | } else { |
| 3269 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 3270 | } |
| 3271 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3272 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3273 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3274 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3275 | return true; |
| 3276 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3279 | bool PeerConnection::RemoveIceCandidates( |
| 3280 | const std::vector<cricket::Candidate>& candidates) { |
| 3281 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3282 | if (IsClosed()) { |
| 3283 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 3284 | return false; |
| 3285 | } |
| 3286 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3287 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3288 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 3289 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3290 | return false; |
| 3291 | } |
| 3292 | |
| 3293 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3294 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3295 | return false; |
| 3296 | } |
| 3297 | |
| 3298 | size_t number_removed = |
| 3299 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3300 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3301 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3302 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3303 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3304 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3308 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3309 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3310 | RTC_LOG(LS_ERROR) |
| 3311 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3312 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3313 | } |
| 3314 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3315 | } |
| 3316 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3317 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3318 | if (!worker_thread()->IsCurrent()) { |
| 3319 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3320 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3323 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3324 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3325 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3326 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3327 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3328 | "min_bitrate_bps <= 0"); |
| 3329 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3330 | if (has_start) { |
| 3331 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3332 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3333 | "start_bitrate_bps < min_bitrate_bps"); |
| 3334 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3335 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3336 | "curent_bitrate_bps < 0"); |
| 3337 | } |
| 3338 | } |
| 3339 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3340 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3341 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3342 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3343 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3344 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3345 | "max_bitrate_bps < min_bitrate_bps"); |
| 3346 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3347 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3348 | "max_bitrate_bps < 0"); |
| 3349 | } |
| 3350 | } |
| 3351 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3352 | RTC_DCHECK(call_.get()); |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3353 | call_->GetTransportControllerSend()->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3354 | |
| 3355 | return RTCError::OK(); |
| 3356 | } |
| 3357 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3358 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3359 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3360 | bitrate_allocation_strategy) { |
| 3361 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3362 | if (!worker_thread->IsCurrent()) { |
| 3363 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3364 | bitrate_allocation_strategy.release(); |
| 3365 | auto functor = [this, strategy_raw]() { |
| 3366 | call_->SetBitrateAllocationStrategy( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3367 | absl::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3368 | }; |
| 3369 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3370 | return; |
| 3371 | } |
| 3372 | RTC_DCHECK(call_.get()); |
| 3373 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3374 | } |
| 3375 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3376 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3377 | if (!worker_thread()->IsCurrent()) { |
| 3378 | worker_thread()->Invoke<void>( |
| 3379 | RTC_FROM_HERE, |
| 3380 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3381 | return; |
| 3382 | } |
| 3383 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3384 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3385 | audio_state->SetPlayout(playout); |
| 3386 | } |
| 3387 | |
| 3388 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3389 | if (!worker_thread()->IsCurrent()) { |
| 3390 | worker_thread()->Invoke<void>( |
| 3391 | RTC_FROM_HERE, |
| 3392 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3393 | return; |
| 3394 | } |
| 3395 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3396 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3397 | audio_state->SetRecording(recording); |
| 3398 | } |
| 3399 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3400 | std::unique_ptr<rtc::SSLCertificate> |
| 3401 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3402 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3403 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3404 | return nullptr; |
| 3405 | } |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 3406 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3409 | std::unique_ptr<rtc::SSLCertChain> |
| 3410 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3411 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3412 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3413 | return nullptr; |
| 3414 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3415 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3416 | audio_transceiver->internal()->channel()->transport_name()); |
| 3417 | } |
| 3418 | |
| 3419 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3420 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3421 | for (auto transceiver : transceivers_) { |
| 3422 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3423 | return transceiver; |
| 3424 | } |
| 3425 | } |
| 3426 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3427 | } |
| 3428 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3429 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3430 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3431 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3432 | const size_t max_size = (max_size_bytes < 0) |
| 3433 | ? RtcEventLog::kUnlimitedOutput |
| 3434 | : rtc::saturated_cast<size_t>(max_size_bytes); |
Bjorn Terelius | 8b55602 | 2018-11-27 15:43:01 +0100 | [diff] [blame] | 3435 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 3436 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 3437 | output_period_ms = 5000; |
| 3438 | } |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3439 | return StartRtcEventLog( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3440 | absl::make_unique<RtcEventLogOutputFile>(file, max_size), |
Bjorn Terelius | 8b55602 | 2018-11-27 15:43:01 +0100 | [diff] [blame] | 3441 | output_period_ms); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3442 | } |
| 3443 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3444 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3445 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3446 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3447 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3448 | bool operator()() { |
| 3449 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3450 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3451 | PeerConnection* const pc; |
| 3452 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3453 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3454 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3455 | return worker_thread()->Invoke<bool>( |
| 3456 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3460 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3461 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3462 | } |
| 3463 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 3464 | rtc::scoped_refptr<DtlsTransportInterface> |
| 3465 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
| 3466 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3467 | } |
| 3468 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3469 | rtc::scoped_refptr<DtlsTransport> |
| 3470 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
| 3471 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3472 | } |
| 3473 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3474 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3475 | return pending_local_description_ ? pending_local_description_.get() |
| 3476 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3480 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3481 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3484 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3485 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3486 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3490 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3491 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3495 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3496 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3500 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3501 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3502 | } |
| 3503 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3504 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3505 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3506 | // Update stats here so that we have the most recent stats for tracks and |
| 3507 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3508 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3509 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3510 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3511 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3512 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 3513 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3514 | transceiver->Stop(); |
| 3515 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 3516 | |
| 3517 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3518 | // the transport controller below. |
| 3519 | if (stats_collector_) { |
| 3520 | stats_collector_->WaitForPendingRequest(); |
| 3521 | } |
| 3522 | |
| 3523 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3524 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3525 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3526 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3527 | // The event log is used in the transport controller, which must be outlived |
| 3528 | // by the former. CreateOffer by the peer connection is implemented |
| 3529 | // asynchronously and if the peer connection is closed without resetting the |
| 3530 | // WebRTC session description factory, the session description factory would |
| 3531 | // call the transport controller. |
| 3532 | webrtc_session_desc_factory_.reset(); |
| 3533 | transport_controller_.reset(); |
| 3534 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3535 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3536 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3537 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3538 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3539 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3540 | call_.reset(); |
| 3541 | // The event log must outlive call (and any other object that uses it). |
| 3542 | event_log_.reset(); |
| 3543 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3544 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3545 | // The .h file says that observer can be discarded after close() returns. |
| 3546 | // Make sure this is true. |
| 3547 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3550 | void PeerConnection::OnMessage(rtc::Message* msg) { |
| 3551 | switch (msg->message_id) { |
| 3552 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3553 | SetSessionDescriptionMsg* param = |
| 3554 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3555 | param->observer->OnSuccess(); |
| 3556 | delete param; |
| 3557 | break; |
| 3558 | } |
| 3559 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3560 | SetSessionDescriptionMsg* param = |
| 3561 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3562 | param->observer->OnFailure(std::move(param->error)); |
| 3563 | delete param; |
| 3564 | break; |
| 3565 | } |
| 3566 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3567 | CreateSessionDescriptionMsg* param = |
| 3568 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3569 | param->observer->OnFailure(std::move(param->error)); |
| 3570 | delete param; |
| 3571 | break; |
| 3572 | } |
| 3573 | case MSG_GETSTATS: { |
| 3574 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 3575 | StatsReports reports; |
| 3576 | stats_->GetStats(param->track, &reports); |
| 3577 | param->observer->OnComplete(reports); |
| 3578 | delete param; |
| 3579 | break; |
| 3580 | } |
| 3581 | case MSG_FREE_DATACHANNELS: { |
| 3582 | sctp_data_channels_to_free_.clear(); |
| 3583 | break; |
| 3584 | } |
| 3585 | case MSG_REPORT_USAGE_PATTERN: { |
| 3586 | ReportUsagePattern(); |
| 3587 | break; |
| 3588 | } |
| 3589 | default: |
| 3590 | RTC_NOTREACHED() << "Not implemented"; |
| 3591 | break; |
| 3592 | } |
| 3593 | } |
| 3594 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3595 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3596 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3597 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3598 | GetAudioTransceiver()->internal()->channel()); |
| 3599 | if (voice_channel) { |
| 3600 | return voice_channel->media_channel(); |
| 3601 | } else { |
| 3602 | return nullptr; |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3607 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3608 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3609 | GetVideoTransceiver()->internal()->channel()); |
| 3610 | if (video_channel) { |
| 3611 | return video_channel->media_channel(); |
| 3612 | } else { |
| 3613 | return nullptr; |
| 3614 | } |
| 3615 | } |
| 3616 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3617 | void PeerConnection::CreateAudioReceiver( |
| 3618 | MediaStreamInterface* stream, |
| 3619 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3620 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3621 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3622 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3623 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3624 | auto* audio_receiver = new AudioRtpReceiver( |
| 3625 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3626 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3627 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3628 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3629 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3630 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3631 | Observer()->OnAddTrack(receiver, std::move(streams)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3632 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3635 | void PeerConnection::CreateVideoReceiver( |
| 3636 | MediaStreamInterface* stream, |
| 3637 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3638 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3639 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3640 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3641 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3642 | auto* video_receiver = new VideoRtpReceiver( |
| 3643 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3644 | video_receiver->SetMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3645 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3646 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3647 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3648 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3649 | Observer()->OnAddTrack(receiver, std::move(streams)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3650 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3653 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3654 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3655 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3656 | const RtpSenderInfo& remote_sender_info) { |
| 3657 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3658 | if (!receiver) { |
| 3659 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3660 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3661 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3662 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3663 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3664 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3665 | } else { |
| 3666 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3667 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3668 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3671 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3672 | MediaStreamInterface* stream) { |
| 3673 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3674 | RTC_DCHECK(track); |
| 3675 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3676 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3677 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3678 | // We already have a sender for this track, so just change the stream_id |
| 3679 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3680 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3681 | return; |
| 3682 | } |
| 3683 | |
| 3684 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3685 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3686 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3687 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3688 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3689 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3690 | // which will connect the sender to the underlying transport. This can |
| 3691 | // occur if a local session description that contains the ID of the sender |
| 3692 | // is set before AddStream is called. It can also occur if the local |
| 3693 | // session description is not changed and RemoveStream is called, and |
| 3694 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3695 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3696 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3697 | if (sender_info) { |
| 3698 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3703 | // indefinitely, when we have unified plan SDP. |
| 3704 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3705 | MediaStreamInterface* stream) { |
| 3706 | RTC_DCHECK(!IsClosed()); |
| 3707 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3708 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3709 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3710 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3711 | return; |
| 3712 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3713 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3717 | MediaStreamInterface* stream) { |
| 3718 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3719 | RTC_DCHECK(track); |
| 3720 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3721 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3722 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3723 | // We already have a sender for this track, so just change the stream_id |
| 3724 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3725 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3726 | return; |
| 3727 | } |
| 3728 | |
| 3729 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3730 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3731 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3732 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3733 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3734 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3735 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3736 | if (sender_info) { |
| 3737 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3742 | MediaStreamInterface* stream) { |
| 3743 | RTC_DCHECK(!IsClosed()); |
| 3744 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3745 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3746 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3747 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3748 | return; |
| 3749 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3750 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3751 | } |
| 3752 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3753 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3754 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3755 | if (ice_connection_state_ == new_state) { |
| 3756 | return; |
| 3757 | } |
| 3758 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3759 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3760 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3761 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3762 | return; |
| 3763 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3764 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3765 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3766 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3767 | RTC_DCHECK(ice_connection_state_ != |
| 3768 | PeerConnectionInterface::kIceConnectionClosed); |
| 3769 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3770 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3771 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 3774 | void PeerConnection::SetStandardizedIceConnectionState( |
| 3775 | PeerConnectionInterface::IceConnectionState new_state) { |
| 3776 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3777 | if (standardized_ice_connection_state_ == new_state) |
| 3778 | return; |
| 3779 | if (IsClosed()) |
| 3780 | return; |
| 3781 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 11:25:14 +0100 | [diff] [blame] | 3782 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 3785 | void PeerConnection::SetConnectionState( |
| 3786 | PeerConnectionInterface::PeerConnectionState new_state) { |
| 3787 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3788 | if (connection_state_ == new_state) |
| 3789 | return; |
| 3790 | if (IsClosed()) |
| 3791 | return; |
| 3792 | connection_state_ = new_state; |
| 3793 | Observer()->OnConnectionChange(new_state); |
| 3794 | } |
| 3795 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3796 | void PeerConnection::OnIceGatheringChange( |
| 3797 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3798 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3799 | if (IsClosed()) { |
| 3800 | return; |
| 3801 | } |
| 3802 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3803 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3806 | void PeerConnection::OnIceCandidate( |
| 3807 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3808 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3809 | if (IsClosed()) { |
| 3810 | return; |
| 3811 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3812 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 3813 | if (candidate->candidate().type() == LOCAL_PORT_TYPE && |
| 3814 | candidate->candidate().address().IsPrivateIP()) { |
| 3815 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 3816 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3817 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3820 | void PeerConnection::OnIceCandidatesRemoved( |
| 3821 | const std::vector<cricket::Candidate>& candidates) { |
| 3822 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3823 | if (IsClosed()) { |
| 3824 | return; |
| 3825 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3826 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3829 | void PeerConnection::ChangeSignalingState( |
| 3830 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3831 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3832 | if (signaling_state_ == signaling_state) { |
| 3833 | return; |
| 3834 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3835 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3836 | << GetSignalingStateString(signaling_state_) |
| 3837 | << " New state: " |
| 3838 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3839 | signaling_state_ = signaling_state; |
| 3840 | if (signaling_state == kClosed) { |
| 3841 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3842 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 3843 | standardized_ice_connection_state_ = |
| 3844 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 3845 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 3846 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3847 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3848 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3849 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3850 | } |
| 3851 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3852 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3855 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3856 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3857 | if (IsClosed()) { |
| 3858 | return; |
| 3859 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3860 | AddAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3861 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3862 | } |
| 3863 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3864 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3865 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3866 | if (IsClosed()) { |
| 3867 | return; |
| 3868 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3869 | RemoveAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3870 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3874 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3875 | if (IsClosed()) { |
| 3876 | return; |
| 3877 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3878 | AddVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3879 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3883 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3884 | if (IsClosed()) { |
| 3885 | return; |
| 3886 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3887 | RemoveVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3888 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3889 | } |
| 3890 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3891 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3892 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3893 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3894 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3895 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3896 | } |
| 3897 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3898 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3899 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3900 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3901 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3902 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3903 | msg->error = std::move(error); |
| 3904 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3905 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3909 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3910 | RTCError error) { |
| 3911 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3912 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 3913 | msg->error = std::move(error); |
| 3914 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3915 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3918 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3919 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3920 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3921 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3922 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3923 | if (IsUnifiedPlan()) { |
| 3924 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3925 | } else { |
| 3926 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3927 | } |
| 3928 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3929 | // Intentionally unset the data channel type for RTP data channel with the |
| 3930 | // second condition. Otherwise the RTP data channels would be successfully |
| 3931 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3932 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3933 | // people won't try to use them. |
| 3934 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3935 | session_options->data_channel_type = data_channel_type(); |
| 3936 | } |
| 3937 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3938 | // Apply ICE restart flag and renomination flag. |
| 3939 | for (auto& options : session_options->media_description_options) { |
| 3940 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3941 | options.transport_options.enable_ice_renomination = |
| 3942 | configuration_.enable_ice_renomination; |
| 3943 | } |
| 3944 | |
| 3945 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3946 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 3947 | session_options->pooled_ice_credentials = |
| 3948 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 3949 | RTC_FROM_HERE, |
| 3950 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 3951 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 3952 | session_options->offer_extmap_allow_mixed = |
| 3953 | configuration_.offer_extmap_allow_mixed; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3957 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3958 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3959 | // Figure out transceiver directional preferences. |
| 3960 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3961 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3962 | |
| 3963 | // By default, generate sendrecv/recvonly m= sections. |
| 3964 | bool recv_audio = true; |
| 3965 | bool recv_video = true; |
| 3966 | |
| 3967 | // By default, only offer a new m= section if we have media to send with it. |
| 3968 | bool offer_new_audio_description = send_audio; |
| 3969 | bool offer_new_video_description = send_video; |
| 3970 | bool offer_new_data_description = HasDataChannels(); |
| 3971 | |
| 3972 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3973 | if (offer_answer_options.offer_to_receive_audio != |
| 3974 | RTCOfferAnswerOptions::kUndefined) { |
| 3975 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3976 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3977 | offer_new_audio_description || |
| 3978 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3979 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3980 | if (offer_answer_options.offer_to_receive_video != |
| 3981 | RTCOfferAnswerOptions::kUndefined) { |
| 3982 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3983 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3984 | offer_new_video_description || |
| 3985 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3986 | } |
| 3987 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3988 | absl::optional<size_t> audio_index; |
| 3989 | absl::optional<size_t> video_index; |
| 3990 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3991 | // If a current description exists, generate m= sections in the same order, |
| 3992 | // using the first audio/video/data section that appears and rejecting |
| 3993 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3994 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3995 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3996 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3997 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3998 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3999 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4000 | } |
| 4001 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4002 | // Add audio/video/data m= sections to the end if needed. |
| 4003 | if (!audio_index && offer_new_audio_description) { |
| 4004 | session_options->media_description_options.push_back( |
| 4005 | cricket::MediaDescriptionOptions( |
| 4006 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4007 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4008 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4009 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4010 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4011 | if (!video_index && offer_new_video_description) { |
| 4012 | session_options->media_description_options.push_back( |
| 4013 | cricket::MediaDescriptionOptions( |
| 4014 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4015 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4016 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4017 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4018 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4019 | if (!data_index && offer_new_data_description) { |
| 4020 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4021 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4022 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4026 | !audio_index ? nullptr |
| 4027 | : &session_options->media_description_options[*audio_index]; |
| 4028 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4029 | !video_index ? nullptr |
| 4030 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4031 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4032 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4033 | audio_media_description_options, |
| 4034 | video_media_description_options, |
| 4035 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4038 | static cricket::MediaDescriptionOptions |
| 4039 | GetMediaDescriptionOptionsForTransceiver( |
| 4040 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4041 | transceiver, |
| 4042 | const std::string& mid) { |
| 4043 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4044 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4045 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4046 | // This behavior is specified in JSEP. The gist is that: |
| 4047 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 4048 | // sendrecv. |
| 4049 | // 2. If the MSID is included, then it must be included in any subsequent |
| 4050 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 4051 | if (!transceiver->stopped() && |
| 4052 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 4053 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 4054 | cricket::SenderOptions sender_options; |
| 4055 | sender_options.track_id = transceiver->sender()->id(); |
| 4056 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4057 | int num_send_encoding_layers = |
| 4058 | transceiver->sender()->init_send_encodings().size(); |
| 4059 | sender_options.num_sim_layers = |
| 4060 | !num_send_encoding_layers ? 1 : num_send_encoding_layers; |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4061 | media_description_options.sender_options.push_back(sender_options); |
| 4062 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4063 | return media_description_options; |
| 4064 | } |
| 4065 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4066 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 4067 | static const ContentInfo* GetContentByIndex( |
| 4068 | const SessionDescriptionInterface* sdesc, |
| 4069 | size_t i) { |
| 4070 | if (!sdesc) { |
| 4071 | return nullptr; |
| 4072 | } |
| 4073 | const ContentInfos& contents = sdesc->description()->contents(); |
| 4074 | return (i < contents.size() ? &contents[i] : nullptr); |
| 4075 | } |
| 4076 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4077 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 4078 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4079 | cricket::MediaSessionOptions* session_options) { |
| 4080 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 4081 | // Offers) and 5.2.2 (Subsequent Offers). |
| 4082 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 4083 | const ContentInfos& local_contents = |
| 4084 | (local_description() ? local_description()->description()->contents() |
| 4085 | : ContentInfos()); |
| 4086 | const ContentInfos& remote_contents = |
| 4087 | (remote_description() ? remote_description()->description()->contents() |
| 4088 | : ContentInfos()); |
| 4089 | // The mline indices that can be recycled. New transceivers should reuse these |
| 4090 | // slots first. |
| 4091 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4092 | // First, go through each media section that exists in either the local or |
| 4093 | // remote description and generate a media section in this offer for the |
| 4094 | // associated transceiver. If a media section can be recycled, generate a |
| 4095 | // default, rejected media section here that can be later overwritten. |
| 4096 | for (size_t i = 0; |
| 4097 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 4098 | // Either |local_content| or |remote_content| is non-null. |
| 4099 | const ContentInfo* local_content = |
| 4100 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4101 | const ContentInfo* current_local_content = |
| 4102 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4103 | const ContentInfo* remote_content = |
| 4104 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4105 | const ContentInfo* current_remote_content = |
| 4106 | GetContentByIndex(current_remote_description(), i); |
| 4107 | bool had_been_rejected = |
| 4108 | (current_local_content && current_local_content->rejected) || |
| 4109 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4110 | const std::string& mid = |
| 4111 | (local_content ? local_content->name : remote_content->name); |
| 4112 | cricket::MediaType media_type = |
| 4113 | (local_content ? local_content->media_description()->type() |
| 4114 | : remote_content->media_description()->type()); |
| 4115 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4116 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4117 | auto transceiver = GetAssociatedTransceiver(mid); |
| 4118 | RTC_CHECK(transceiver); |
| 4119 | // A media section is considered eligible for recycling if it is marked as |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4120 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 4121 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4122 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4123 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 4124 | RtpTransceiverDirection::kInactive, |
| 4125 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4126 | recycleable_mline_indices.push(i); |
| 4127 | } else { |
| 4128 | session_options->media_description_options.push_back( |
| 4129 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 4130 | // CreateOffer shouldn't really cause any state changes in |
| 4131 | // PeerConnection, but we need a way to match new transceivers to new |
| 4132 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 4133 | // by recording the index of the media section generated for the |
| 4134 | // transceiver in the offer. |
| 4135 | transceiver->internal()->set_mline_index(i); |
| 4136 | } |
| 4137 | } else { |
| 4138 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4139 | RTC_CHECK(GetDataMid()); |
| 4140 | if (had_been_rejected || mid != *GetDataMid()) { |
| 4141 | session_options->media_description_options.push_back( |
| 4142 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 4143 | } else { |
| 4144 | session_options->media_description_options.push_back( |
| 4145 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 4146 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4147 | } |
| 4148 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4149 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4150 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 4151 | // and not associated). Reuse media sections marked as recyclable first, |
| 4152 | // otherwise append to the end of the offer. New media sections should be |
| 4153 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 4154 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4155 | if (transceiver->mid() || transceiver->stopped()) { |
| 4156 | continue; |
| 4157 | } |
| 4158 | size_t mline_index; |
| 4159 | if (!recycleable_mline_indices.empty()) { |
| 4160 | mline_index = recycleable_mline_indices.front(); |
| 4161 | recycleable_mline_indices.pop(); |
| 4162 | session_options->media_description_options[mline_index] = |
| 4163 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4164 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4165 | } else { |
| 4166 | mline_index = session_options->media_description_options.size(); |
| 4167 | session_options->media_description_options.push_back( |
| 4168 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4169 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4170 | } |
| 4171 | // See comment above for why CreateOffer changes the transceiver's state. |
| 4172 | transceiver->internal()->set_mline_index(mline_index); |
| 4173 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4174 | // Lastly, add a m-section if we have local data channels and an m section |
| 4175 | // does not already exist. |
| 4176 | if (!GetDataMid() && HasDataChannels()) { |
| 4177 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4178 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4179 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | void PeerConnection::GetOptionsForAnswer( |
| 4183 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4184 | cricket::MediaSessionOptions* session_options) { |
| 4185 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 4186 | |
| 4187 | if (IsUnifiedPlan()) { |
| 4188 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 4189 | } else { |
| 4190 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 4191 | } |
| 4192 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4193 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 4194 | // the RTP data channels would be successfully negotiated by default and the |
| 4195 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 4196 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 4197 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4198 | session_options->data_channel_type = data_channel_type(); |
| 4199 | } |
| 4200 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4201 | // Apply ICE renomination flag. |
| 4202 | for (auto& options : session_options->media_description_options) { |
| 4203 | options.transport_options.enable_ice_renomination = |
| 4204 | configuration_.enable_ice_renomination; |
| 4205 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 4206 | |
| 4207 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4208 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4209 | session_options->pooled_ice_credentials = |
| 4210 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4211 | RTC_FROM_HERE, |
| 4212 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4213 | port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4214 | } |
| 4215 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4216 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 4217 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4218 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4219 | // Figure out transceiver directional preferences. |
| 4220 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4221 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4222 | |
| 4223 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 4224 | // restricted by the direction in the offer. |
| 4225 | bool recv_audio = true; |
| 4226 | bool recv_video = true; |
| 4227 | |
| 4228 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4229 | if (offer_answer_options.offer_to_receive_audio != |
| 4230 | RTCOfferAnswerOptions::kUndefined) { |
| 4231 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 4232 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4233 | if (offer_answer_options.offer_to_receive_video != |
| 4234 | RTCOfferAnswerOptions::kUndefined) { |
| 4235 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4238 | absl::optional<size_t> audio_index; |
| 4239 | absl::optional<size_t> video_index; |
| 4240 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 4241 | |
| 4242 | // Generate m= sections that match those in the offer. |
| 4243 | // Note that mediasession.cc will handle intersection our preferred |
| 4244 | // direction with the offered direction. |
| 4245 | GenerateMediaDescriptionOptions( |
| 4246 | remote_description(), |
| 4247 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4248 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 4249 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4250 | |
| 4251 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4252 | !audio_index ? nullptr |
| 4253 | : &session_options->media_description_options[*audio_index]; |
| 4254 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4255 | !video_index ? nullptr |
| 4256 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4257 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4258 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4259 | audio_media_description_options, |
| 4260 | video_media_description_options, |
| 4261 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4262 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4263 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4264 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 4265 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4266 | cricket::MediaSessionOptions* session_options) { |
| 4267 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 4268 | // Answers) and 5.3.2 (Subsequent Answers). |
| 4269 | RTC_DCHECK(remote_description()); |
| 4270 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 4271 | for (const ContentInfo& content : |
| 4272 | remote_description()->description()->contents()) { |
| 4273 | cricket::MediaType media_type = content.media_description()->type(); |
| 4274 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4275 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4276 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 4277 | RTC_CHECK(transceiver); |
| 4278 | session_options->media_description_options.push_back( |
| 4279 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 4280 | } else { |
| 4281 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 4282 | // Reject all data sections if data channels are disabled. |
| 4283 | // Reject a data section if it has already been rejected. |
| 4284 | // Reject all data sections except for the first one. |
| 4285 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 4286 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4287 | session_options->media_description_options.push_back( |
| 4288 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4289 | } else { |
| 4290 | session_options->media_description_options.push_back( |
| 4291 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4292 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4293 | } |
| 4294 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4295 | } |
| 4296 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4297 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 4298 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4299 | RtpTransceiverDirection audio_direction, |
| 4300 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4301 | absl::optional<size_t>* audio_index, |
| 4302 | absl::optional<size_t>* video_index, |
| 4303 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4304 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4305 | for (const cricket::ContentInfo& content : |
| 4306 | session_desc->description()->contents()) { |
| 4307 | if (IsAudioContent(&content)) { |
| 4308 | // If we already have an audio m= section, reject this extra one. |
| 4309 | if (*audio_index) { |
| 4310 | session_options->media_description_options.push_back( |
| 4311 | cricket::MediaDescriptionOptions( |
| 4312 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4313 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4314 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4315 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4316 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4317 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 4318 | content.name, audio_direction, |
| 4319 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4320 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4321 | } |
| 4322 | } else if (IsVideoContent(&content)) { |
| 4323 | // If we already have an video m= section, reject this extra one. |
| 4324 | if (*video_index) { |
| 4325 | session_options->media_description_options.push_back( |
| 4326 | cricket::MediaDescriptionOptions( |
| 4327 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4328 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4329 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4330 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4331 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4332 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 4333 | content.name, video_direction, |
| 4334 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4335 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4336 | } |
| 4337 | } else { |
| 4338 | RTC_DCHECK(IsDataContent(&content)); |
| 4339 | // If we already have an data m= section, reject this extra one. |
| 4340 | if (*data_index) { |
| 4341 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4342 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4343 | } else { |
| 4344 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4345 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4346 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4347 | } |
| 4348 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4349 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4350 | } |
| 4351 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4352 | cricket::MediaDescriptionOptions |
| 4353 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4354 | const std::string& mid) const { |
| 4355 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4356 | // expect sendrecv. |
| 4357 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4358 | RtpTransceiverDirection::kSendRecv, |
| 4359 | /*stopped=*/false); |
| 4360 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4361 | return options; |
| 4362 | } |
| 4363 | |
| 4364 | cricket::MediaDescriptionOptions |
| 4365 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4366 | const std::string& mid) const { |
| 4367 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4368 | RtpTransceiverDirection::kInactive, |
| 4369 | /*stopped=*/true); |
| 4370 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4371 | return options; |
| 4372 | } |
| 4373 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4374 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4375 | switch (data_channel_type_) { |
| 4376 | case cricket::DCT_RTP: |
| 4377 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4378 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4379 | } |
| 4380 | return rtp_data_channel_->content_name(); |
| 4381 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4382 | return sctp_mid_; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4383 | case cricket::DCT_MEDIA_TRANSPORT: |
| 4384 | return media_transport_data_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4385 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4386 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4387 | } |
| 4388 | } |
| 4389 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4390 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4391 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4392 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4393 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4394 | } |
| 4395 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4396 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4397 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4398 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4399 | cricket::MediaType media_type, |
| 4400 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4401 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4402 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4403 | std::vector<RtpSenderInfo>* current_senders = |
| 4404 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4405 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4406 | // 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] | 4407 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4408 | for (auto sender_it = current_senders->begin(); |
| 4409 | sender_it != current_senders->end(); |
| 4410 | /* incremented manually */) { |
| 4411 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4412 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4413 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4414 | std::string params_stream_id; |
| 4415 | if (params) { |
| 4416 | params_stream_id = |
| 4417 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4418 | : kDefaultStreamId); |
| 4419 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4420 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4421 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4422 | // 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] | 4423 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4424 | sender_exists) { |
| 4425 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4426 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4427 | OnRemoteSenderRemoved(info, media_type); |
| 4428 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4429 | } |
| 4430 | } |
| 4431 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4432 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4433 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4434 | if (!params.has_ssrcs()) { |
| 4435 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4436 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4437 | // create a default. |
| 4438 | default_sender_needed = true; |
| 4439 | break; |
| 4440 | } |
| 4441 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4442 | // |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] | 4443 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4444 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4445 | // not supported in Plan B, we just take the first here and create the |
| 4446 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4447 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4448 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4449 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4450 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4451 | uint32_t ssrc = params.first_ssrc(); |
| 4452 | |
| 4453 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4454 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4455 | if (!stream) { |
| 4456 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4457 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4458 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4459 | remote_streams_->AddStream(stream); |
| 4460 | new_streams->AddStream(stream); |
| 4461 | } |
| 4462 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4463 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4464 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4465 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4466 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4467 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4468 | } |
| 4469 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4470 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4471 | // Add default sender if necessary. |
| 4472 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4473 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4474 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4475 | if (!default_stream) { |
| 4476 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4477 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4478 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4479 | remote_streams_->AddStream(default_stream); |
| 4480 | new_streams->AddStream(default_stream); |
| 4481 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4482 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4483 | ? kDefaultAudioSenderId |
| 4484 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4485 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4486 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4487 | if (!default_sender_info) { |
| 4488 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4489 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4490 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4491 | } |
| 4492 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4493 | } |
| 4494 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4495 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4496 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4497 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4498 | << " receiver for track_id=" << sender_info.sender_id |
| 4499 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4500 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4501 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4502 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4503 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4504 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4505 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4506 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4507 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4511 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4512 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4513 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 4514 | << " receiver for track_id=" << sender_info.sender_id |
| 4515 | << " and stream_id=" << sender_info.stream_id; |
| 4516 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4517 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4518 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4519 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4520 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4521 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4522 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4523 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4524 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4525 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4526 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4527 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4528 | } |
| 4529 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4530 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4531 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4532 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4533 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4534 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4535 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4536 | // There's no guarantee the track is still available, e.g. the track may |
| 4537 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4538 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4539 | } |
| 4540 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4541 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4542 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4543 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4544 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4545 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4549 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4550 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4551 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4552 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4553 | streams_to_remove.push_back(stream); |
| 4554 | } |
| 4555 | } |
| 4556 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4557 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4558 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4559 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4563 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4564 | const std::vector<cricket::StreamParams>& streams, |
| 4565 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4566 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4567 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4568 | // 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] | 4569 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4570 | for (auto sender_it = current_senders->begin(); |
| 4571 | sender_it != current_senders->end(); |
| 4572 | /* incremented manually */) { |
| 4573 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4574 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4575 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4576 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4577 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4578 | OnLocalSenderRemoved(info, media_type); |
| 4579 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4580 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4581 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4585 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4586 | for (const cricket::StreamParams& params : streams) { |
| 4587 | // 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] | 4588 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4589 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4590 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4591 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4592 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4593 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4594 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4595 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4596 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4597 | } |
| 4598 | } |
| 4599 | } |
| 4600 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4601 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4602 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4603 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4604 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4605 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4606 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4607 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4608 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4609 | return; |
| 4610 | } |
| 4611 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4612 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4613 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4614 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4615 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4616 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4617 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4618 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4619 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4620 | } |
| 4621 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4622 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4623 | cricket::MediaType media_type) { |
| 4624 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4625 | if (!sender) { |
| 4626 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4627 | // SessionDescriptions has been renegotiated. |
| 4628 | return; |
| 4629 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4630 | |
| 4631 | // A sender has been removed from the SessionDescription but it's still |
| 4632 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4633 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4634 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4635 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4636 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4637 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4638 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4639 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4640 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4644 | const cricket::StreamParamsVec& streams) { |
| 4645 | std::vector<std::string> existing_channels; |
| 4646 | |
| 4647 | // Find new and active data channels. |
| 4648 | for (const cricket::StreamParams& params : streams) { |
| 4649 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4650 | // we use the same naming of data channels as we do for |
| 4651 | // MediaStreams and Tracks. |
| 4652 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4653 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4654 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4655 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4656 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4657 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4658 | continue; |
| 4659 | } |
| 4660 | // Set the SSRC the data channel should use for sending. |
| 4661 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4662 | existing_channels.push_back(data_channel_it->first); |
| 4663 | } |
| 4664 | |
| 4665 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4666 | } |
| 4667 | |
| 4668 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4669 | const cricket::StreamParamsVec& streams) { |
| 4670 | std::vector<std::string> existing_channels; |
| 4671 | |
| 4672 | // Find new and active data channels. |
| 4673 | for (const cricket::StreamParams& params : streams) { |
| 4674 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4675 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4676 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4677 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4678 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4679 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4680 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4681 | // This is a new data channel. |
| 4682 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4683 | } else { |
| 4684 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4685 | } |
| 4686 | existing_channels.push_back(label); |
| 4687 | } |
| 4688 | |
| 4689 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4690 | } |
| 4691 | |
| 4692 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4693 | const std::vector<std::string>& active_channels, |
| 4694 | bool is_local_update) { |
| 4695 | auto it = rtp_data_channels_.begin(); |
| 4696 | while (it != rtp_data_channels_.end()) { |
| 4697 | DataChannel* data_channel = it->second; |
| 4698 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4699 | data_channel->label()) != active_channels.end()) { |
| 4700 | ++it; |
| 4701 | continue; |
| 4702 | } |
| 4703 | |
| 4704 | if (is_local_update) { |
| 4705 | data_channel->SetSendSsrc(0); |
| 4706 | } else { |
| 4707 | data_channel->RemotePeerRequestClose(); |
| 4708 | } |
| 4709 | |
| 4710 | if (data_channel->state() == DataChannel::kClosed) { |
| 4711 | rtp_data_channels_.erase(it); |
| 4712 | it = rtp_data_channels_.begin(); |
| 4713 | } else { |
| 4714 | ++it; |
| 4715 | } |
| 4716 | } |
| 4717 | } |
| 4718 | |
| 4719 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4720 | uint32_t remote_ssrc) { |
| 4721 | rtc::scoped_refptr<DataChannel> channel( |
| 4722 | InternalCreateDataChannel(label, nullptr)); |
| 4723 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4724 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4725 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4726 | return; |
| 4727 | } |
| 4728 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4729 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4730 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4731 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4735 | const std::string& label, |
| 4736 | const InternalDataChannelInit* config) { |
| 4737 | if (IsClosed()) { |
| 4738 | return nullptr; |
| 4739 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4740 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4741 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4742 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4743 | return nullptr; |
| 4744 | } |
| 4745 | InternalDataChannelInit new_config = |
| 4746 | config ? (*config) : InternalDataChannelInit(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4747 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4748 | if (new_config.id < 0) { |
| 4749 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4750 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4751 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4752 | RTC_LOG(LS_ERROR) |
| 4753 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4754 | return nullptr; |
| 4755 | } |
| 4756 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4757 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4758 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4759 | return nullptr; |
| 4760 | } |
| 4761 | } |
| 4762 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4763 | rtc::scoped_refptr<DataChannel> channel( |
| 4764 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4765 | if (!channel) { |
| 4766 | sid_allocator_.ReleaseSid(new_config.id); |
| 4767 | return nullptr; |
| 4768 | } |
| 4769 | |
| 4770 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4771 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4772 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4773 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4774 | return nullptr; |
| 4775 | } |
| 4776 | rtp_data_channels_[channel->label()] = channel; |
| 4777 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4778 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4779 | sctp_data_channels_.push_back(channel); |
| 4780 | channel->SignalClosed.connect(this, |
| 4781 | &PeerConnection::OnSctpDataChannelClosed); |
| 4782 | } |
| 4783 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4784 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4785 | return channel; |
| 4786 | } |
| 4787 | |
| 4788 | bool PeerConnection::HasDataChannels() const { |
| 4789 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4790 | } |
| 4791 | |
| 4792 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4793 | for (const auto& channel : sctp_data_channels_) { |
| 4794 | if (channel->id() < 0) { |
| 4795 | int sid; |
| 4796 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4797 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4798 | continue; |
| 4799 | } |
| 4800 | channel->SetSctpSid(sid); |
| 4801 | } |
| 4802 | } |
| 4803 | } |
| 4804 | |
| 4805 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4806 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4807 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4808 | ++it) { |
| 4809 | if (it->get() == channel) { |
| 4810 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 4811 | // After the closing procedure is done, it's safe to use this ID for |
| 4812 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4813 | sid_allocator_.ReleaseSid(channel->id()); |
| 4814 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4815 | // Since this method is triggered by a signal from the DataChannel, |
| 4816 | // we can't free it directly here; we need to free it asynchronously. |
| 4817 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4818 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4819 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4820 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4821 | return; |
| 4822 | } |
| 4823 | } |
| 4824 | } |
| 4825 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4826 | void PeerConnection::OnDataChannelDestroyed() { |
| 4827 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4828 | // DataChannel may callback to us and try to modify the list. |
| 4829 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4830 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4831 | for (const auto& kv : temp_rtp_dcs) { |
| 4832 | kv.second->OnTransportChannelDestroyed(); |
| 4833 | } |
| 4834 | |
| 4835 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4836 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4837 | for (const auto& channel : temp_sctp_dcs) { |
| 4838 | channel->OnTransportChannelDestroyed(); |
| 4839 | } |
| 4840 | } |
| 4841 | |
| 4842 | void PeerConnection::OnDataChannelOpenMessage( |
| 4843 | const std::string& label, |
| 4844 | const InternalDataChannelInit& config) { |
| 4845 | rtc::scoped_refptr<DataChannel> channel( |
| 4846 | InternalCreateDataChannel(label, &config)); |
| 4847 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4848 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4849 | return; |
| 4850 | } |
| 4851 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4852 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4853 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4854 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 4855 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4858 | bool PeerConnection::HandleOpenMessage_s( |
| 4859 | const cricket::ReceiveDataParams& params, |
| 4860 | const rtc::CopyOnWriteBuffer& buffer) { |
| 4861 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 4862 | // Received OPEN message; parse and signal that a new data channel should |
| 4863 | // be created. |
| 4864 | std::string label; |
| 4865 | InternalDataChannelInit config; |
| 4866 | config.id = params.ssrc; |
| 4867 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 4868 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 4869 | << params.ssrc; |
| 4870 | return true; |
| 4871 | } |
| 4872 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 4873 | OnDataChannelOpenMessage(label, config); |
| 4874 | return true; |
| 4875 | } |
| 4876 | return false; |
| 4877 | } |
| 4878 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4879 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4880 | PeerConnection::GetAudioTransceiver() const { |
| 4881 | // This method only works with Plan B SDP, where there is a single |
| 4882 | // audio/video transceiver. |
| 4883 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4884 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4885 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4886 | return transceiver; |
| 4887 | } |
| 4888 | } |
| 4889 | RTC_NOTREACHED(); |
| 4890 | return nullptr; |
| 4891 | } |
| 4892 | |
| 4893 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4894 | PeerConnection::GetVideoTransceiver() const { |
| 4895 | // This method only works with Plan B SDP, where there is a single |
| 4896 | // audio/video transceiver. |
| 4897 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4898 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4899 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4900 | return transceiver; |
| 4901 | } |
| 4902 | } |
| 4903 | RTC_NOTREACHED(); |
| 4904 | return nullptr; |
| 4905 | } |
| 4906 | |
| 4907 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4908 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4909 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4910 | switch (type) { |
| 4911 | case cricket::MEDIA_TYPE_AUDIO: |
| 4912 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4913 | case cricket::MEDIA_TYPE_VIDEO: |
| 4914 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4915 | case cricket::MEDIA_TYPE_DATA: |
| 4916 | return false; |
| 4917 | } |
| 4918 | RTC_NOTREACHED(); |
| 4919 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4920 | } |
| 4921 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4922 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4923 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 4924 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4925 | for (auto sender : transceiver->internal()->senders()) { |
| 4926 | if (sender->track() == track) { |
| 4927 | return sender; |
| 4928 | } |
| 4929 | } |
| 4930 | } |
| 4931 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4932 | } |
| 4933 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4934 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4935 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 4936 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4937 | for (auto sender : transceiver->internal()->senders()) { |
| 4938 | if (sender->id() == sender_id) { |
| 4939 | return sender; |
| 4940 | } |
| 4941 | } |
| 4942 | } |
| 4943 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4944 | } |
| 4945 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4946 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4947 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 4948 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4949 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4950 | if (receiver->id() == receiver_id) { |
| 4951 | return receiver; |
| 4952 | } |
| 4953 | } |
| 4954 | } |
| 4955 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4956 | } |
| 4957 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4958 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4959 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4960 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4961 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4962 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4963 | ? &remote_audio_sender_infos_ |
| 4964 | : &remote_video_sender_infos_; |
| 4965 | } |
| 4966 | |
| 4967 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4968 | cricket::MediaType media_type) { |
| 4969 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4970 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4971 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4972 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4973 | } |
| 4974 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4975 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4976 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4977 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4978 | const std::string sender_id) const { |
| 4979 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4980 | if (sender_info.stream_id == stream_id && |
| 4981 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4982 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4983 | } |
| 4984 | } |
| 4985 | return nullptr; |
| 4986 | } |
| 4987 | |
| 4988 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4989 | for (const auto& channel : sctp_data_channels_) { |
| 4990 | if (channel->id() == sid) { |
| 4991 | return channel; |
| 4992 | } |
| 4993 | } |
| 4994 | return nullptr; |
| 4995 | } |
| 4996 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4997 | bool PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 4998 | const cricket::ServerAddresses& stun_servers, |
| 4999 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5000 | const RTCConfiguration& configuration) { |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 5001 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5002 | // To handle both internal and externally created port allocator, we will |
| 5003 | // enable BUNDLE here. |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5004 | port_allocator_flags_ = port_allocator_->flags(); |
| 5005 | port_allocator_flags_ |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5006 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5007 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5008 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5009 | // by experiment. |
| 5010 | if (configuration.disable_ipv6) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5011 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 5012 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 5013 | .find("Disabled") == 0) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5014 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5017 | if (configuration.disable_ipv6_on_wifi) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5018 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5019 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5020 | } |
| 5021 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5022 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5023 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5024 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5025 | } |
| 5026 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5027 | if (configuration.candidate_network_policy == |
| 5028 | kCandidateNetworkPolicyLowCost) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5029 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5030 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5031 | } |
| 5032 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5033 | if (configuration.disable_link_local_networks) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5034 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5035 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5036 | } |
| 5037 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5038 | port_allocator_->set_flags(port_allocator_flags_); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5039 | // No step delay is used while allocating ports. |
| 5040 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 5041 | port_allocator_->set_candidate_filter( |
| 5042 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 5043 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5044 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5045 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5046 | for (auto& turn_server : turn_servers_copy) { |
| 5047 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 5048 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5049 | // Call this last since it may create pooled allocator sessions using the |
| 5050 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5051 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5052 | stun_servers, std::move(turn_servers_copy), |
| 5053 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 5054 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5055 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5056 | return true; |
| 5057 | } |
| 5058 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 5059 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5060 | const cricket::ServerAddresses& stun_servers, |
| 5061 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5062 | IceTransportsType type, |
| 5063 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 5064 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5065 | webrtc::TurnCustomizer* turn_customizer, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5066 | absl::optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5067 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5068 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5069 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5070 | // size is not allowed, and changing the set of ICE servers will not result |
| 5071 | // in new candidates being gathered. |
| 5072 | if (local_description()) { |
| 5073 | port_allocator_->FreezeCandidatePool(); |
| 5074 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5075 | // Add the custom tls turn servers if they exist. |
| 5076 | auto turn_servers_copy = turn_servers; |
| 5077 | for (auto& turn_server : turn_servers_copy) { |
| 5078 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5079 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5080 | // Call this last since it may create pooled allocator sessions using the |
| 5081 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 5082 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5083 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 5084 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5085 | } |
| 5086 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 5087 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5088 | return factory_->channel_manager(); |
| 5089 | } |
| 5090 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 5091 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5092 | std::unique_ptr<RtcEventLogOutput> output, |
| 5093 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5094 | if (!event_log_) { |
| 5095 | return false; |
| 5096 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5097 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5098 | } |
| 5099 | |
| 5100 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5101 | if (event_log_) { |
| 5102 | event_log_->StopLogging(); |
| 5103 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5104 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 5105 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5106 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5107 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 5108 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5109 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5110 | if (channel && channel->content_name() == content_name) { |
| 5111 | return channel; |
| 5112 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5113 | } |
| 5114 | if (rtp_data_channel() && |
| 5115 | rtp_data_channel()->content_name() == content_name) { |
| 5116 | return rtp_data_channel(); |
| 5117 | } |
| 5118 | return nullptr; |
| 5119 | } |
| 5120 | |
| 5121 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5122 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5123 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5124 | RTC_LOG(LS_INFO) |
| 5125 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5126 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5127 | return false; |
| 5128 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5129 | if (!sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5130 | 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] | 5131 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5132 | return false; |
| 5133 | } |
| 5134 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5135 | absl::optional<rtc::SSLRole> dtls_role; |
| 5136 | if (sctp_mid_) { |
| 5137 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 5138 | } else if (is_caller_) { |
| 5139 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5140 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5141 | if (dtls_role) { |
| 5142 | *role = *dtls_role; |
| 5143 | return true; |
| 5144 | } |
| 5145 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5149 | rtc::SSLRole* role) { |
| 5150 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5151 | RTC_LOG(LS_INFO) |
| 5152 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5153 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5154 | return false; |
| 5155 | } |
| 5156 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5157 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5158 | if (dtls_role) { |
| 5159 | *role = *dtls_role; |
| 5160 | return true; |
| 5161 | } |
| 5162 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5163 | } |
| 5164 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5165 | void PeerConnection::SetSessionError(SessionError error, |
| 5166 | const std::string& error_desc) { |
| 5167 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5168 | if (error != session_error_) { |
| 5169 | session_error_ = error; |
| 5170 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5171 | } |
| 5172 | } |
| 5173 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5174 | RTCError PeerConnection::UpdateSessionState( |
| 5175 | SdpType type, |
| 5176 | cricket::ContentSource source, |
| 5177 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5178 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5179 | |
| 5180 | // If there's already a pending error then no state transition should happen. |
| 5181 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5182 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5183 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5184 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5185 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5186 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | // Update the signaling state according to the specified state machine (see |
| 5190 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5191 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5192 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5193 | ? PeerConnectionInterface::kHaveLocalOffer |
| 5194 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5195 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5196 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5197 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 5198 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5199 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5200 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5201 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 5202 | } |
| 5203 | |
| 5204 | // Update internal objects according to the session description's media |
| 5205 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5206 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5207 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 5208 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5209 | } |
| 5210 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5211 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5212 | } |
| 5213 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5214 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5215 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5216 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5217 | const SessionDescriptionInterface* sdesc = |
| 5218 | (source == cricket::CS_LOCAL ? local_description() |
| 5219 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5220 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5221 | |
| 5222 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 5223 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5224 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5225 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5226 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5227 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5228 | continue; |
| 5229 | } |
| 5230 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5231 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5232 | if (!content_desc) { |
| 5233 | continue; |
| 5234 | } |
| 5235 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5236 | bool success = (source == cricket::CS_LOCAL) |
| 5237 | ? channel->SetLocalContent(content_desc, type, &error) |
| 5238 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5239 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5240 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 5245 | if (rtp_data_channel_) { |
| 5246 | const ContentInfo* data_content = |
| 5247 | cricket::GetFirstDataContent(sdesc->description()); |
| 5248 | if (data_content && !data_content->rejected) { |
| 5249 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5250 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5251 | if (data_desc) { |
| 5252 | std::string error; |
| 5253 | bool success = |
| 5254 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5255 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5256 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5257 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5258 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5259 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5260 | } |
| 5261 | } |
| 5262 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5263 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5264 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 5265 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 5266 | if (sctp_transport_ && local_description() && remote_description() && |
| 5267 | cricket::GetFirstDataContent(local_description()->description()) && |
| 5268 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5269 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5270 | RTC_FROM_HERE, |
| 5271 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5272 | if (!success) { |
| 5273 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5274 | "Failed to push down SCTP parameters."); |
| 5275 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5276 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5277 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5278 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 5282 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5283 | RTC_DCHECK(local_description()); |
| 5284 | RTC_DCHECK(remote_description()); |
| 5285 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 5286 | // When we support "max-message-size", that would also be pushed down here. |
| 5287 | return sctp_transport_->Start( |
| 5288 | GetSctpPort(local_description()->description()), |
| 5289 | GetSctpPort(remote_description()->description())); |
| 5290 | } |
| 5291 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5292 | RTCError PeerConnection::PushdownTransportDescription( |
| 5293 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5294 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5295 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5296 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5297 | if (source == cricket::CS_LOCAL) { |
| 5298 | const SessionDescriptionInterface* sdesc = local_description(); |
| 5299 | RTC_DCHECK(sdesc); |
| 5300 | return transport_controller_->SetLocalDescription(type, |
| 5301 | sdesc->description()); |
| 5302 | } else { |
| 5303 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 5304 | RTC_DCHECK(sdesc); |
| 5305 | return transport_controller_->SetRemoteDescription(type, |
| 5306 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5307 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | bool PeerConnection::GetTransportDescription( |
| 5311 | const SessionDescription* description, |
| 5312 | const std::string& content_name, |
| 5313 | cricket::TransportDescription* tdesc) { |
| 5314 | if (!description || !tdesc) { |
| 5315 | return false; |
| 5316 | } |
| 5317 | const TransportInfo* transport_info = |
| 5318 | description->GetTransportInfoByName(content_name); |
| 5319 | if (!transport_info) { |
| 5320 | return false; |
| 5321 | } |
| 5322 | *tdesc = transport_info->description; |
| 5323 | return true; |
| 5324 | } |
| 5325 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5326 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 5327 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 5328 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5329 | switch (config.continual_gathering_policy) { |
| 5330 | case PeerConnectionInterface::GATHER_ONCE: |
| 5331 | gathering_policy = cricket::GATHER_ONCE; |
| 5332 | break; |
| 5333 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 5334 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 5335 | break; |
| 5336 | default: |
| 5337 | RTC_NOTREACHED(); |
| 5338 | gathering_policy = cricket::GATHER_ONCE; |
| 5339 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5340 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5341 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5342 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 5343 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5344 | ice_config.prioritize_most_likely_candidate_pairs = |
| 5345 | config.prioritize_most_likely_ice_candidate_pairs; |
| 5346 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5347 | RTCConfigurationToIceConfigOptionalInt( |
| 5348 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5349 | ice_config.continual_gathering_policy = gathering_policy; |
| 5350 | ice_config.presume_writable_when_fully_relayed = |
| 5351 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 5352 | ice_config.ice_check_interval_strong_connectivity = |
| 5353 | config.ice_check_interval_strong_connectivity; |
| 5354 | ice_config.ice_check_interval_weak_connectivity = |
| 5355 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5356 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-29 23:08:51 -0800 | [diff] [blame] | 5357 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 5358 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 5359 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5360 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5361 | ice_config.regather_all_networks_interval_range = |
| 5362 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5363 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5364 | return ice_config; |
| 5365 | } |
| 5366 | |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5367 | static absl::string_view GetTrackIdBySsrc( |
| 5368 | const SessionDescriptionInterface* session_description, |
| 5369 | uint32_t ssrc) { |
| 5370 | if (!session_description) { |
| 5371 | return {}; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5372 | } |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5373 | for (const cricket::ContentInfo& content : |
| 5374 | session_description->description()->contents()) { |
| 5375 | const cricket::MediaContentDescription& media = |
| 5376 | *content.media_description(); |
| 5377 | if (media.type() == cricket::MEDIA_TYPE_AUDIO || |
| 5378 | media.type() == cricket::MEDIA_TYPE_VIDEO) { |
| 5379 | const cricket::StreamParams* stream_params = |
| 5380 | cricket::GetStreamBySsrc(media.streams(), ssrc); |
| 5381 | if (stream_params) { |
| 5382 | return stream_params->id; |
| 5383 | } |
| 5384 | } |
| 5385 | } |
| 5386 | return {}; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5389 | absl::string_view PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc) { |
| 5390 | return GetTrackIdBySsrc(local_description(), ssrc); |
| 5391 | } |
| 5392 | |
| 5393 | absl::string_view PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc) { |
| 5394 | return GetTrackIdBySsrc(remote_description(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5395 | } |
| 5396 | |
| 5397 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5398 | const rtc::CopyOnWriteBuffer& payload, |
| 5399 | cricket::SendDataResult* result) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5400 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
| 5401 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_, " |
| 5402 | "sctp_transport_, and media_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5403 | return false; |
| 5404 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5405 | if (media_transport_) { |
| 5406 | SendDataParams send_params; |
| 5407 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 5408 | send_params.ordered = params.ordered; |
| 5409 | if (params.max_rtx_count >= 0) { |
| 5410 | send_params.max_rtx_count = params.max_rtx_count; |
| 5411 | } else if (params.max_rtx_ms >= 0) { |
| 5412 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 5413 | } |
| 5414 | return media_transport_->SendData(params.sid, send_params, payload).ok(); |
| 5415 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5416 | return rtp_data_channel_ |
| 5417 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5418 | : network_thread()->Invoke<bool>( |
| 5419 | RTC_FROM_HERE, |
| 5420 | Bind(&cricket::SctpTransportInternal::SendData, |
| 5421 | sctp_transport_.get(), params, payload, result)); |
| 5422 | } |
| 5423 | |
| 5424 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5425 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5426 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5427 | // Don't log an error here, because DataChannels are expected to call |
| 5428 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5429 | // whether or not the underlying transport is ready. |
| 5430 | return false; |
| 5431 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5432 | if (media_transport_) { |
| 5433 | SignalMediaTransportWritable_s.connect(webrtc_data_channel, |
| 5434 | &DataChannel::OnChannelReady); |
| 5435 | SignalMediaTransportReceivedData_s.connect(webrtc_data_channel, |
| 5436 | &DataChannel::OnDataReceived); |
| 5437 | SignalMediaTransportChannelClosing_s.connect( |
| 5438 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5439 | SignalMediaTransportChannelClosed_s.connect( |
| 5440 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
| 5441 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5442 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5443 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5444 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5445 | &DataChannel::OnDataReceived); |
| 5446 | } else { |
| 5447 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5448 | &DataChannel::OnChannelReady); |
| 5449 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5450 | &DataChannel::OnDataReceived); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5451 | SignalSctpClosingProcedureStartedRemotely.connect( |
| 5452 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5453 | SignalSctpClosingProcedureComplete.connect( |
| 5454 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5455 | } |
| 5456 | return true; |
| 5457 | } |
| 5458 | |
| 5459 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5460 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5461 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5462 | RTC_LOG(LS_ERROR) |
| 5463 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5464 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5465 | return; |
| 5466 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5467 | if (media_transport_) { |
| 5468 | SignalMediaTransportWritable_s.disconnect(webrtc_data_channel); |
| 5469 | SignalMediaTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 5470 | SignalMediaTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 5471 | SignalMediaTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 5472 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5473 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5474 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5475 | } else { |
| 5476 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5477 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5478 | SignalSctpClosingProcedureStartedRemotely.disconnect(webrtc_data_channel); |
| 5479 | SignalSctpClosingProcedureComplete.disconnect(webrtc_data_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5480 | } |
| 5481 | } |
| 5482 | |
| 5483 | void PeerConnection::AddSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5484 | if (media_transport_) { |
| 5485 | // No-op. Media transport does not need to add streams. |
| 5486 | return; |
| 5487 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5488 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5489 | RTC_LOG(LS_ERROR) |
| 5490 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5491 | return; |
| 5492 | } |
| 5493 | network_thread()->Invoke<void>( |
| 5494 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 5495 | sctp_transport_.get(), sid)); |
| 5496 | } |
| 5497 | |
| 5498 | void PeerConnection::RemoveSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5499 | if (media_transport_) { |
| 5500 | media_transport_->CloseChannel(sid); |
| 5501 | return; |
| 5502 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5503 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5504 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5505 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5506 | return; |
| 5507 | } |
| 5508 | network_thread()->Invoke<void>( |
| 5509 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 5510 | sctp_transport_.get(), sid)); |
| 5511 | } |
| 5512 | |
| 5513 | bool PeerConnection::ReadyToSendData() const { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5514 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5515 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5516 | (media_transport_ && media_transport_ready_to_send_data_) || |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5517 | sctp_ready_to_send_data_; |
| 5518 | } |
| 5519 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5520 | void PeerConnection::OnDataReceived(int channel_id, |
| 5521 | DataMessageType type, |
| 5522 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5523 | cricket::ReceiveDataParams params; |
| 5524 | params.sid = channel_id; |
| 5525 | params.type = ToCricketDataMessageType(type); |
| 5526 | media_transport_invoker_->AsyncInvoke<void>( |
| 5527 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 5528 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5529 | if (!HandleOpenMessage_s(params, buffer)) { |
| 5530 | SignalMediaTransportReceivedData_s(params, buffer); |
| 5531 | } |
| 5532 | }); |
| 5533 | } |
| 5534 | |
| 5535 | void PeerConnection::OnChannelClosing(int channel_id) { |
| 5536 | media_transport_invoker_->AsyncInvoke<void>( |
| 5537 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5538 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5539 | SignalMediaTransportChannelClosing_s(channel_id); |
| 5540 | }); |
| 5541 | } |
| 5542 | |
| 5543 | void PeerConnection::OnChannelClosed(int channel_id) { |
| 5544 | media_transport_invoker_->AsyncInvoke<void>( |
| 5545 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5546 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5547 | SignalMediaTransportChannelClosed_s(channel_id); |
| 5548 | }); |
| 5549 | } |
| 5550 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5551 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5552 | if (sctp_mid_ && transport_controller_) { |
| 5553 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5554 | if (dtls_transport) { |
| 5555 | return dtls_transport->transport_name(); |
| 5556 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5557 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5558 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5559 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5560 | } |
| 5561 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5562 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5563 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5564 | network_thread()->Invoke<void>( |
| 5565 | RTC_FROM_HERE, |
| 5566 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 5567 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5568 | return candidate_states_list; |
| 5569 | } |
| 5570 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5571 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5572 | const { |
| 5573 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 5574 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5575 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5576 | if (channel) { |
| 5577 | transport_names_by_mid[channel->content_name()] = |
| 5578 | channel->transport_name(); |
| 5579 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5580 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5581 | if (rtp_data_channel_) { |
| 5582 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5583 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5584 | } |
| 5585 | if (sctp_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5586 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5587 | RTC_DCHECK(transport_name); |
| 5588 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5589 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5590 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5591 | } |
| 5592 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5593 | std::map<std::string, cricket::TransportStats> |
| 5594 | PeerConnection::GetTransportStatsByNames( |
| 5595 | const std::set<std::string>& transport_names) { |
| 5596 | if (!network_thread()->IsCurrent()) { |
| 5597 | return network_thread() |
| 5598 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5599 | RTC_FROM_HERE, |
| 5600 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5601 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5602 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5603 | for (const std::string& transport_name : transport_names) { |
| 5604 | cricket::TransportStats transport_stats; |
| 5605 | bool success = |
| 5606 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5607 | if (success) { |
| 5608 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5609 | } else { |
| 5610 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5611 | << transport_name; |
| 5612 | } |
| 5613 | } |
| 5614 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | bool PeerConnection::GetLocalCertificate( |
| 5618 | const std::string& transport_name, |
| 5619 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5620 | if (!certificate) { |
| 5621 | return false; |
| 5622 | } |
| 5623 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 5624 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5625 | } |
| 5626 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5627 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5628 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5629 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5630 | } |
| 5631 | |
| 5632 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5633 | return data_channel_type_; |
| 5634 | } |
| 5635 | |
| 5636 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5637 | return pending_ice_restarts_.find(content_name) != |
| 5638 | pending_ice_restarts_.end(); |
| 5639 | } |
| 5640 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5641 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5642 | return transport_controller_->NeedsIceRestart(content_name); |
| 5643 | } |
| 5644 | |
| 5645 | void PeerConnection::OnCertificateReady( |
| 5646 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5647 | transport_controller_->SetLocalCertificate(certificate); |
| 5648 | } |
| 5649 | |
| 5650 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5651 | SetSessionError(SessionError::kTransport, |
| 5652 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5653 | } |
| 5654 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 5655 | void PeerConnection::OnTransportControllerConnectionState( |
| 5656 | cricket::IceConnectionState state) { |
| 5657 | switch (state) { |
| 5658 | case cricket::kIceConnectionConnecting: |
| 5659 | // If the current state is Connected or Completed, then there were |
| 5660 | // writable channels but now there are not, so the next state must |
| 5661 | // be Disconnected. |
| 5662 | // kIceConnectionConnecting is currently used as the default, |
| 5663 | // un-connected state by the TransportController, so its only use is |
| 5664 | // detecting disconnections. |
| 5665 | if (ice_connection_state_ == |
| 5666 | PeerConnectionInterface::kIceConnectionConnected || |
| 5667 | ice_connection_state_ == |
| 5668 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5669 | SetIceConnectionState( |
| 5670 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5671 | } |
| 5672 | break; |
| 5673 | case cricket::kIceConnectionFailed: |
| 5674 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5675 | break; |
| 5676 | case cricket::kIceConnectionConnected: |
| 5677 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 5678 | "all transports are writable."; |
| 5679 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5680 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 5681 | break; |
| 5682 | case cricket::kIceConnectionCompleted: |
| 5683 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 5684 | "all transports are complete."; |
| 5685 | if (ice_connection_state_ != |
| 5686 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5687 | // If jumping directly from "checking" to "connected", |
| 5688 | // signal "connected" first. |
| 5689 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5690 | } |
| 5691 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5692 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 5693 | ReportTransportStats(); |
| 5694 | break; |
| 5695 | default: |
| 5696 | RTC_NOTREACHED(); |
| 5697 | } |
| 5698 | } |
| 5699 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5700 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5701 | const std::string& transport_name, |
| 5702 | const cricket::Candidates& candidates) { |
| 5703 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5704 | int sdp_mline_index; |
| 5705 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5706 | RTC_LOG(LS_ERROR) |
| 5707 | << "OnTransportControllerCandidatesGathered: content name " |
| 5708 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5709 | return; |
| 5710 | } |
| 5711 | |
| 5712 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5713 | citer != candidates.end(); ++citer) { |
| 5714 | // Use transport_name as the candidate media id. |
| 5715 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5716 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5717 | if (local_description()) { |
| 5718 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5719 | } |
| 5720 | OnIceCandidate(std::move(candidate)); |
| 5721 | } |
| 5722 | } |
| 5723 | |
| 5724 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5725 | const std::vector<cricket::Candidate>& candidates) { |
| 5726 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5727 | // Sanity check. |
| 5728 | for (const cricket::Candidate& candidate : candidates) { |
| 5729 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5730 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5731 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5732 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5733 | return; |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | if (local_description()) { |
| 5738 | mutable_local_description()->RemoveCandidates(candidates); |
| 5739 | } |
| 5740 | OnIceCandidatesRemoved(candidates); |
| 5741 | } |
| 5742 | |
| 5743 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5744 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5745 | RTC_HISTOGRAM_ENUMERATION( |
| 5746 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 5747 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5748 | } |
| 5749 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5750 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 5751 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5752 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5753 | if (channel && !channel->enabled()) { |
| 5754 | channel->Enable(true); |
| 5755 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5756 | } |
| 5757 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5758 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5759 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5760 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5761 | } |
| 5762 | |
| 5763 | // Returns the media index for a local ice candidate given the content name. |
| 5764 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5765 | const std::string& content_name, |
| 5766 | int* sdp_mline_index) { |
| 5767 | if (!local_description() || !sdp_mline_index) { |
| 5768 | return false; |
| 5769 | } |
| 5770 | |
| 5771 | bool content_found = false; |
| 5772 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5773 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5774 | if (contents[index].name == content_name) { |
| 5775 | *sdp_mline_index = static_cast<int>(index); |
| 5776 | content_found = true; |
| 5777 | break; |
| 5778 | } |
| 5779 | } |
| 5780 | return content_found; |
| 5781 | } |
| 5782 | |
| 5783 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5784 | const SessionDescriptionInterface* remote_desc) { |
| 5785 | if (!remote_desc) { |
| 5786 | return true; |
| 5787 | } |
| 5788 | bool ret = true; |
| 5789 | |
| 5790 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5791 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5792 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5793 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5794 | bool valid = false; |
| 5795 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5796 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5797 | RTC_LOG(LS_INFO) |
| 5798 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5799 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5800 | } |
| 5801 | continue; |
| 5802 | } |
| 5803 | ret = UseCandidate(candidate); |
| 5804 | if (!ret) { |
| 5805 | break; |
| 5806 | } |
| 5807 | } |
| 5808 | } |
| 5809 | return ret; |
| 5810 | } |
| 5811 | |
| 5812 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5813 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5814 | size_t remote_content_size = |
| 5815 | remote_description()->description()->contents().size(); |
| 5816 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5817 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5818 | return false; |
| 5819 | } |
| 5820 | |
| 5821 | cricket::ContentInfo content = |
| 5822 | remote_description()->description()->contents()[mediacontent_index]; |
| 5823 | std::vector<cricket::Candidate> candidates; |
| 5824 | candidates.push_back(candidate->candidate()); |
| 5825 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5826 | RTCError error = |
| 5827 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 5828 | if (error.ok()) { |
| 5829 | // Candidates successfully submitted for checking. |
| 5830 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5831 | ice_connection_state_ == |
| 5832 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5833 | // If state is New, then the session has just gotten its first remote ICE |
| 5834 | // candidates, so go to Checking. |
| 5835 | // If state is Disconnected, the session is re-using old candidates or |
| 5836 | // receiving additional ones, so go to Checking. |
| 5837 | // If state is Connected, stay Connected. |
| 5838 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5839 | // newly added transport, then the state actually _should_ move to |
| 5840 | // checking. Add a way to distinguish that case. |
| 5841 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5842 | } |
| 5843 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 5844 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5845 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5846 | } |
| 5847 | return true; |
| 5848 | } |
| 5849 | |
| 5850 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5851 | // Destroy video channel first since it may have a pointer to the |
| 5852 | // voice channel. |
| 5853 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5854 | if (!video_info || video_info->rejected) { |
| 5855 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5856 | } |
| 5857 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5858 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5859 | if (!audio_info || audio_info->rejected) { |
| 5860 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5861 | } |
| 5862 | |
| 5863 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5864 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5865 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5866 | } |
| 5867 | } |
| 5868 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5869 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5870 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5871 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5872 | if (configuration_.bundle_policy == |
| 5873 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5874 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5875 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5876 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5877 | "max-bundle configured but session description " |
| 5878 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5879 | } |
| 5880 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5881 | return std::move(bundle_group); |
| 5882 | } |
| 5883 | |
| 5884 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5885 | // Creating the media channels. Transports should already have been created |
| 5886 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5887 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5888 | if (voice && !voice->rejected && |
| 5889 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5890 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5891 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5892 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5893 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5894 | } |
| 5895 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5896 | } |
| 5897 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5898 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5899 | if (video && !video->rejected && |
| 5900 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5901 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5902 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5903 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5904 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5905 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5906 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5907 | } |
| 5908 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5909 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5910 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5911 | !rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5912 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5913 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5914 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5918 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5919 | } |
| 5920 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5921 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5922 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5923 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5924 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 5925 | MediaTransportInterface* media_transport = nullptr; |
| 5926 | if (configuration_.use_media_transport) { |
| 5927 | media_transport = GetMediaTransport(mid); |
| 5928 | } |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5929 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5930 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5931 | call_.get(), configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 5932 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
| 5933 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5934 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5935 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5936 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5937 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5938 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5939 | voice_channel->SignalSentPacket.connect(this, |
| 5940 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5941 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5942 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5943 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5944 | } |
| 5945 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5946 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5947 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5948 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5949 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 5950 | MediaTransportInterface* media_transport = nullptr; |
| 5951 | if (configuration_.use_media_transport) { |
| 5952 | media_transport = GetMediaTransport(mid); |
| 5953 | } |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5954 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5955 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 5956 | call_.get(), configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 5957 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
| 5958 | video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5959 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5960 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5961 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5962 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5963 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5964 | video_channel->SignalSentPacket.connect(this, |
| 5965 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5966 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5967 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5968 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5969 | } |
| 5970 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5971 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5972 | switch (data_channel_type_) { |
| 5973 | case cricket::DCT_MEDIA_TRANSPORT: |
| 5974 | if (network_thread()->Invoke<bool>( |
| 5975 | RTC_FROM_HERE, |
| 5976 | rtc::Bind(&PeerConnection::SetupMediaTransportForDataChannels_n, |
| 5977 | this, mid))) { |
| 5978 | for (const auto& channel : sctp_data_channels_) { |
| 5979 | channel->OnTransportChannelCreated(); |
| 5980 | } |
| 5981 | return true; |
| 5982 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5983 | return false; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5984 | case cricket::DCT_SCTP: |
| 5985 | if (!sctp_factory_) { |
| 5986 | RTC_LOG(LS_ERROR) |
| 5987 | << "Trying to create SCTP transport, but didn't compile with " |
| 5988 | "SCTP support (HAVE_SCTP)"; |
| 5989 | return false; |
| 5990 | } |
| 5991 | if (!network_thread()->Invoke<bool>( |
| 5992 | RTC_FROM_HERE, |
| 5993 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
| 5994 | return false; |
| 5995 | } |
| 5996 | for (const auto& channel : sctp_data_channels_) { |
| 5997 | channel->OnTransportChannelCreated(); |
| 5998 | } |
| 5999 | return true; |
| 6000 | case cricket::DCT_RTP: |
| 6001 | default: |
| 6002 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 6003 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 6004 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
| 6005 | SrtpRequired(), GetCryptoOptions()); |
| 6006 | if (!rtp_data_channel_) { |
| 6007 | return false; |
| 6008 | } |
| 6009 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 6010 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6011 | rtp_data_channel_->SignalSentPacket.connect( |
| 6012 | this, &PeerConnection::OnSentPacket_w); |
| 6013 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 6014 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6015 | } |
| 6016 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6017 | return true; |
| 6018 | } |
| 6019 | |
| 6020 | Call::Stats PeerConnection::GetCallStats() { |
| 6021 | if (!worker_thread()->IsCurrent()) { |
| 6022 | return worker_thread()->Invoke<Call::Stats>( |
| 6023 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6024 | } |
| 6025 | if (call_) { |
| 6026 | return call_->GetStats(); |
| 6027 | } else { |
| 6028 | return Call::Stats(); |
| 6029 | } |
| 6030 | } |
| 6031 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6032 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6033 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6034 | RTC_DCHECK(sctp_factory_); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6035 | cricket::DtlsTransportInternal* dtls_transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6036 | transport_controller_->GetDtlsTransport(mid); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6037 | RTC_DCHECK(dtls_transport); |
| 6038 | sctp_transport_ = sctp_factory_->CreateSctpTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6039 | RTC_DCHECK(sctp_transport_); |
| 6040 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 6041 | sctp_transport_->SignalReadyToSendData.connect( |
| 6042 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 6043 | sctp_transport_->SignalDataReceived.connect( |
| 6044 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6045 | // TODO(deadbeef): All we do here is AsyncInvoke to fire the signal on |
| 6046 | // another thread. Would be nice if there was a helper class similar to |
| 6047 | // sigslot::repeater that did this for us, eliminating a bunch of boilerplate |
| 6048 | // code. |
| 6049 | sctp_transport_->SignalClosingProcedureStartedRemotely.connect( |
| 6050 | this, &PeerConnection::OnSctpClosingProcedureStartedRemotely_n); |
| 6051 | sctp_transport_->SignalClosingProcedureComplete.connect( |
| 6052 | this, &PeerConnection::OnSctpClosingProcedureComplete_n); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6053 | sctp_mid_ = mid; |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6054 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6055 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6056 | } |
| 6057 | |
| 6058 | void PeerConnection::DestroySctpTransport_n() { |
| 6059 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6060 | sctp_transport_.reset(nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6061 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6062 | sctp_invoker_.reset(nullptr); |
| 6063 | sctp_ready_to_send_data_ = false; |
| 6064 | } |
| 6065 | |
| 6066 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 6067 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6068 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6069 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6070 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6071 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6072 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6073 | // pending tasks will be cleared. |
| 6074 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 6075 | OnSctpTransportReadyToSendData_s(true); |
| 6076 | }); |
| 6077 | } |
| 6078 | |
| 6079 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 6080 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6081 | sctp_ready_to_send_data_ = ready; |
| 6082 | SignalSctpReadyToSendData(ready); |
| 6083 | } |
| 6084 | |
| 6085 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 6086 | const cricket::ReceiveDataParams& params, |
| 6087 | const rtc::CopyOnWriteBuffer& payload) { |
| 6088 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6089 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6090 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6091 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6092 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6093 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6094 | // pending tasks will be cleared. |
| 6095 | sctp_invoker_->AsyncInvoke<void>( |
| 6096 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 6097 | OnSctpTransportDataReceived_s(params, payload); |
| 6098 | }); |
| 6099 | } |
| 6100 | |
| 6101 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 6102 | const cricket::ReceiveDataParams& params, |
| 6103 | const rtc::CopyOnWriteBuffer& payload) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6104 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6105 | if (!HandleOpenMessage_s(params, payload)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6106 | SignalSctpDataReceived(params, payload); |
| 6107 | } |
| 6108 | } |
| 6109 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6110 | void PeerConnection::OnSctpClosingProcedureStartedRemotely_n(int sid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6111 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6112 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6113 | sctp_invoker_->AsyncInvoke<void>( |
| 6114 | RTC_FROM_HERE, signaling_thread(), |
| 6115 | rtc::Bind(&sigslot::signal1<int>::operator(), |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6116 | &SignalSctpClosingProcedureStartedRemotely, sid)); |
| 6117 | } |
| 6118 | |
| 6119 | void PeerConnection::OnSctpClosingProcedureComplete_n(int sid) { |
| 6120 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6121 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6122 | sctp_invoker_->AsyncInvoke<void>( |
| 6123 | RTC_FROM_HERE, signaling_thread(), |
| 6124 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 6125 | &SignalSctpClosingProcedureComplete, sid)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6126 | } |
| 6127 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6128 | bool PeerConnection::SetupMediaTransportForDataChannels_n( |
| 6129 | const std::string& mid) { |
| 6130 | media_transport_ = transport_controller_->GetMediaTransport(mid); |
| 6131 | if (!media_transport_) { |
| 6132 | RTC_LOG(LS_ERROR) << "Media transport is not available for data channels"; |
| 6133 | return false; |
| 6134 | } |
| 6135 | |
| 6136 | media_transport_invoker_ = absl::make_unique<rtc::AsyncInvoker>(); |
| 6137 | media_transport_->SetDataSink(this); |
| 6138 | media_transport_data_mid_ = mid; |
| 6139 | transport_controller_->SignalMediaTransportStateChanged.connect( |
| 6140 | this, &PeerConnection::OnMediaTransportStateChanged_n); |
| 6141 | // Check the initial state right away, in case transport is already writable. |
| 6142 | OnMediaTransportStateChanged_n(); |
| 6143 | return true; |
| 6144 | } |
| 6145 | |
| 6146 | void PeerConnection::TeardownMediaTransportForDataChannels_n() { |
| 6147 | if (!media_transport_) { |
| 6148 | return; |
| 6149 | } |
| 6150 | transport_controller_->SignalMediaTransportStateChanged.disconnect(this); |
| 6151 | media_transport_data_mid_.reset(); |
| 6152 | media_transport_->SetDataSink(nullptr); |
| 6153 | media_transport_invoker_ = nullptr; |
| 6154 | media_transport_ = nullptr; |
| 6155 | } |
| 6156 | |
| 6157 | void PeerConnection::OnMediaTransportStateChanged_n() { |
| 6158 | if (!media_transport_data_mid_ || |
| 6159 | transport_controller_->GetMediaTransportState( |
| 6160 | *media_transport_data_mid_) != MediaTransportState::kWritable) { |
| 6161 | return; |
| 6162 | } |
| 6163 | media_transport_invoker_->AsyncInvoke<void>( |
| 6164 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 6165 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6166 | media_transport_ready_to_send_data_ = true; |
| 6167 | SignalMediaTransportWritable_s(media_transport_ready_to_send_data_); |
| 6168 | }); |
| 6169 | } |
| 6170 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6171 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6172 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6173 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6174 | if (!bundle_enabled) |
| 6175 | return true; |
| 6176 | |
| 6177 | const cricket::ContentGroup* bundle_group = |
| 6178 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6179 | RTC_DCHECK(bundle_group != NULL); |
| 6180 | |
| 6181 | const cricket::ContentInfos& contents = desc->contents(); |
| 6182 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6183 | citer != contents.end(); ++citer) { |
| 6184 | const cricket::ContentInfo* content = (&*citer); |
| 6185 | RTC_DCHECK(content != NULL); |
| 6186 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 6187 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6188 | if (!HasRtcpMuxEnabled(content)) |
| 6189 | return false; |
| 6190 | } |
| 6191 | } |
| 6192 | // RTCP-MUX is enabled in all the contents. |
| 6193 | return true; |
| 6194 | } |
| 6195 | |
| 6196 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6197 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6198 | } |
| 6199 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6200 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6201 | std::set<std::string> mids; |
| 6202 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 11:32:20 -0800 | [diff] [blame] | 6203 | if (content.name.empty()) { |
| 6204 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6205 | "A media section is missing a MID attribute."); |
| 6206 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6207 | if (!mids.insert(content.name).second) { |
| 6208 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6209 | "Duplicate a=mid value '" + content.name + "'."); |
| 6210 | } |
| 6211 | } |
| 6212 | return RTCError::OK(); |
| 6213 | } |
| 6214 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6215 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6216 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6217 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6218 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6219 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6220 | } |
| 6221 | |
| 6222 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6223 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6224 | } |
| 6225 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6226 | SdpType type = sdesc->GetType(); |
| 6227 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6228 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6229 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 6230 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6231 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6232 | } |
| 6233 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6234 | RTCError error = ValidateMids(*sdesc->description()); |
| 6235 | if (!error.ok()) { |
| 6236 | return error; |
| 6237 | } |
| 6238 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6239 | // Verify crypto settings. |
| 6240 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6241 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6242 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6243 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6244 | if (!crypto_error.ok()) { |
| 6245 | return crypto_error; |
| 6246 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6247 | } |
| 6248 | |
| 6249 | // Verify ice-ufrag and ice-pwd. |
| 6250 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6251 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6252 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6253 | } |
| 6254 | |
| 6255 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6256 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6257 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6258 | } |
| 6259 | |
| 6260 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6261 | // m-lines that do not rtcp-mux enabled. |
| 6262 | |
| 6263 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6264 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6265 | // With an answer we want to compare the new answer session description with |
| 6266 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6267 | const cricket::SessionDescription* offer_desc = |
| 6268 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6269 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6270 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6271 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6272 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6273 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6274 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6275 | } |
| 6276 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6277 | // The re-offers should respect the order of m= sections in current |
| 6278 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6279 | // With a re-offer, either the current local or current remote descriptions |
| 6280 | // could be the most up to date, so we would like to check against both of |
| 6281 | // them if they exist. It could be the case that one of them has a 0 port |
| 6282 | // for a media section, but the other does not. This is important to check |
| 6283 | // against in the case that we are recycling an m= section. |
| 6284 | const cricket::SessionDescription* current_desc = nullptr; |
| 6285 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6286 | if (local_description()) { |
| 6287 | current_desc = local_description()->description(); |
| 6288 | if (remote_description()) { |
| 6289 | secondary_current_desc = remote_description()->description(); |
| 6290 | } |
| 6291 | } else if (remote_description()) { |
| 6292 | current_desc = remote_description()->description(); |
| 6293 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6294 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6295 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6296 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6297 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6298 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6299 | } |
| 6300 | } |
| 6301 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6302 | if (IsUnifiedPlan()) { |
| 6303 | // Ensure that each audio and video media section has at most one |
| 6304 | // "StreamParams". This will return an error if receiving a session |
| 6305 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6306 | // same type. With Unified Plan, there can only be at most one track per |
| 6307 | // media section. |
| 6308 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 6309 | const MediaContentDescription& desc = *content.description; |
| 6310 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6311 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6312 | desc.streams().size() > 1u) { |
| 6313 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6314 | "Media section has more than one track specified " |
| 6315 | "with a=ssrc lines which is not supported with " |
| 6316 | "Unified Plan."); |
| 6317 | } |
| 6318 | } |
| 6319 | } |
| 6320 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6321 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6322 | } |
| 6323 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6324 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6325 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6326 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6327 | return (state == PeerConnectionInterface::kStable) || |
| 6328 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6329 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6330 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6331 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6332 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6333 | } |
| 6334 | } |
| 6335 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6336 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6337 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6338 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6339 | return (state == PeerConnectionInterface::kStable) || |
| 6340 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6341 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6342 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6343 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6344 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6345 | } |
| 6346 | } |
| 6347 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6348 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6349 | switch (error) { |
| 6350 | case SessionError::kNone: |
| 6351 | return "ERROR_NONE"; |
| 6352 | case SessionError::kContent: |
| 6353 | return "ERROR_CONTENT"; |
| 6354 | case SessionError::kTransport: |
| 6355 | return "ERROR_TRANSPORT"; |
| 6356 | } |
| 6357 | RTC_NOTREACHED(); |
| 6358 | return ""; |
| 6359 | } |
| 6360 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6361 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 6362 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6363 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6364 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 6365 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6366 | } |
| 6367 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6368 | void PeerConnection::ReportSdpFormatReceived( |
| 6369 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6370 | int num_audio_mlines = 0; |
| 6371 | int num_video_mlines = 0; |
| 6372 | int num_audio_tracks = 0; |
| 6373 | int num_video_tracks = 0; |
| 6374 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6375 | cricket::MediaType media_type = content.media_description()->type(); |
| 6376 | int num_tracks = std::max( |
| 6377 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6378 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6379 | num_audio_mlines += 1; |
| 6380 | num_audio_tracks += num_tracks; |
| 6381 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6382 | num_video_mlines += 1; |
| 6383 | num_video_tracks += num_tracks; |
| 6384 | } |
| 6385 | } |
| 6386 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6387 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6388 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6389 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6390 | format = kSdpFormatReceivedComplexPlanB; |
| 6391 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6392 | format = kSdpFormatReceivedSimple; |
| 6393 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6394 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6395 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6396 | } |
| 6397 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6398 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6399 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6400 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6401 | } |
| 6402 | |
| 6403 | void PeerConnection::ReportUsagePattern() const { |
| 6404 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6405 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6406 | usage_event_accumulator_, |
| 6407 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6408 | const int bad_bits = |
| 6409 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED) | |
| 6410 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6411 | const int good_bits = |
| 6412 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED) | |
| 6413 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6414 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6415 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6416 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6417 | // If called after close(), we can't report, because observer may have |
| 6418 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6419 | if (observer_) { |
| 6420 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6421 | } else { |
| 6422 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6423 | << usage_event_accumulator_ |
| 6424 | << " observed after observer shutdown"; |
| 6425 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6426 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6427 | } |
| 6428 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6429 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 6430 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6431 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6432 | switch (answer.description()->msid_signaling()) { |
| 6433 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6434 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6435 | break; |
| 6436 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6437 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6438 | break; |
| 6439 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6440 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6441 | break; |
| 6442 | case cricket::kMsidSignalingMediaSection | |
| 6443 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6444 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6445 | break; |
| 6446 | default: |
| 6447 | RTC_NOTREACHED(); |
| 6448 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6449 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 6450 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6451 | } |
| 6452 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6453 | // We need to check the local/remote description for the Transport instead of |
| 6454 | // the session, because a new Transport added during renegotiation may have |
| 6455 | // them unset while the session has them set from the previous negotiation. |
| 6456 | // Not doing so may trigger the auto generation of transport description and |
| 6457 | // mess up DTLS identity information, ICE credential, etc. |
| 6458 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 6459 | const IceCandidateInterface* candidate, |
| 6460 | const SessionDescriptionInterface* remote_desc, |
| 6461 | bool* valid) { |
| 6462 | *valid = true; |
| 6463 | |
| 6464 | const SessionDescriptionInterface* current_remote_desc = |
| 6465 | remote_desc ? remote_desc : remote_description(); |
| 6466 | |
| 6467 | if (!current_remote_desc) { |
| 6468 | return false; |
| 6469 | } |
| 6470 | |
| 6471 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 6472 | size_t remote_content_size = |
| 6473 | current_remote_desc->description()->contents().size(); |
| 6474 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6475 | RTC_LOG(LS_ERROR) |
| 6476 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 6477 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6478 | |
| 6479 | *valid = false; |
| 6480 | return false; |
| 6481 | } |
| 6482 | |
| 6483 | cricket::ContentInfo content = |
| 6484 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 6485 | |
| 6486 | const std::string transport_name = GetTransportName(content.name); |
| 6487 | if (transport_name.empty()) { |
| 6488 | return false; |
| 6489 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6490 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6491 | } |
| 6492 | |
| 6493 | bool PeerConnection::SrtpRequired() const { |
| 6494 | return dtls_enabled_ || |
| 6495 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 6496 | } |
| 6497 | |
| 6498 | void PeerConnection::OnTransportControllerGatheringState( |
| 6499 | cricket::IceGatheringState state) { |
| 6500 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6501 | if (state == cricket::kIceGatheringGathering) { |
| 6502 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 6503 | } else if (state == cricket::kIceGatheringComplete) { |
| 6504 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 6505 | } |
| 6506 | } |
| 6507 | |
| 6508 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6509 | std::map<std::string, std::set<cricket::MediaType>> |
| 6510 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame^] | 6511 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6512 | if (transceiver->internal()->channel()) { |
| 6513 | const std::string& transport_name = |
| 6514 | transceiver->internal()->channel()->transport_name(); |
| 6515 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 6516 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6517 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6518 | } |
| 6519 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6520 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 6521 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6522 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6523 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6524 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6525 | if (transport_name) { |
| 6526 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6527 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6528 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6529 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6530 | for (const auto& entry : media_types_by_transport_name) { |
| 6531 | const std::string& transport_name = entry.first; |
| 6532 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6533 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6534 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6535 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6536 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6537 | } |
| 6538 | } |
| 6539 | } |
| 6540 | // Walk through the ConnectionInfos to gather best connection usage |
| 6541 | // for IPv4 and IPv6. |
| 6542 | void PeerConnection::ReportBestConnectionState( |
| 6543 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6544 | for (const cricket::TransportChannelStats& channel_stats : |
| 6545 | stats.channel_stats) { |
| 6546 | for (const cricket::ConnectionInfo& connection_info : |
| 6547 | channel_stats.connection_infos) { |
| 6548 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6549 | continue; |
| 6550 | } |
| 6551 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6552 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6553 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6554 | |
| 6555 | // Increment the counter for IceCandidatePairType. |
| 6556 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6557 | (local.type() == RELAY_PORT_TYPE && |
| 6558 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6559 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 6560 | GetIceCandidatePairCounter(local, remote), |
| 6561 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6562 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6563 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 6564 | GetIceCandidatePairCounter(local, remote), |
| 6565 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6566 | } else { |
| 6567 | RTC_CHECK(0); |
| 6568 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6569 | |
| 6570 | // Increment the counter for IP type. |
| 6571 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6572 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6573 | kBestConnections_IPv4, |
| 6574 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6575 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6576 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6577 | kBestConnections_IPv6, |
| 6578 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6579 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 6580 | RTC_CHECK(!local.address().hostname().empty() && |
| 6581 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | return; |
| 6585 | } |
| 6586 | } |
| 6587 | } |
| 6588 | |
| 6589 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6590 | const cricket::TransportStats& stats, |
| 6591 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6592 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6593 | return; |
| 6594 | } |
| 6595 | |
| 6596 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6597 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6598 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6599 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6600 | return; |
| 6601 | } |
| 6602 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6603 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6604 | for (cricket::MediaType media_type : media_types) { |
| 6605 | switch (media_type) { |
| 6606 | case cricket::MEDIA_TYPE_AUDIO: |
| 6607 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6608 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 6609 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6610 | break; |
| 6611 | case cricket::MEDIA_TYPE_VIDEO: |
| 6612 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6613 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 6614 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6615 | break; |
| 6616 | case cricket::MEDIA_TYPE_DATA: |
| 6617 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6618 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 6619 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6620 | break; |
| 6621 | default: |
| 6622 | RTC_NOTREACHED(); |
| 6623 | continue; |
| 6624 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6625 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6629 | for (cricket::MediaType media_type : media_types) { |
| 6630 | switch (media_type) { |
| 6631 | case cricket::MEDIA_TYPE_AUDIO: |
| 6632 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6633 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 6634 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6635 | break; |
| 6636 | case cricket::MEDIA_TYPE_VIDEO: |
| 6637 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6638 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 6639 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6640 | break; |
| 6641 | case cricket::MEDIA_TYPE_DATA: |
| 6642 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6643 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 6644 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6645 | break; |
| 6646 | default: |
| 6647 | RTC_NOTREACHED(); |
| 6648 | continue; |
| 6649 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6650 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6651 | } |
| 6652 | } |
| 6653 | |
| 6654 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6655 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6656 | RTC_DCHECK(call_); |
| 6657 | call_->OnSentPacket(sent_packet); |
| 6658 | } |
| 6659 | |
| 6660 | const std::string PeerConnection::GetTransportName( |
| 6661 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6662 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6663 | if (channel) { |
| 6664 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6665 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6666 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6667 | RTC_DCHECK(sctp_mid_); |
| 6668 | if (content_name == *sctp_mid_) { |
| 6669 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6670 | } |
| 6671 | } |
| 6672 | // Return an empty string if failed to retrieve the transport name. |
| 6673 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6674 | } |
| 6675 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6676 | void PeerConnection::DestroyTransceiverChannel( |
| 6677 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6678 | transceiver) { |
| 6679 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6680 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6681 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6682 | if (channel) { |
| 6683 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6684 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6685 | } |
| 6686 | } |
| 6687 | |
| 6688 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6689 | if (rtp_data_channel_) { |
| 6690 | OnDataChannelDestroyed(); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6691 | DestroyChannelInterface(rtp_data_channel_); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6692 | rtp_data_channel_ = nullptr; |
| 6693 | } |
| 6694 | |
| 6695 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 6696 | // grab a reference to this PeerConnection. If this is called from the |
| 6697 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 6698 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 6699 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 6700 | |
| 6701 | if (sctp_transport_) { |
| 6702 | OnDataChannelDestroyed(); |
| 6703 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 6704 | [this] { DestroySctpTransport_n(); }); |
| 6705 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6706 | |
| 6707 | if (media_transport_) { |
| 6708 | OnDataChannelDestroyed(); |
| 6709 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 6710 | RTC_DCHECK_RUN_ON(network_thread()); |
| 6711 | TeardownMediaTransportForDataChannels_n(); |
| 6712 | }); |
| 6713 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6714 | } |
| 6715 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6716 | void PeerConnection::DestroyChannelInterface( |
| 6717 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6718 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6719 | switch (channel->media_type()) { |
| 6720 | case cricket::MEDIA_TYPE_AUDIO: |
| 6721 | channel_manager()->DestroyVoiceChannel( |
| 6722 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6723 | break; |
| 6724 | case cricket::MEDIA_TYPE_VIDEO: |
| 6725 | channel_manager()->DestroyVideoChannel( |
| 6726 | static_cast<cricket::VideoChannel*>(channel)); |
| 6727 | break; |
| 6728 | case cricket::MEDIA_TYPE_DATA: |
| 6729 | channel_manager()->DestroyRtpDataChannel( |
| 6730 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6731 | break; |
| 6732 | default: |
| 6733 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6734 | break; |
| 6735 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6736 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6737 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6738 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6739 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6740 | RtpTransportInternal* rtp_transport, |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 6741 | cricket::DtlsTransportInternal* dtls_transport, |
| 6742 | MediaTransportInterface* media_transport) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6743 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6744 | auto base_channel = GetChannel(mid); |
| 6745 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6746 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6747 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6748 | if (sctp_transport_ && mid == sctp_mid_) { |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6749 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6750 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 6751 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 6752 | if (configuration_.use_media_transport) { |
| 6753 | // Only pass media transport to call object if media transport is used |
| 6754 | // for media (and not data channel). |
| 6755 | call_->MediaTransportChange(media_transport); |
| 6756 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 6757 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6758 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6759 | } |
| 6760 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6761 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 6762 | // In earlier production code, the pointer was not cleared on close, |
| 6763 | // which might have led to undefined behavior if the observer was not |
| 6764 | // deallocated, or strange crashes if it was. |
| 6765 | // We use CHECK in order to catch such behavior if it exists. |
| 6766 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 6767 | RTC_CHECK(observer_); |
| 6768 | return observer_; |
| 6769 | } |
| 6770 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 6771 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 6772 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 6773 | // after it has been removed. |
| 6774 | return configuration_.crypto_options.has_value() |
| 6775 | ? *configuration_.crypto_options |
| 6776 | : factory_->options().crypto_options; |
| 6777 | } |
| 6778 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6779 | void PeerConnection::ClearStatsCache() { |
| 6780 | if (stats_collector_) { |
| 6781 | stats_collector_->ClearCachedStatsReport(); |
| 6782 | } |
| 6783 | } |
| 6784 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6785 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 6786 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 6787 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6788 | } |
| 6789 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6790 | } // namespace webrtc |