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 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 20 | #include "absl/algorithm/container.h" |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 21 | #include "absl/memory/memory.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 21:02:22 +0100 | [diff] [blame] | 22 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "api/jsep_ice_candidate.h" |
| 24 | #include "api/jsep_session_description.h" |
| 25 | #include "api/media_stream_proxy.h" |
| 26 | #include "api/media_stream_track_proxy.h" |
| 27 | #include "api/uma_metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "call/call.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "logging/rtc_event_log/ice_logger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 30 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "logging/rtc_event_log/rtc_event_log.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "media/sctp/sctp_transport.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 33 | #include "pc/audio_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "pc/audio_track.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 35 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "pc/channel_manager.h" |
| 37 | #include "pc/dtmf_sender.h" |
| 38 | #include "pc/media_stream.h" |
| 39 | #include "pc/media_stream_observer.h" |
| 40 | #include "pc/remote_audio_source.h" |
| 41 | #include "pc/rtp_media_utils.h" |
| 42 | #include "pc/rtp_receiver.h" |
| 43 | #include "pc/rtp_sender.h" |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 44 | #include "pc/sctp_transport.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 45 | #include "pc/sctp_utils.h" |
| 46 | #include "pc/sdp_utils.h" |
| 47 | #include "pc/stream_collection.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 48 | #include "pc/video_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 49 | #include "pc/video_track.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 50 | #include "rtc_base/bind.h" |
| 51 | #include "rtc_base/checks.h" |
| 52 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 53 | #include "rtc_base/numerics/safe_conversions.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 54 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 55 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 56 | #include "rtc_base/trace_event.h" |
| 57 | #include "system_wrappers/include/clock.h" |
| 58 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 59 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 61 | using cricket::ContentInfo; |
| 62 | using cricket::ContentInfos; |
| 63 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 64 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 65 | using cricket::RidDescription; |
| 66 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 67 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 68 | using cricket::SimulcastDescription; |
| 69 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 70 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 71 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 72 | using cricket::TransportInfo; |
| 73 | |
| 74 | using cricket::LOCAL_PORT_TYPE; |
| 75 | using cricket::STUN_PORT_TYPE; |
| 76 | using cricket::RELAY_PORT_TYPE; |
| 77 | using cricket::PRFLX_PORT_TYPE; |
| 78 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 79 | namespace webrtc { |
| 80 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 81 | // Error messages |
| 82 | const char kBundleWithoutRtcpMux[] = |
| 83 | "rtcp-mux must be enabled when BUNDLE " |
| 84 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 85 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 86 | const char kInvalidSdp[] = "Invalid session description."; |
| 87 | const char kMlineMismatchInAnswer[] = |
| 88 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 89 | "answer."; |
| 90 | const char kMlineMismatchInSubsequentOffer[] = |
| 91 | "The order of m-lines in subsequent offer doesn't match order from " |
| 92 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 93 | const char kSdpWithoutDtlsFingerprint[] = |
| 94 | "Called with SDP without DTLS fingerprint."; |
| 95 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 96 | const char kSdpWithoutIceUfragPwd[] = |
| 97 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 98 | const char kSessionError[] = "Session error code: "; |
| 99 | const char kSessionErrorDesc[] = "Session error description: "; |
| 100 | const char kDtlsSrtpSetupFailureRtp[] = |
| 101 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 102 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 103 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 105 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 107 | // UMA metric names. |
| 108 | const char kSimulcastVersionApplyLocalDescription[] = |
| 109 | "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription"; |
| 110 | const char kSimulcastVersionApplyRemoteDescription[] = |
| 111 | "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription"; |
| 112 | const char kSimulcastNumberOfEncodings[] = |
| 113 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
| 114 | const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled"; |
| 115 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 116 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 117 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 118 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 119 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 120 | // The length of RTCP CNAMEs. |
| 121 | static const int kRtcpCnameLength = 16; |
| 122 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 123 | enum { |
| 124 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 125 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 126 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 127 | MSG_GETSTATS, |
| 128 | MSG_FREE_DATACHANNELS, |
| 129 | MSG_REPORT_USAGE_PATTERN, |
| 130 | }; |
| 131 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 132 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 133 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 134 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 135 | explicit SetSessionDescriptionMsg( |
| 136 | webrtc::SetSessionDescriptionObserver* observer) |
| 137 | : observer(observer) {} |
| 138 | |
| 139 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 140 | RTCError error; |
| 141 | }; |
| 142 | |
| 143 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 144 | explicit CreateSessionDescriptionMsg( |
| 145 | webrtc::CreateSessionDescriptionObserver* observer) |
| 146 | : observer(observer) {} |
| 147 | |
| 148 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 149 | RTCError error; |
| 150 | }; |
| 151 | |
| 152 | struct GetStatsMsg : public rtc::MessageData { |
| 153 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 154 | webrtc::MediaStreamTrackInterface* track) |
| 155 | : observer(observer), track(track) {} |
| 156 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 157 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 158 | }; |
| 159 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 160 | // Check if we can send |new_stream| on a PeerConnection. |
| 161 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 162 | webrtc::MediaStreamInterface* new_stream) { |
| 163 | if (!new_stream || !current_streams) { |
| 164 | return false; |
| 165 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 166 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 167 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 168 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 174 | // If the direction is "recvonly" or "inactive", treat the description |
| 175 | // as containing no streams. |
| 176 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 177 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 178 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 179 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 180 | ? desc->streams() |
| 181 | : std::vector<cricket::StreamParams>(); |
| 182 | } |
| 183 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 184 | bool IsValidOfferToReceiveMedia(int value) { |
| 185 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 186 | return (value >= Options::kUndefined) && |
| 187 | (value <= Options::kMaxOfferToReceiveMedia); |
| 188 | } |
| 189 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 190 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 191 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 192 | const std::vector<rtc::scoped_refptr< |
| 193 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 194 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 195 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 196 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 197 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 198 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 199 | if (audio_media_description_options) { |
| 200 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 201 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 202 | } |
| 203 | } else { |
| 204 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 205 | if (video_media_description_options) { |
| 206 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 207 | sender->id(), sender->internal()->stream_ids(), {}, |
| 208 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 211 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 212 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 213 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 214 | // Add options to |session_options| from |rtp_data_channels|. |
| 215 | void AddRtpDataChannelOptions( |
| 216 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 217 | rtp_data_channels, |
| 218 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 219 | if (!data_media_description_options) { |
| 220 | return; |
| 221 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 222 | // Check for data channels. |
| 223 | for (const auto& kv : rtp_data_channels) { |
| 224 | const DataChannel* channel = kv.second; |
| 225 | if (channel->state() == DataChannel::kConnecting || |
| 226 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 227 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 228 | // the data channel's label. |
| 229 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 230 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 235 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 236 | PeerConnectionInterface::IceTransportsType type) { |
| 237 | switch (type) { |
| 238 | case PeerConnectionInterface::kNone: |
| 239 | return cricket::CF_NONE; |
| 240 | case PeerConnectionInterface::kRelay: |
| 241 | return cricket::CF_RELAY; |
| 242 | case PeerConnectionInterface::kNoHost: |
| 243 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 244 | case PeerConnectionInterface::kAll: |
| 245 | return cricket::CF_ALL; |
| 246 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 247 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 248 | } |
| 249 | return cricket::CF_NONE; |
| 250 | } |
| 251 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 252 | // Helper to set an error and return from a method. |
| 253 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 254 | if (error) { |
| 255 | error->set_type(type); |
| 256 | } |
| 257 | return type == webrtc::RTCErrorType::NONE; |
| 258 | } |
| 259 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 260 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 261 | bool ok = error.ok(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 262 | if (error_out) { |
| 263 | *error_out = std::move(error); |
| 264 | } |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 265 | return ok; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 268 | std::string GetSignalingStateString( |
| 269 | PeerConnectionInterface::SignalingState state) { |
| 270 | switch (state) { |
| 271 | case PeerConnectionInterface::kStable: |
| 272 | return "kStable"; |
| 273 | case PeerConnectionInterface::kHaveLocalOffer: |
| 274 | return "kHaveLocalOffer"; |
| 275 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 276 | return "kHavePrAnswer"; |
| 277 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 278 | return "kHaveRemoteOffer"; |
| 279 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 280 | return "kHaveRemotePrAnswer"; |
| 281 | case PeerConnectionInterface::kClosed: |
| 282 | return "kClosed"; |
| 283 | } |
| 284 | RTC_NOTREACHED(); |
| 285 | return ""; |
| 286 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 287 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 288 | IceCandidatePairType GetIceCandidatePairCounter( |
| 289 | const cricket::Candidate& local, |
| 290 | const cricket::Candidate& remote) { |
| 291 | const auto& l = local.type(); |
| 292 | const auto& r = remote.type(); |
| 293 | const auto& host = LOCAL_PORT_TYPE; |
| 294 | const auto& srflx = STUN_PORT_TYPE; |
| 295 | const auto& relay = RELAY_PORT_TYPE; |
| 296 | const auto& prflx = PRFLX_PORT_TYPE; |
| 297 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 298 | bool local_hostname = |
| 299 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 300 | bool remote_hostname = !remote.address().hostname().empty() && |
| 301 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 302 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 303 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 304 | if (local_hostname) { |
| 305 | if (remote_hostname) { |
| 306 | return kIceCandidatePairHostNameHostName; |
| 307 | } else if (remote_private) { |
| 308 | return kIceCandidatePairHostNameHostPrivate; |
| 309 | } else { |
| 310 | return kIceCandidatePairHostNameHostPublic; |
| 311 | } |
| 312 | } else if (local_private) { |
| 313 | if (remote_hostname) { |
| 314 | return kIceCandidatePairHostPrivateHostName; |
| 315 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 316 | return kIceCandidatePairHostPrivateHostPrivate; |
| 317 | } else { |
| 318 | return kIceCandidatePairHostPrivateHostPublic; |
| 319 | } |
| 320 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 321 | if (remote_hostname) { |
| 322 | return kIceCandidatePairHostPublicHostName; |
| 323 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 324 | return kIceCandidatePairHostPublicHostPrivate; |
| 325 | } else { |
| 326 | return kIceCandidatePairHostPublicHostPublic; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | if (l == host && r == srflx) |
| 331 | return kIceCandidatePairHostSrflx; |
| 332 | if (l == host && r == relay) |
| 333 | return kIceCandidatePairHostRelay; |
| 334 | if (l == host && r == prflx) |
| 335 | return kIceCandidatePairHostPrflx; |
| 336 | if (l == srflx && r == host) |
| 337 | return kIceCandidatePairSrflxHost; |
| 338 | if (l == srflx && r == srflx) |
| 339 | return kIceCandidatePairSrflxSrflx; |
| 340 | if (l == srflx && r == relay) |
| 341 | return kIceCandidatePairSrflxRelay; |
| 342 | if (l == srflx && r == prflx) |
| 343 | return kIceCandidatePairSrflxPrflx; |
| 344 | if (l == relay && r == host) |
| 345 | return kIceCandidatePairRelayHost; |
| 346 | if (l == relay && r == srflx) |
| 347 | return kIceCandidatePairRelaySrflx; |
| 348 | if (l == relay && r == relay) |
| 349 | return kIceCandidatePairRelayRelay; |
| 350 | if (l == relay && r == prflx) |
| 351 | return kIceCandidatePairRelayPrflx; |
| 352 | if (l == prflx && r == host) |
| 353 | return kIceCandidatePairPrflxHost; |
| 354 | if (l == prflx && r == srflx) |
| 355 | return kIceCandidatePairPrflxSrflx; |
| 356 | if (l == prflx && r == relay) |
| 357 | return kIceCandidatePairPrflxRelay; |
| 358 | return kIceCandidatePairMax; |
| 359 | } |
| 360 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 361 | // Logic to decide if an m= section can be recycled. This means that the new |
| 362 | // m= section is not rejected, but the old local or remote m= section is |
| 363 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 364 | // of the old remote and old local descriptions in no particular order. |
| 365 | // We need to check both the old local and remote because either |
| 366 | // could be the most current from the latest negotation. |
| 367 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 368 | const ContentInfo& content, |
| 369 | const ContentInfo* old_content_one, |
| 370 | const ContentInfo* old_content_two) { |
| 371 | return type == SdpType::kOffer && !content.rejected && |
| 372 | ((old_content_one && old_content_one->rejected) || |
| 373 | (old_content_two && old_content_two->rejected)); |
| 374 | } |
| 375 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 376 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 377 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 378 | // less than |current_desc|. In the case of checking an answer's |
| 379 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 380 | // local or remote. In the case of checking an offer's |new_desc| we |
| 381 | // check against the local and remote descriptions stored from the last |
| 382 | // negotiation, because either of these could be the most up to date for |
| 383 | // possible rejected m sections. These are the |current_desc| and |
| 384 | // |secondary_current_desc|. |
| 385 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 386 | const SessionDescription* secondary_current_desc, |
| 387 | const SessionDescription& new_desc, |
| 388 | const SdpType type) { |
| 389 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 393 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 394 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 395 | if (secondary_current_desc && |
| 396 | i < secondary_current_desc->contents().size()) { |
| 397 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 398 | } |
| 399 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 400 | ¤t_desc.contents()[i], |
| 401 | secondary_content_info)) { |
| 402 | // For new offer descriptions, if the media section can be recycled, it's |
| 403 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 404 | continue; |
| 405 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 406 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 410 | new_desc.contents()[i].media_description(); |
| 411 | const MediaContentDescription* current_desc_mdesc = |
| 412 | current_desc.contents()[i].media_description(); |
| 413 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 414 | return false; |
| 415 | } |
| 416 | } |
| 417 | return true; |
| 418 | } |
| 419 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 420 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 421 | const SessionDescription& desc2) { |
| 422 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 425 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 426 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 427 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 428 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 429 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 430 | static constexpr struct { |
| 431 | KeyExchangeProtocolType protocol_type; |
| 432 | cricket::MediaType media_type; |
| 433 | KeyExchangeProtocolMedia protocol_media; |
| 434 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 435 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 436 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 437 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 438 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 439 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 440 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 441 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 442 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 443 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 444 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 445 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 446 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 447 | }; |
| 448 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 449 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 450 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 451 | |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 452 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 453 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 454 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 455 | i.protocol_media, |
| 456 | kEnumCounterKeyProtocolMediaTypeMax); |
| 457 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 461 | void NoteAddIceCandidateResult(int result) { |
| 462 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 463 | kAddIceCandidateMax); |
| 464 | } |
| 465 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 466 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 467 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 468 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 469 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 470 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 471 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 472 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 473 | const cricket::ContentGroup* bundle = |
| 474 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 475 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 476 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 477 | continue; |
| 478 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 479 | // Note what media is used with each crypto protocol, for all sections. |
| 480 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 481 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 482 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 483 | const std::string& mid = content_info.name; |
| 484 | if (bundle && bundle->HasContentName(mid) && |
| 485 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 486 | // This isn't the first media section in the BUNDLE group, so it's not |
| 487 | // required to have crypto attributes, since only the crypto attributes |
| 488 | // from the first section actually get used. |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | // If the content isn't rejected or bundled into another m= section, crypto |
| 493 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 494 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 495 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 496 | if (!media || !tinfo) { |
| 497 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 498 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 499 | } |
| 500 | if (dtls_enabled) { |
| 501 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 502 | RTC_LOG(LS_WARNING) |
| 503 | << "Session description must have DTLS fingerprint if " |
| 504 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 505 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 506 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 507 | } |
| 508 | } else { |
| 509 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 510 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 511 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 512 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 516 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 520 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 521 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 522 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 523 | const cricket::ContentGroup* bundle = |
| 524 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 525 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 526 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 527 | continue; |
| 528 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 529 | const std::string& mid = content_info.name; |
| 530 | if (bundle && bundle->HasContentName(mid) && |
| 531 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 532 | // This isn't the first media section in the BUNDLE group, so it's not |
| 533 | // required to have ufrag/password, since only the ufrag/password from |
| 534 | // the first section actually get used. |
| 535 | continue; |
| 536 | } |
| 537 | |
| 538 | // If the content isn't rejected or bundled into another m= section, |
| 539 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 540 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 541 | if (!tinfo) { |
| 542 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 543 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | if (tinfo->description.ice_ufrag.empty() || |
| 547 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 548 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 549 | return false; |
| 550 | } |
| 551 | } |
| 552 | return true; |
| 553 | } |
| 554 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 555 | // Get the SCTP port out of a SessionDescription. |
| 556 | // Return -1 if not found. |
| 557 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 558 | const cricket::DataContentDescription* data_desc = |
| 559 | GetFirstDataContentDescription(session_description); |
| 560 | RTC_DCHECK(data_desc); |
| 561 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 562 | return -1; |
| 563 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 564 | std::string value; |
| 565 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 566 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 567 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 568 | if (!codec.Matches(match_pattern)) { |
| 569 | continue; |
| 570 | } |
| 571 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 572 | return rtc::FromString<int>(value); |
| 573 | } |
| 574 | } |
| 575 | return -1; |
| 576 | } |
| 577 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 578 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 579 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 580 | const SessionDescriptionInterface* new_desc, |
| 581 | const std::string& content_name) { |
| 582 | if (!old_desc) { |
| 583 | return false; |
| 584 | } |
| 585 | const SessionDescription* new_sd = new_desc->description(); |
| 586 | const SessionDescription* old_sd = old_desc->description(); |
| 587 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 588 | if (!cinfo || cinfo->rejected) { |
| 589 | return false; |
| 590 | } |
| 591 | // If the content isn't rejected, check if ufrag and password has changed. |
| 592 | const cricket::TransportDescription* new_transport_desc = |
| 593 | new_sd->GetTransportDescriptionByName(content_name); |
| 594 | const cricket::TransportDescription* old_transport_desc = |
| 595 | old_sd->GetTransportDescriptionByName(content_name); |
| 596 | if (!new_transport_desc || !old_transport_desc) { |
| 597 | // No transport description exists. This is not an ICE restart. |
| 598 | return false; |
| 599 | } |
| 600 | if (cricket::IceCredentialsChanged( |
| 601 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 602 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 603 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 604 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 605 | return true; |
| 606 | } |
| 607 | return false; |
| 608 | } |
| 609 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 610 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 611 | // from an RTCError. |
| 612 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 613 | SdpType type, |
| 614 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 615 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 616 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 617 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 618 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 621 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 622 | std::string output = "streams=["; |
| 623 | const char* separator = ""; |
| 624 | for (const auto& stream_id : stream_ids) { |
| 625 | output.append(separator).append(stream_id); |
| 626 | separator = ", "; |
| 627 | } |
| 628 | output.append("]"); |
| 629 | return output; |
| 630 | } |
| 631 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 632 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 633 | int rtc_configuration_parameter) { |
| 634 | if (rtc_configuration_parameter == |
| 635 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 636 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 637 | } |
| 638 | return rtc_configuration_parameter; |
| 639 | } |
| 640 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 641 | cricket::DataMessageType ToCricketDataMessageType(DataMessageType type) { |
| 642 | switch (type) { |
| 643 | case DataMessageType::kText: |
| 644 | return cricket::DMT_TEXT; |
| 645 | case DataMessageType::kBinary: |
| 646 | return cricket::DMT_BINARY; |
| 647 | case DataMessageType::kControl: |
| 648 | return cricket::DMT_CONTROL; |
| 649 | default: |
| 650 | return cricket::DMT_NONE; |
| 651 | } |
| 652 | return cricket::DMT_NONE; |
| 653 | } |
| 654 | |
| 655 | DataMessageType ToWebrtcDataMessageType(cricket::DataMessageType type) { |
| 656 | switch (type) { |
| 657 | case cricket::DMT_TEXT: |
| 658 | return DataMessageType::kText; |
| 659 | case cricket::DMT_BINARY: |
| 660 | return DataMessageType::kBinary; |
| 661 | case cricket::DMT_CONTROL: |
| 662 | return DataMessageType::kControl; |
| 663 | case cricket::DMT_NONE: |
| 664 | default: |
| 665 | RTC_NOTREACHED(); |
| 666 | } |
| 667 | return DataMessageType::kControl; |
| 668 | } |
| 669 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 670 | void ReportSimulcastApiVersion(const char* name, |
| 671 | const SessionDescription& session) { |
| 672 | bool has_legacy = false; |
| 673 | bool has_spec_compliant = false; |
| 674 | for (const ContentInfo& content : session.contents()) { |
| 675 | if (!content.description) { |
| 676 | continue; |
| 677 | } |
| 678 | has_spec_compliant |= content.description->HasSimulcast(); |
| 679 | for (const StreamParams& sp : content.description->streams()) { |
| 680 | has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if (has_legacy) { |
| 685 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy, |
| 686 | kSimulcastApiVersionMax); |
| 687 | } |
| 688 | if (has_spec_compliant) { |
| 689 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant, |
| 690 | kSimulcastApiVersionMax); |
| 691 | } |
| 692 | if (!has_legacy && !has_spec_compliant) { |
| 693 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone, |
| 694 | kSimulcastApiVersionMax); |
| 695 | } |
| 696 | } |
| 697 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 698 | } // namespace |
| 699 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 700 | // Upon completion, posts a task to execute the callback of the |
| 701 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 702 | // point, the state of the peer connection might no longer reflect the effects |
| 703 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 704 | // modified during the post. |
| 705 | // TODO(hbos): Remove this class once we remove the version of |
| 706 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 707 | // SetSessionDescriptionObserver as an argument. |
| 708 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 709 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 710 | public: |
| 711 | SetRemoteDescriptionObserverAdapter( |
| 712 | rtc::scoped_refptr<PeerConnection> pc, |
| 713 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 714 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 715 | |
| 716 | // SetRemoteDescriptionObserverInterface implementation. |
| 717 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 718 | if (error.ok()) |
| 719 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 720 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 721 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | private: |
| 725 | rtc::scoped_refptr<PeerConnection> pc_; |
| 726 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 727 | }; |
| 728 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 729 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 730 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 731 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 732 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 733 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 734 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 735 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 736 | BundlePolicy bundle_policy; |
| 737 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 738 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 739 | int ice_candidate_pool_size; |
| 740 | bool disable_ipv6; |
| 741 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 742 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 743 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 744 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 745 | absl::optional<int> screencast_min_bitrate; |
| 746 | absl::optional<bool> combined_audio_video_bwe; |
| 747 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 748 | TcpCandidatePolicy tcp_candidate_policy; |
| 749 | CandidateNetworkPolicy candidate_network_policy; |
| 750 | int audio_jitter_buffer_max_packets; |
| 751 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 752 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 753 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 754 | int ice_connection_receiving_timeout; |
| 755 | int ice_backup_candidate_pair_ping_interval; |
| 756 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 757 | bool prioritize_most_likely_ice_candidate_pairs; |
| 758 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 759 | bool prune_turn_ports; |
| 760 | bool presume_writable_when_fully_relayed; |
| 761 | bool enable_ice_renomination; |
| 762 | bool redetermine_role_on_ice_restart; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 763 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 764 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 765 | absl::optional<int> ice_check_min_interval; |
| 766 | absl::optional<int> ice_unwritable_timeout; |
| 767 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 768 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 769 | absl::optional<int> stun_candidate_keepalive_interval; |
| 770 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 771 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 772 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 773 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 774 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 775 | bool use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 776 | bool use_media_transport_for_data_channels; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 777 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 778 | bool offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 779 | }; |
| 780 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 781 | "Did you add something to RTCConfiguration and forget to " |
| 782 | "update operator==?"); |
| 783 | return type == o.type && servers == o.servers && |
| 784 | bundle_policy == o.bundle_policy && |
| 785 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 786 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 787 | candidate_network_policy == o.candidate_network_policy && |
| 788 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 789 | audio_jitter_buffer_fast_accelerate == |
| 790 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 791 | audio_jitter_buffer_min_delay_ms == |
| 792 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 793 | audio_jitter_buffer_enable_rtx_handling == |
| 794 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 795 | ice_connection_receiving_timeout == |
| 796 | o.ice_connection_receiving_timeout && |
| 797 | ice_backup_candidate_pair_ping_interval == |
| 798 | o.ice_backup_candidate_pair_ping_interval && |
| 799 | continual_gathering_policy == o.continual_gathering_policy && |
| 800 | certificates == o.certificates && |
| 801 | prioritize_most_likely_ice_candidate_pairs == |
| 802 | o.prioritize_most_likely_ice_candidate_pairs && |
| 803 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 804 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 805 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 806 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 807 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 808 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 809 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 810 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 811 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 812 | prune_turn_ports == o.prune_turn_ports && |
| 813 | presume_writable_when_fully_relayed == |
| 814 | o.presume_writable_when_fully_relayed && |
| 815 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 816 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 817 | ice_check_interval_strong_connectivity == |
| 818 | o.ice_check_interval_strong_connectivity && |
| 819 | ice_check_interval_weak_connectivity == |
| 820 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 821 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 822 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 823 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 824 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 825 | stun_candidate_keepalive_interval == |
| 826 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 827 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 828 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 829 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 830 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 831 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 832 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 833 | use_media_transport_for_data_channels == |
| 834 | o.use_media_transport_for_data_channels && |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 835 | crypto_options == o.crypto_options && |
| 836 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 840 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 841 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 842 | } |
| 843 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 844 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 845 | std::string GenerateRtcpCname() { |
| 846 | std::string cname; |
| 847 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 848 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 849 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 850 | } |
| 851 | return cname; |
| 852 | } |
| 853 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 854 | bool ValidateOfferAnswerOptions( |
| 855 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 856 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 857 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 858 | } |
| 859 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 860 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 861 | // m= sections (in other words, nothing that involves a map/array). |
| 862 | void ExtractSharedMediaSessionOptions( |
| 863 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 864 | cricket::MediaSessionOptions* session_options) { |
| 865 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 866 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 867 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 868 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 869 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 870 | std::unique_ptr<RtcEventLog> event_log, |
| 871 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 872 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 873 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 874 | event_log_ptr_(event_log_.get()), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 875 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 876 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 877 | remote_streams_(StreamCollection::Create()), |
| 878 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 879 | |
| 880 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 881 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 882 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 883 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 884 | // Need to stop transceivers before destroying the stats collector because |
| 885 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 886 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 887 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 888 | transceiver->Stop(); |
| 889 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 890 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 891 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 892 | if (stats_collector_) { |
| 893 | stats_collector_->WaitForPendingRequest(); |
| 894 | stats_collector_ = nullptr; |
| 895 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 896 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 897 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 898 | // the last stats request can still read from the channels. |
| 899 | DestroyAllChannels(); |
| 900 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 901 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 902 | |
| 903 | webrtc_session_desc_factory_.reset(); |
| 904 | sctp_invoker_.reset(); |
| 905 | sctp_factory_.reset(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 906 | media_transport_invoker_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 907 | transport_controller_.reset(); |
| 908 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 909 | // port_allocator_ lives on the network thread and should be destroyed there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 910 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 911 | RTC_DCHECK_RUN_ON(network_thread()); |
| 912 | port_allocator_.reset(); |
| 913 | }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 914 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 915 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 916 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 917 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 918 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 919 | event_log_.reset(); |
| 920 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 923 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 924 | // Destroy video channels first since they may have a pointer to a voice |
| 925 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 926 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 927 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 928 | DestroyTransceiverChannel(transceiver); |
| 929 | } |
| 930 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 931 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 932 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 933 | DestroyTransceiverChannel(transceiver); |
| 934 | } |
| 935 | } |
| 936 | DestroyDataChannel(); |
| 937 | } |
| 938 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 939 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 940 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 941 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 942 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 943 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 944 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 945 | |
| 946 | RTCError config_error = ValidateConfiguration(configuration); |
| 947 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 948 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 949 | return false; |
| 950 | } |
| 951 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 952 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 953 | RTC_LOG(LS_ERROR) |
| 954 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 955 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 956 | return false; |
| 957 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 958 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 959 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 960 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 961 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 962 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 963 | return false; |
| 964 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 965 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 966 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 967 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 968 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 969 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 970 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 971 | cricket::ServerAddresses stun_servers; |
| 972 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 973 | |
| 974 | RTCErrorType parse_error = |
| 975 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 976 | if (parse_error != RTCErrorType::NONE) { |
| 977 | return false; |
| 978 | } |
| 979 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 980 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 981 | // there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 982 | const auto pa_result = |
| 983 | network_thread()->Invoke<InitializePortAllocatorResult>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 984 | RTC_FROM_HERE, |
| 985 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 986 | stun_servers, turn_servers, configuration)); |
| 987 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 988 | // If initialization was successful, note if STUN or TURN servers |
| 989 | // were supplied. |
| 990 | if (!stun_servers.empty()) { |
| 991 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 992 | } |
| 993 | if (!turn_servers.empty()) { |
| 994 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 995 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 996 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 997 | // Send information about IPv4/IPv6 status. |
| 998 | PeerConnectionAddressFamilyCounter address_family; |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 999 | if (pa_result.enable_ipv6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1000 | address_family = kPeerConnection_IPv6; |
| 1001 | } else { |
| 1002 | address_family = kPeerConnection_IPv4; |
| 1003 | } |
| 1004 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 1005 | kPeerConnectionAddressFamilyCounter_Max); |
| 1006 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1007 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 1008 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1009 | // RFC 3264: The numeric value of the session id and version in the |
| 1010 | // o line MUST be representable with a "64 bit signed integer". |
| 1011 | // Due to this constraint session id |session_id_| is max limited to |
| 1012 | // LLONG_MAX. |
| 1013 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1014 | JsepTransportController::Config config; |
| 1015 | config.redetermine_role_on_ice_restart = |
| 1016 | configuration.redetermine_role_on_ice_restart; |
| 1017 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 1018 | config.disable_encryption = options.disable_encryption; |
| 1019 | config.bundle_policy = configuration.bundle_policy; |
| 1020 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1021 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 1022 | // stub. |
| 1023 | config.crypto_options = configuration.crypto_options.has_value() |
| 1024 | ? *configuration.crypto_options |
| 1025 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1026 | config.transport_observer = this; |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 1027 | config.event_log = event_log_ptr_; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1028 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 1029 | config.enable_external_auth = true; |
| 1030 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 1031 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1032 | |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 1033 | if (configuration.use_media_transport || |
| 1034 | configuration.use_media_transport_for_data_channels) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1035 | if (!factory_->media_transport_factory()) { |
| 1036 | RTC_DCHECK(false) |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 1037 | << "PeerConnecton is initialized with use_media_transport = true or " |
| 1038 | << "use_media_transport_for_data_channels = true " |
| 1039 | << "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1040 | return false; |
| 1041 | } |
| 1042 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 1043 | if (configuration.use_media_transport || |
| 1044 | configuration.use_media_transport_for_data_channels) { |
| 1045 | // TODO(bugs.webrtc.org/9719): This check will eventually go away, when |
| 1046 | // RTP media transport is introduced. But until then, we require SDES to |
| 1047 | // be enabled. |
| 1048 | if (configuration.enable_dtls_srtp.has_value() && |
| 1049 | configuration.enable_dtls_srtp.value()) { |
| 1050 | RTC_LOG(LS_WARNING) |
| 1051 | << "When media transport is used, SDES must be enabled. Set " |
| 1052 | "configuration.enable_dtls_srtp to false. use_media_transport=" |
| 1053 | << configuration.use_media_transport |
| 1054 | << ", use_media_transport_for_data_channels=" |
| 1055 | << configuration.use_media_transport_for_data_channels; |
| 1056 | return false; |
| 1057 | } |
| 1058 | } |
| 1059 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 1060 | config.use_media_transport_for_media = configuration.use_media_transport; |
| 1061 | config.use_media_transport_for_data_channels = |
| 1062 | configuration.use_media_transport_for_data_channels; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1063 | config.media_transport_factory = factory_->media_transport_factory(); |
| 1064 | } |
| 1065 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1066 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 1067 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1068 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1069 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1070 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1071 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1072 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1073 | transport_controller_->SignalConnectionState.connect( |
| 1074 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1075 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1076 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1077 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1078 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1079 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1080 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1081 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1082 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 1083 | |
| 1084 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1085 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1086 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1087 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1088 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1089 | configuration_ = configuration; |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1090 | use_media_transport_ = configuration.use_media_transport; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1091 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1092 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1093 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1094 | if (!configuration.certificates.empty()) { |
| 1095 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1096 | // just picking the first one. The decision should be made based on the DTLS |
| 1097 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1098 | certificate = configuration.certificates[0]; |
| 1099 | } |
| 1100 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1101 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1102 | |
| 1103 | if (options.disable_encryption) { |
| 1104 | dtls_enabled_ = false; |
| 1105 | } else { |
| 1106 | // 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] | 1107 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1108 | // |configuration| can override the default |dtls_enabled_| value. |
| 1109 | if (configuration.enable_dtls_srtp) { |
| 1110 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1111 | } |
| 1112 | } |
| 1113 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 1114 | if (configuration.use_media_transport_for_data_channels) { |
| 1115 | if (configuration.enable_rtp_data_channel) { |
| 1116 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1117 | "use_media_transport_for_data_channels are " |
| 1118 | "incompatible and cannot both be set to true"; |
| 1119 | return false; |
| 1120 | } |
| 1121 | data_channel_type_ = cricket::DCT_MEDIA_TRANSPORT; |
| 1122 | } else if (configuration.enable_rtp_data_channel) { |
| 1123 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1124 | // set. It takes precendence over the disable_sctp_data_channels |
| 1125 | // PeerConnectionFactoryInterface::Options. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1126 | data_channel_type_ = cricket::DCT_RTP; |
| 1127 | } else { |
| 1128 | // DTLS has to be enabled to use SCTP. |
| 1129 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1130 | data_channel_type_ = cricket::DCT_SCTP; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | video_options_.screencast_min_bitrate_kbps = |
| 1135 | configuration.screencast_min_bitrate; |
| 1136 | audio_options_.combined_audio_video_bwe = |
| 1137 | configuration.combined_audio_video_bwe; |
| 1138 | |
| 1139 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1140 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1141 | |
| 1142 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1143 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1144 | |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 1145 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1146 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1147 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 1148 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1149 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1150 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1151 | // Whether the certificate generator/certificate is null or not determines |
| 1152 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1153 | // the right instructions by clearing the variables if needed. |
| 1154 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1155 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1156 | certificate = nullptr; |
| 1157 | } else if (certificate) { |
| 1158 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1159 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1163 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1164 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1165 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1166 | this, &PeerConnection::OnCertificateReady); |
| 1167 | |
| 1168 | if (options.disable_encryption) { |
| 1169 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1170 | } |
| 1171 | |
| 1172 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1173 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1174 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1175 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1176 | // Add default audio/video transceivers for Plan B SDP. |
| 1177 | if (!IsUnifiedPlan()) { |
| 1178 | transceivers_.push_back( |
| 1179 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1180 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1181 | transceivers_.push_back( |
| 1182 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1183 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1184 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1185 | int delay_ms = |
| 1186 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1187 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1188 | MSG_REPORT_USAGE_PATTERN, nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1189 | return true; |
| 1190 | } |
| 1191 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1192 | RTCError PeerConnection::ValidateConfiguration( |
| 1193 | const RTCConfiguration& config) const { |
| 1194 | if (config.ice_regather_interval_range && |
| 1195 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1196 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1197 | "ice_regather_interval_range specified but continual " |
| 1198 | "gathering policy is GATHER_ONCE"); |
| 1199 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1200 | auto result = |
| 1201 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1202 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1205 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1206 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1207 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1208 | "Plan SdpSemantics. Please use GetSenders " |
| 1209 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1210 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1213 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1214 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1215 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1216 | "Plan SdpSemantics. Please use GetReceivers " |
| 1217 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1218 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1221 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1222 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1223 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1224 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1225 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1226 | if (IsClosed()) { |
| 1227 | return false; |
| 1228 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1229 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1230 | return false; |
| 1231 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1232 | |
| 1233 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1234 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1235 | observer->SignalAudioTrackAdded.connect(this, |
| 1236 | &PeerConnection::OnAudioTrackAdded); |
| 1237 | observer->SignalAudioTrackRemoved.connect( |
| 1238 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1239 | observer->SignalVideoTrackAdded.connect(this, |
| 1240 | &PeerConnection::OnVideoTrackAdded); |
| 1241 | observer->SignalVideoTrackRemoved.connect( |
| 1242 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1243 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1244 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1245 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1246 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1247 | } |
| 1248 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1249 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1252 | stats_->AddStream(local_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1253 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1254 | return true; |
| 1255 | } |
| 1256 | |
| 1257 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1258 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1259 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1260 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1261 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1262 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1263 | if (!IsClosed()) { |
| 1264 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1265 | RemoveAudioTrack(track.get(), local_stream); |
| 1266 | } |
| 1267 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1268 | RemoveVideoTrack(track.get(), local_stream); |
| 1269 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1270 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1271 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1272 | stream_observers_.erase( |
| 1273 | std::remove_if( |
| 1274 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1275 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1276 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1277 | }), |
| 1278 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1279 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1280 | if (IsClosed()) { |
| 1281 | return; |
| 1282 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1283 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1286 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1287 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1288 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1289 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1290 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1291 | if (!track) { |
| 1292 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1293 | } |
| 1294 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1295 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1296 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1297 | "Track has invalid kind: " + track->kind()); |
| 1298 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1299 | if (IsClosed()) { |
| 1300 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1301 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1302 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1303 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1304 | LOG_AND_RETURN_ERROR( |
| 1305 | RTCErrorType::INVALID_PARAMETER, |
| 1306 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1307 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1308 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1309 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1310 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1311 | if (sender_or_error.ok()) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1312 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1313 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1314 | } |
| 1315 | return sender_or_error; |
| 1316 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1317 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1318 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1319 | PeerConnection::AddTrackPlanB( |
| 1320 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1321 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1322 | if (stream_ids.size() > 1u) { |
| 1323 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1324 | "AddTrack with more than one stream is not " |
| 1325 | "supported with Plan B semantics."); |
| 1326 | } |
| 1327 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1328 | if (adjusted_stream_ids.empty()) { |
| 1329 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1330 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1331 | cricket::MediaType media_type = |
| 1332 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1333 | ? cricket::MEDIA_TYPE_AUDIO |
| 1334 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1335 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1336 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1337 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1338 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1339 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1340 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1341 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1342 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1343 | if (sender_info) { |
| 1344 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1345 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1346 | } else { |
| 1347 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1348 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1349 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1350 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1351 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1352 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1353 | if (sender_info) { |
| 1354 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1355 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1356 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1357 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1358 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1359 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1360 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1361 | PeerConnection::AddTrackUnifiedPlan( |
| 1362 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1363 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1364 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1365 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1366 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1367 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1368 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1369 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1370 | transceiver->internal()->set_direction( |
| 1371 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1372 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1373 | transceiver->internal()->set_direction( |
| 1374 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1375 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1376 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1377 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1378 | } else { |
| 1379 | cricket::MediaType media_type = |
| 1380 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1381 | ? cricket::MEDIA_TYPE_AUDIO |
| 1382 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1383 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1384 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1385 | std::string sender_id = track->id(); |
| 1386 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1387 | // This can happen if this is the second time AddTrack has created a sender |
| 1388 | // for this track. |
| 1389 | if (FindSenderById(sender_id)) { |
| 1390 | sender_id = rtc::CreateRandomUuid(); |
| 1391 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1392 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1393 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1394 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1395 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1396 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1397 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1398 | return transceiver->sender(); |
| 1399 | } |
| 1400 | |
| 1401 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1402 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1403 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1404 | RTC_DCHECK(track); |
| 1405 | for (auto transceiver : transceivers_) { |
| 1406 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1407 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1408 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1409 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1410 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1411 | return transceiver; |
| 1412 | } |
| 1413 | } |
| 1414 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1418 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1419 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1422 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1423 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1424 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1425 | if (!sender) { |
| 1426 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1427 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1428 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1429 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1430 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1431 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1432 | if (IsUnifiedPlan()) { |
| 1433 | auto transceiver = FindTransceiverBySender(sender); |
| 1434 | if (!transceiver || !sender->track()) { |
| 1435 | return RTCError::OK(); |
| 1436 | } |
| 1437 | sender->SetTrack(nullptr); |
| 1438 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1439 | transceiver->internal()->set_direction( |
| 1440 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1441 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1442 | transceiver->internal()->set_direction( |
| 1443 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1444 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1445 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1446 | bool removed; |
| 1447 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1448 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1449 | } else { |
| 1450 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1451 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1452 | } |
| 1453 | if (!removed) { |
| 1454 | LOG_AND_RETURN_ERROR( |
| 1455 | RTCErrorType::INVALID_PARAMETER, |
| 1456 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1457 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1458 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1459 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1460 | return RTCError::OK(); |
| 1461 | } |
| 1462 | |
| 1463 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1464 | PeerConnection::FindTransceiverBySender( |
| 1465 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1466 | for (auto transceiver : transceivers_) { |
| 1467 | if (transceiver->sender() == sender) { |
| 1468 | return transceiver; |
| 1469 | } |
| 1470 | } |
| 1471 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1472 | } |
| 1473 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1474 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1475 | PeerConnection::AddTransceiver( |
| 1476 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1477 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1478 | } |
| 1479 | |
| 1480 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1481 | PeerConnection::AddTransceiver( |
| 1482 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1483 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1484 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1485 | RTC_CHECK(IsUnifiedPlan()) |
| 1486 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1487 | if (!track) { |
| 1488 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1489 | } |
| 1490 | cricket::MediaType media_type; |
| 1491 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1492 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1493 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1494 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1495 | } else { |
| 1496 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1497 | "Track kind is not audio or video"); |
| 1498 | } |
| 1499 | return AddTransceiver(media_type, track, init); |
| 1500 | } |
| 1501 | |
| 1502 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1503 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1504 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1505 | } |
| 1506 | |
| 1507 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1508 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1509 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1510 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1511 | RTC_CHECK(IsUnifiedPlan()) |
| 1512 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1513 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1514 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1515 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1516 | "media type is not audio or video"); |
| 1517 | } |
| 1518 | return AddTransceiver(media_type, nullptr, init); |
| 1519 | } |
| 1520 | |
| 1521 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1522 | PeerConnection::AddTransceiver( |
| 1523 | cricket::MediaType media_type, |
| 1524 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1525 | const RtpTransceiverInit& init, |
| 1526 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1527 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1528 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1529 | if (track) { |
| 1530 | RTC_DCHECK_EQ(media_type, |
| 1531 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1532 | ? cricket::MEDIA_TYPE_AUDIO |
| 1533 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1534 | } |
| 1535 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 1536 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1537 | init.send_encodings.size(), 0, 7, 8); |
| 1538 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1539 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1540 | [](const RtpEncodingParameters& encoding) { |
| 1541 | return !encoding.rid.empty(); |
| 1542 | }); |
| 1543 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1544 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1545 | RTCErrorType::INVALID_PARAMETER, |
| 1546 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1549 | if (absl::c_any_of(init.send_encodings, |
| 1550 | [](const RtpEncodingParameters& encoding) { |
| 1551 | return encoding.ssrc.has_value(); |
| 1552 | })) { |
| 1553 | LOG_AND_RETURN_ERROR( |
| 1554 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1555 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | RtpParameters parameters; |
| 1559 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1560 | |
| 1561 | // Encodings are dropped from the tail if too many are provided. |
| 1562 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1563 | parameters.encodings.erase( |
| 1564 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1565 | parameters.encodings.end()); |
| 1566 | } |
| 1567 | |
| 1568 | // Single RID should be removed. |
| 1569 | if (parameters.encodings.size() == 1 && |
| 1570 | !parameters.encodings[0].rid.empty()) { |
| 1571 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1572 | parameters.encodings[0].rid.clear(); |
| 1573 | } |
| 1574 | |
| 1575 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1576 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1577 | rtc::UniqueStringGenerator rid_generator; |
| 1578 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1579 | encoding.rid = rid_generator(); |
| 1580 | } |
| 1581 | } |
| 1582 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1583 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1584 | LOG_AND_RETURN_ERROR( |
| 1585 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1586 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1587 | } |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1588 | |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 1589 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1590 | if (!result.ok()) { |
| 1591 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1592 | } |
| 1593 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1594 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1595 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1596 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1597 | // that sender ID is already in use. |
| 1598 | std::string sender_id = |
| 1599 | (track && !FindSenderById(track->id()) ? track->id() |
| 1600 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1601 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1602 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1603 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1604 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1605 | transceiver->internal()->set_direction(init.direction); |
| 1606 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1607 | if (fire_callback) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1608 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1609 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1610 | |
| 1611 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1612 | } |
| 1613 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1614 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1615 | PeerConnection::CreateSender( |
| 1616 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1617 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1618 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1619 | const std::vector<std::string>& stream_ids, |
| 1620 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1621 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1622 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1623 | RTC_DCHECK(!track || |
| 1624 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1625 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1626 | signaling_thread(), |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1627 | AudioRtpSender::Create(worker_thread(), id, stats_.get())); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1628 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1629 | } else { |
| 1630 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1631 | RTC_DCHECK(!track || |
| 1632 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1633 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1634 | signaling_thread(), VideoRtpSender::Create(worker_thread(), id)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1635 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1636 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1637 | bool set_track_succeeded = sender->SetTrack(track); |
| 1638 | RTC_DCHECK(set_track_succeeded); |
| 1639 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1640 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1641 | return sender; |
| 1642 | } |
| 1643 | |
| 1644 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1645 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1646 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1647 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1648 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1649 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1650 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1651 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1652 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1653 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1654 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1655 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1656 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1657 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1658 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1659 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1660 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1661 | return receiver; |
| 1662 | } |
| 1663 | |
| 1664 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1665 | PeerConnection::CreateAndAddTransceiver( |
| 1666 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1667 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1668 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1669 | // Ensure that the new sender does not have an ID that is already in use by |
| 1670 | // another sender. |
| 1671 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1672 | // could be invalid, but should not cause a crash). |
| 1673 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1674 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1675 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1676 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1677 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1678 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1679 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1680 | } |
| 1681 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1682 | void PeerConnection::OnNegotiationNeeded() { |
| 1683 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1684 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1685 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1686 | } |
| 1687 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1688 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1689 | const std::string& kind, |
| 1690 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1691 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1692 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1693 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1694 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1695 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1696 | if (IsClosed()) { |
| 1697 | return nullptr; |
| 1698 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1699 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1700 | // Internally we need to have one stream with Plan B semantics, so we |
| 1701 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1702 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1703 | if (stream_id.empty()) { |
| 1704 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1705 | RTC_LOG(LS_INFO) |
| 1706 | << "No stream_id specified for sender. Generated stream ID: " |
| 1707 | << stream_ids[0]; |
| 1708 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1709 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1710 | } |
| 1711 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1712 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1713 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1714 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1715 | auto audio_sender = AudioRtpSender::Create( |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1716 | worker_thread(), rtc::CreateRandomUuid(), stats_.get()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1717 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1718 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1719 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1720 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1721 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1722 | auto video_sender = |
| 1723 | VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1724 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1725 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1726 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1727 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1728 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1729 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1730 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1731 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1732 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1733 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1734 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1737 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1738 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1739 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1740 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1741 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1742 | } |
| 1743 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1746 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1747 | PeerConnection::GetSendersInternal() const { |
| 1748 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1749 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1750 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1751 | auto senders = transceiver->internal()->senders(); |
| 1752 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1753 | } |
| 1754 | return all_senders; |
| 1755 | } |
| 1756 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1757 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1758 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1759 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1760 | for (const auto& receiver : GetReceiversInternal()) { |
| 1761 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1762 | } |
| 1763 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1766 | std::vector< |
| 1767 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1768 | PeerConnection::GetReceiversInternal() const { |
| 1769 | std::vector< |
| 1770 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1771 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1772 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1773 | auto receivers = transceiver->internal()->receivers(); |
| 1774 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1775 | receivers.end()); |
| 1776 | } |
| 1777 | return all_receivers; |
| 1778 | } |
| 1779 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1780 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1781 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1782 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1783 | RTC_CHECK(IsUnifiedPlan()) |
| 1784 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1785 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1786 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1787 | all_transceivers.push_back(transceiver); |
| 1788 | } |
| 1789 | return all_transceivers; |
| 1790 | } |
| 1791 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1792 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1793 | MediaStreamTrackInterface* track, |
| 1794 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1795 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1796 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1797 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1798 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1799 | return false; |
| 1800 | } |
| 1801 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1802 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1803 | // The StatsCollector is used to tell if a track is valid because it may |
| 1804 | // remember tracks that the PeerConnection previously removed. |
| 1805 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1806 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1807 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1808 | return false; |
| 1809 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1810 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1811 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1812 | return true; |
| 1813 | } |
| 1814 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1815 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1816 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1817 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1818 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1819 | stats_collector_->GetStatsReport(callback); |
| 1820 | } |
| 1821 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1822 | void PeerConnection::GetStats( |
| 1823 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1824 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1825 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1826 | RTC_DCHECK(callback); |
| 1827 | RTC_DCHECK(stats_collector_); |
| 1828 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1829 | if (selector) { |
| 1830 | for (const auto& proxy_transceiver : transceivers_) { |
| 1831 | for (const auto& proxy_sender : |
| 1832 | proxy_transceiver->internal()->senders()) { |
| 1833 | if (proxy_sender == selector) { |
| 1834 | internal_sender = proxy_sender->internal(); |
| 1835 | break; |
| 1836 | } |
| 1837 | } |
| 1838 | if (internal_sender) |
| 1839 | break; |
| 1840 | } |
| 1841 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1842 | // 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] | 1843 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1844 | // PeerConnection). This means that "all the stats objects representing the |
| 1845 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1846 | // produces an empty stats report. |
| 1847 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1848 | } |
| 1849 | |
| 1850 | void PeerConnection::GetStats( |
| 1851 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1852 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1853 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1854 | RTC_DCHECK(callback); |
| 1855 | RTC_DCHECK(stats_collector_); |
| 1856 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1857 | if (selector) { |
| 1858 | for (const auto& proxy_transceiver : transceivers_) { |
| 1859 | for (const auto& proxy_receiver : |
| 1860 | proxy_transceiver->internal()->receivers()) { |
| 1861 | if (proxy_receiver == selector) { |
| 1862 | internal_receiver = proxy_receiver->internal(); |
| 1863 | break; |
| 1864 | } |
| 1865 | } |
| 1866 | if (internal_receiver) |
| 1867 | break; |
| 1868 | } |
| 1869 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1870 | // 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] | 1871 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1872 | // the PeerConnection). This means that "all the stats objects representing |
| 1873 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1874 | // selector produces an empty stats report. |
| 1875 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1876 | } |
| 1877 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1878 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1879 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1880 | return signaling_state_; |
| 1881 | } |
| 1882 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1883 | PeerConnectionInterface::IceConnectionState |
| 1884 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1885 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1886 | return ice_connection_state_; |
| 1887 | } |
| 1888 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1889 | PeerConnectionInterface::IceConnectionState |
| 1890 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1891 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1892 | return standardized_ice_connection_state_; |
| 1893 | } |
| 1894 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1895 | PeerConnectionInterface::PeerConnectionState |
| 1896 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1897 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1898 | return connection_state_; |
| 1899 | } |
| 1900 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1901 | PeerConnectionInterface::IceGatheringState |
| 1902 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1903 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1904 | return ice_gathering_state_; |
| 1905 | } |
| 1906 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1907 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1908 | const std::string& label, |
| 1909 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 10:17:47 +0100 | [diff] [blame] | 1910 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1911 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1912 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1913 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1914 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1915 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1916 | if (config) { |
| 1917 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1918 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1919 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1920 | InternalCreateDataChannel(label, internal_config.get())); |
| 1921 | if (!channel.get()) { |
| 1922 | return nullptr; |
| 1923 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1924 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1925 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1926 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1927 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1928 | Observer()->OnRenegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1929 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1930 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1931 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1932 | } |
| 1933 | |
| 1934 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1935 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1936 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1937 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1938 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1939 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1940 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1941 | return; |
| 1942 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1943 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1944 | if (IsClosed()) { |
| 1945 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1946 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1947 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1948 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1949 | return; |
| 1950 | } |
| 1951 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1952 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1953 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1954 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1955 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1956 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1957 | return; |
| 1958 | } |
| 1959 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1960 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1961 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1962 | if (IsUnifiedPlan()) { |
| 1963 | RTCError error = HandleLegacyOfferOptions(options); |
| 1964 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1965 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1966 | return; |
| 1967 | } |
| 1968 | } |
| 1969 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1970 | cricket::MediaSessionOptions session_options; |
| 1971 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1972 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1975 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1976 | const RTCOfferAnswerOptions& options) { |
| 1977 | RTC_DCHECK(IsUnifiedPlan()); |
| 1978 | |
| 1979 | if (options.offer_to_receive_audio == 0) { |
| 1980 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1981 | cricket::MEDIA_TYPE_AUDIO); |
| 1982 | } else if (options.offer_to_receive_audio == 1) { |
| 1983 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1984 | } else if (options.offer_to_receive_audio > 1) { |
| 1985 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1986 | "offer_to_receive_audio > 1 is not supported."); |
| 1987 | } |
| 1988 | |
| 1989 | if (options.offer_to_receive_video == 0) { |
| 1990 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1991 | cricket::MEDIA_TYPE_VIDEO); |
| 1992 | } else if (options.offer_to_receive_video == 1) { |
| 1993 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1994 | } else if (options.offer_to_receive_video > 1) { |
| 1995 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1996 | "offer_to_receive_video > 1 is not supported."); |
| 1997 | } |
| 1998 | |
| 1999 | return RTCError::OK(); |
| 2000 | } |
| 2001 | |
| 2002 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2003 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2004 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2005 | RtpTransceiverDirection new_direction = |
| 2006 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 2007 | if (new_direction != transceiver->direction()) { |
| 2008 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 2009 | << " transceiver (MID=" |
| 2010 | << transceiver->mid().value_or("<not set>") << ") from " |
| 2011 | << RtpTransceiverDirectionToString( |
| 2012 | transceiver->direction()) |
| 2013 | << " to " |
| 2014 | << RtpTransceiverDirectionToString(new_direction) |
| 2015 | << " since CreateOffer specified offer_to_receive=0"; |
| 2016 | transceiver->internal()->set_direction(new_direction); |
| 2017 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 2022 | cricket::MediaType media_type) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 2023 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2024 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2025 | RTC_LOG(LS_INFO) |
| 2026 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 2027 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2028 | RtpTransceiverInit init; |
| 2029 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 2030 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2035 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 2036 | std::vector< |
| 2037 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2038 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2039 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2040 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2041 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 2042 | receiving_transceivers.push_back(transceiver); |
| 2043 | } |
| 2044 | } |
| 2045 | return receiving_transceivers; |
| 2046 | } |
| 2047 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2048 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 2049 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2050 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2051 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2052 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2053 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2054 | return; |
| 2055 | } |
| 2056 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2057 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 2058 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 2059 | std::string error = |
| 2060 | "PeerConnection cannot create an answer in a state other than " |
| 2061 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2062 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2063 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2064 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2065 | return; |
| 2066 | } |
| 2067 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2068 | // The remote description should be set if we're in the right state. |
| 2069 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2070 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2071 | if (IsUnifiedPlan()) { |
| 2072 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2073 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2074 | "supported with Unified Plan semantics. Use the " |
| 2075 | "RtpTransceiver API instead."; |
| 2076 | } |
| 2077 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2078 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2079 | "supported with Unified Plan semantics. Use the " |
| 2080 | "RtpTransceiver API instead."; |
| 2081 | } |
| 2082 | } |
| 2083 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2084 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2085 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2086 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2087 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | void PeerConnection::SetLocalDescription( |
| 2091 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2092 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2093 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2094 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2095 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2096 | // The SetLocalDescription contract is that we take ownership of the session |
| 2097 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 2098 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 2099 | // description as a unique_ptr argument to formalize this agreement. |
| 2100 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 2101 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2102 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2103 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2104 | return; |
| 2105 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2106 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2107 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2108 | PostSetSessionDescriptionFailure( |
| 2109 | observer, |
| 2110 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2111 | return; |
| 2112 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2113 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2114 | // If a session error has occurred the PeerConnection is in a possibly |
| 2115 | // inconsistent state so fail right away. |
| 2116 | if (session_error() != SessionError::kNone) { |
| 2117 | std::string error_message = GetSessionErrorMsg(); |
| 2118 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2119 | PostSetSessionDescriptionFailure( |
| 2120 | observer, |
| 2121 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2122 | return; |
| 2123 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2124 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2125 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2126 | if (!error.ok()) { |
| 2127 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2128 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2129 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2130 | PostSetSessionDescriptionFailure( |
| 2131 | observer, |
| 2132 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2133 | return; |
| 2134 | } |
| 2135 | |
| 2136 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2137 | // which may destroy it before returning. |
| 2138 | const SdpType type = desc->GetType(); |
| 2139 | |
| 2140 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2141 | // |desc| may be destroyed at this point. |
| 2142 | |
| 2143 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2144 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2145 | // inconsistent state, so act conservatively here and set the session error |
| 2146 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2147 | SetSessionError(SessionError::kContent, error.message()); |
| 2148 | std::string error_message = |
| 2149 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2150 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2151 | PostSetSessionDescriptionFailure( |
| 2152 | observer, |
| 2153 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2154 | return; |
| 2155 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2156 | RTC_DCHECK(local_description()); |
| 2157 | |
| 2158 | PostSetSessionDescriptionSuccess(observer); |
| 2159 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 2160 | // MaybeStartGathering needs to be called after posting |
| 2161 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2162 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2163 | transport_controller_->MaybeStartGathering(); |
| 2164 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2165 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2166 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2167 | // MaybeStartGathering... |
| 2168 | network_thread()->Invoke<void>( |
| 2169 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2170 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2171 | // Make UMA notes about what was agreed to. |
| 2172 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2173 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2174 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | RTCError PeerConnection::ApplyLocalDescription( |
| 2178 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2179 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2180 | RTC_DCHECK(desc); |
| 2181 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2182 | // Update stats here so that we have the most recent stats for tracks and |
| 2183 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2184 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2185 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2186 | // Take a reference to the old local description since it's used below to |
| 2187 | // compare against the new local description. When setting the new local |
| 2188 | // description, grab ownership of the replaced session description in case it |
| 2189 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2190 | // of the method. |
| 2191 | const SessionDescriptionInterface* old_local_description = |
| 2192 | local_description(); |
| 2193 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2194 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2195 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2196 | replaced_local_description = pending_local_description_ |
| 2197 | ? std::move(pending_local_description_) |
| 2198 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2199 | current_local_description_ = std::move(desc); |
| 2200 | pending_local_description_ = nullptr; |
| 2201 | current_remote_description_ = std::move(pending_remote_description_); |
| 2202 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2203 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2204 | pending_local_description_ = std::move(desc); |
| 2205 | } |
| 2206 | // The session description to apply now must be accessed by |
| 2207 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2208 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2209 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 2210 | // Report statistics about any use of simulcast. |
| 2211 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 2212 | *local_description()->description()); |
| 2213 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2214 | if (!is_caller_) { |
| 2215 | if (remote_description()) { |
| 2216 | // Remote description was applied first, so this PC is the callee. |
| 2217 | is_caller_ = false; |
| 2218 | } else { |
| 2219 | // Local description is applied first, so this PC is the caller. |
| 2220 | is_caller_ = true; |
| 2221 | } |
| 2222 | } |
| 2223 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2224 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2225 | if (!error.ok()) { |
| 2226 | return error; |
| 2227 | } |
| 2228 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2229 | if (IsUnifiedPlan()) { |
| 2230 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2231 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2232 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2233 | if (!error.ok()) { |
| 2234 | return error; |
| 2235 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2236 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2237 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2238 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2239 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2240 | // Note that code paths that don't set MID won't be able to use |
| 2241 | // information about DTLS transports. |
| 2242 | if (transceiver->mid()) { |
| 2243 | auto dtls_transport = |
| 2244 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2245 | transceiver->internal()->sender_internal()->set_transport( |
| 2246 | dtls_transport); |
| 2247 | transceiver->internal()->receiver_internal()->set_transport( |
| 2248 | dtls_transport); |
| 2249 | } |
| 2250 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2251 | const ContentInfo* content = |
| 2252 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2253 | if (!content) { |
| 2254 | continue; |
| 2255 | } |
| 2256 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2257 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2258 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2259 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2260 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2261 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2262 | // "recvonly", process the removal of a remote track for the media |
| 2263 | // description, given transceiver, removeList, and muteTracks. |
| 2264 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2265 | (transceiver->internal()->fired_direction() && |
| 2266 | RtpTransceiverDirectionHasRecv( |
| 2267 | *transceiver->internal()->fired_direction()))) { |
| 2268 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2269 | &removed_streams); |
| 2270 | } |
| 2271 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2272 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2273 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2274 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2275 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2276 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2277 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2278 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2279 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2280 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2281 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2282 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2283 | } |
| 2284 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2285 | // Media channels will be created only when offer is set. These may use new |
| 2286 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2287 | if (type == SdpType::kOffer) { |
| 2288 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2289 | // description is applied. Restore back to old description. |
| 2290 | RTCError error = CreateChannels(*local_description()->description()); |
| 2291 | if (!error.ok()) { |
| 2292 | return error; |
| 2293 | } |
| 2294 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2295 | // Remove unused channels if MediaContentDescription is rejected. |
| 2296 | RemoveUnusedChannels(local_description()->description()); |
| 2297 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2298 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2299 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2300 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2301 | if (!error.ok()) { |
| 2302 | return error; |
| 2303 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2304 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2305 | if (remote_description()) { |
| 2306 | // Now that we have a local description, we can push down remote candidates. |
| 2307 | UseCandidatesInSessionDescription(remote_description()); |
| 2308 | } |
| 2309 | |
| 2310 | pending_ice_restarts_.clear(); |
| 2311 | if (session_error() != SessionError::kNone) { |
| 2312 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2313 | } |
| 2314 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2315 | // If setting the description decided our SSL role, allocate any necessary |
| 2316 | // SCTP sids. |
| 2317 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2318 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2319 | AllocateSctpSids(role); |
| 2320 | } |
| 2321 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2322 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2323 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2324 | const ContentInfo* content = |
| 2325 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2326 | if (!content) { |
| 2327 | continue; |
| 2328 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2329 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2330 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2331 | // 0 is a special value meaning "this sender has no associated send |
| 2332 | // stream". Need to call this so the sender won't attempt to configure |
| 2333 | // a no longer existing stream and run into DCHECKs in the lower |
| 2334 | // layers. |
| 2335 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2336 | } else { |
| 2337 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2338 | const std::vector<StreamParams>& streams = channel->local_streams(); |
| 2339 | transceiver->internal()->sender_internal()->set_stream_ids( |
| 2340 | streams[0].stream_ids()); |
| 2341 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2342 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | } else { |
| 2346 | // Plan B semantics. |
| 2347 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2348 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2349 | // local session description. |
| 2350 | const cricket::ContentInfo* audio_content = |
| 2351 | GetFirstAudioContent(local_description()->description()); |
| 2352 | if (audio_content) { |
| 2353 | if (audio_content->rejected) { |
| 2354 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2355 | } else { |
| 2356 | const cricket::AudioContentDescription* audio_desc = |
| 2357 | audio_content->media_description()->as_audio(); |
| 2358 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2359 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2360 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2361 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2362 | const cricket::ContentInfo* video_content = |
| 2363 | GetFirstVideoContent(local_description()->description()); |
| 2364 | if (video_content) { |
| 2365 | if (video_content->rejected) { |
| 2366 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2367 | } else { |
| 2368 | const cricket::VideoContentDescription* video_desc = |
| 2369 | video_content->media_description()->as_video(); |
| 2370 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2371 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2372 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2376 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2377 | if (data_content) { |
| 2378 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2379 | data_content->media_description()->as_data(); |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 2380 | if (absl::StartsWith(data_desc->protocol(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2381 | cricket::kMediaProtocolRtpPrefix)) { |
| 2382 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2383 | } |
| 2384 | } |
| 2385 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2386 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2389 | // The SDP parser used to populate these values by default for the 'content |
| 2390 | // name' if an a=mid line was absent. |
| 2391 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2392 | switch (media_type) { |
| 2393 | case cricket::MEDIA_TYPE_AUDIO: |
| 2394 | return cricket::CN_AUDIO; |
| 2395 | case cricket::MEDIA_TYPE_VIDEO: |
| 2396 | return cricket::CN_VIDEO; |
| 2397 | case cricket::MEDIA_TYPE_DATA: |
| 2398 | return cricket::CN_DATA; |
| 2399 | } |
| 2400 | RTC_NOTREACHED(); |
| 2401 | return ""; |
| 2402 | } |
| 2403 | |
| 2404 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2405 | cricket::SessionDescription* new_remote_description) { |
| 2406 | RTC_DCHECK(new_remote_description); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2407 | const cricket::ContentInfos& local_contents = |
| 2408 | (local_description() ? local_description()->description()->contents() |
| 2409 | : cricket::ContentInfos()); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2410 | const cricket::ContentInfos& remote_contents = |
| 2411 | (remote_description() ? remote_description()->description()->contents() |
| 2412 | : cricket::ContentInfos()); |
| 2413 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2414 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2415 | if (!content.name.empty()) { |
| 2416 | continue; |
| 2417 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2418 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2419 | absl::string_view source_explanation; |
| 2420 | if (IsUnifiedPlan()) { |
| 2421 | if (i < local_contents.size()) { |
| 2422 | new_mid = local_contents[i].name; |
| 2423 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2424 | } else if (i < remote_contents.size()) { |
| 2425 | new_mid = remote_contents[i].name; |
| 2426 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2427 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2428 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2429 | source_explanation = "generated just now"; |
| 2430 | } |
| 2431 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2432 | new_mid = std::string( |
| 2433 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2434 | source_explanation = "to match pre-existing behavior"; |
| 2435 | } |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2436 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2437 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2438 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2439 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2440 | << " is missing an a=mid line. Filling in the value '" |
| 2441 | << new_mid << "' " << source_explanation << "."; |
| 2442 | } |
| 2443 | } |
| 2444 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2445 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2446 | SetSessionDescriptionObserver* observer, |
| 2447 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2448 | SetRemoteDescription( |
| 2449 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2450 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2451 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2452 | } |
| 2453 | |
| 2454 | void PeerConnection::SetRemoteDescription( |
| 2455 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2456 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2457 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2458 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2459 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2460 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2461 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2462 | return; |
| 2463 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2464 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2465 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2466 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2467 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2468 | return; |
| 2469 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2470 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2471 | // If a session error has occurred the PeerConnection is in a possibly |
| 2472 | // inconsistent state so fail right away. |
| 2473 | if (session_error() != SessionError::kNone) { |
| 2474 | std::string error_message = GetSessionErrorMsg(); |
| 2475 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2476 | observer->OnSetRemoteDescriptionComplete( |
| 2477 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2478 | return; |
| 2479 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2480 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2481 | if (desc->GetType() == SdpType::kOffer) { |
| 2482 | // Report to UMA the format of the received offer. |
| 2483 | ReportSdpFormatReceived(*desc); |
| 2484 | } |
| 2485 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2486 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 2487 | // points. |
| 2488 | FillInMissingRemoteMids(desc->description()); |
| 2489 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2490 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2491 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2492 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2493 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2494 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2495 | observer->OnSetRemoteDescriptionComplete( |
| 2496 | RTCError(error.type(), std::move(error_message))); |
| 2497 | return; |
| 2498 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2499 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2500 | // Grab the description type before moving ownership to |
| 2501 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2502 | const SdpType type = desc->GetType(); |
| 2503 | |
| 2504 | error = ApplyRemoteDescription(std::move(desc)); |
| 2505 | // |desc| may be destroyed at this point. |
| 2506 | |
| 2507 | if (!error.ok()) { |
| 2508 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2509 | // inconsistent state, so act conservatively here and set the session error |
| 2510 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2511 | SetSessionError(SessionError::kContent, error.message()); |
| 2512 | std::string error_message = |
| 2513 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2514 | RTC_LOG(LS_ERROR) << error_message; |
| 2515 | observer->OnSetRemoteDescriptionComplete( |
| 2516 | RTCError(error.type(), std::move(error_message))); |
| 2517 | return; |
| 2518 | } |
| 2519 | RTC_DCHECK(remote_description()); |
| 2520 | |
| 2521 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2522 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2523 | // MaybeStartGathering... |
| 2524 | network_thread()->Invoke<void>( |
| 2525 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2526 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2527 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2528 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2532 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2536 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2537 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2538 | RTC_DCHECK(desc); |
| 2539 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2540 | // Update stats here so that we have the most recent stats for tracks and |
| 2541 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2542 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2543 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2544 | // Take a reference to the old remote description since it's used below to |
| 2545 | // compare against the new remote description. When setting the new remote |
| 2546 | // description, grab ownership of the replaced session description in case it |
| 2547 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2548 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2549 | const SessionDescriptionInterface* old_remote_description = |
| 2550 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2551 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2552 | SdpType type = desc->GetType(); |
| 2553 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2554 | replaced_remote_description = pending_remote_description_ |
| 2555 | ? std::move(pending_remote_description_) |
| 2556 | : std::move(current_remote_description_); |
| 2557 | current_remote_description_ = std::move(desc); |
| 2558 | pending_remote_description_ = nullptr; |
| 2559 | current_local_description_ = std::move(pending_local_description_); |
| 2560 | } else { |
| 2561 | replaced_remote_description = std::move(pending_remote_description_); |
| 2562 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2563 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2564 | // The session description to apply now must be accessed by |
| 2565 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2566 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2567 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 2568 | // Report statistics about any use of simulcast. |
| 2569 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 2570 | *remote_description()->description()); |
| 2571 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2572 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2573 | if (!error.ok()) { |
| 2574 | return error; |
| 2575 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2576 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2577 | if (IsUnifiedPlan()) { |
| 2578 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2579 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2580 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2581 | if (!error.ok()) { |
| 2582 | return error; |
| 2583 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2584 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2585 | // Media channels will be created only when offer is set. These may use new |
| 2586 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2587 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2588 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2589 | // description is applied. Restore back to old description. |
| 2590 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2591 | if (!error.ok()) { |
| 2592 | return error; |
| 2593 | } |
| 2594 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2595 | // Remove unused channels if MediaContentDescription is rejected. |
| 2596 | RemoveUnusedChannels(remote_description()->description()); |
| 2597 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2598 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2599 | // NOTE: Candidates allocation will be initiated only when |
| 2600 | // SetLocalDescription is called. |
| 2601 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2602 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2603 | if (!error.ok()) { |
| 2604 | return error; |
| 2605 | } |
| 2606 | |
| 2607 | if (local_description() && |
| 2608 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2609 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2610 | } |
| 2611 | |
| 2612 | if (old_remote_description) { |
| 2613 | for (const cricket::ContentInfo& content : |
| 2614 | old_remote_description->description()->contents()) { |
| 2615 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2616 | // password that indicates the remote peer requests an ICE restart. |
| 2617 | // TODO(deadbeef): When we start storing both the current and pending |
| 2618 | // remote description, this should reset pending_ice_restarts and compare |
| 2619 | // against the current description. |
| 2620 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2621 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2622 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2623 | pending_ice_restarts_.insert(content.name); |
| 2624 | } |
| 2625 | } else { |
| 2626 | // We retain all received candidates only if ICE is not restarted. |
| 2627 | // When ICE is restarted, all previous candidates belong to an old |
| 2628 | // generation and should not be kept. |
| 2629 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2630 | // description should only contain candidates from the last set remote |
| 2631 | // description plus any candidates added since then. We should remove |
| 2632 | // this once we're sure it won't break anything. |
| 2633 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2634 | old_remote_description, content.name, mutable_remote_description()); |
| 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | if (session_error() != SessionError::kNone) { |
| 2640 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2641 | } |
| 2642 | |
| 2643 | // Set the the ICE connection state to connecting since the connection may |
| 2644 | // become writable with peer reflexive candidates before any remote candidate |
| 2645 | // is signaled. |
| 2646 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2647 | // is to have a new signal the indicates a change in checking state from the |
| 2648 | // transport and expose a new checking() member from transport that can be |
| 2649 | // read to determine the current checking state. The existing SignalConnecting |
| 2650 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2651 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2652 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2653 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2654 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2655 | } |
| 2656 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2657 | // If setting the description decided our SSL role, allocate any necessary |
| 2658 | // SCTP sids. |
| 2659 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2660 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2661 | AllocateSctpSids(role); |
| 2662 | } |
| 2663 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2664 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2665 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2666 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2667 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2668 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2669 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2670 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2671 | const ContentInfo* content = |
| 2672 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2673 | if (!content) { |
| 2674 | continue; |
| 2675 | } |
| 2676 | const MediaContentDescription* media_desc = content->media_description(); |
| 2677 | RtpTransceiverDirection local_direction = |
| 2678 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2679 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 2680 | // RTCSessionDescription: Set the associated remote streams given |
| 2681 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 2682 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 2683 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2684 | std::vector<std::string> stream_ids; |
| 2685 | if (!media_desc->streams().empty()) { |
| 2686 | // The remote description has signaled the stream IDs. |
| 2687 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2688 | } |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2689 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 2690 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 2691 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 2692 | stream_ids, &added_streams, |
| 2693 | &removed_streams); |
| 2694 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 2695 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 2696 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 2697 | // process the addition of a remote track for the media description. |
| 2698 | if (!transceiver->fired_direction() || |
| 2699 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 2700 | RTC_LOG(LS_INFO) |
| 2701 | << "Processing the addition of a remote track for MID=" |
| 2702 | << content->name << "."; |
| 2703 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 11:25:05 +0100 | [diff] [blame] | 2704 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2705 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2706 | // 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] | 2707 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2708 | // removal of a remote track for the media description, given transceiver, |
| 2709 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2710 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2711 | (transceiver->fired_direction() && |
| 2712 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2713 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2714 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2715 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2716 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2717 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2718 | // 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] | 2719 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2720 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2721 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2722 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2723 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2724 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 2725 | if (transceiver->mid()) { |
| 2726 | auto dtls_transport = |
| 2727 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2728 | transceiver->internal()->sender_internal()->set_transport( |
| 2729 | dtls_transport); |
| 2730 | transceiver->internal()->receiver_internal()->set_transport( |
| 2731 | dtls_transport); |
| 2732 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2733 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2734 | // 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] | 2735 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2736 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2737 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2738 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2739 | transceiver->Stop(); |
| 2740 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2741 | if (!content->rejected && |
| 2742 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2743 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2744 | uint32_t ssrc = 0; |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2745 | if (!media_desc->streams().empty() && |
| 2746 | media_desc->streams()[0].has_ssrcs()) { |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2747 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2748 | } |
| 2749 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2750 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2751 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2752 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2753 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2754 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2755 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2756 | observer->OnTrack(transceiver); |
| 2757 | observer->OnAddTrack(transceiver->receiver(), |
| 2758 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2759 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2760 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2761 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2762 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2763 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2764 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2765 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2766 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2767 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2768 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2769 | } |
| 2770 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2771 | const cricket::ContentInfo* audio_content = |
| 2772 | GetFirstAudioContent(remote_description()->description()); |
| 2773 | const cricket::ContentInfo* video_content = |
| 2774 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2775 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2776 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2777 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2778 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2779 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2780 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2781 | |
| 2782 | // Check if the descriptions include streams, just in case the peer supports |
| 2783 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2784 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2785 | (audio_desc && !audio_desc->streams().empty()) || |
| 2786 | (video_desc && !video_desc->streams().empty())) { |
| 2787 | remote_peer_supports_msid_ = true; |
| 2788 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2789 | |
| 2790 | // We wait to signal new streams until we finish processing the description, |
| 2791 | // since only at that point will new streams have all their tracks. |
| 2792 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2793 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2794 | if (!IsUnifiedPlan()) { |
| 2795 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2796 | // rejected media section, there is some cleanup logic that expects the |
| 2797 | // voice/ video channel to still be set. But in this method the voice/video |
| 2798 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2799 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2800 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2801 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2802 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2803 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2804 | // and MediaStreams. |
| 2805 | if (audio_content) { |
| 2806 | if (audio_content->rejected) { |
| 2807 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2808 | } else { |
| 2809 | bool default_audio_track_needed = |
| 2810 | !remote_peer_supports_msid_ && |
| 2811 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2812 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2813 | default_audio_track_needed, audio_desc->type(), |
| 2814 | new_streams); |
| 2815 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2816 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2817 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2818 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2819 | // and MediaStreams. |
| 2820 | if (video_content) { |
| 2821 | if (video_content->rejected) { |
| 2822 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2823 | } else { |
| 2824 | bool default_video_track_needed = |
| 2825 | !remote_peer_supports_msid_ && |
| 2826 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2827 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2828 | default_video_track_needed, video_desc->type(), |
| 2829 | new_streams); |
| 2830 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2831 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2832 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2833 | // Update the DataChannels with the information from the remote peer. |
| 2834 | if (data_desc) { |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 2835 | if (absl::StartsWith(data_desc->protocol(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2836 | cricket::kMediaProtocolRtpPrefix)) { |
| 2837 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2838 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2839 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2840 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2841 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2842 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2843 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2844 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2845 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2846 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2847 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2848 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2849 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2850 | UpdateEndedRemoteMediaStreams(); |
| 2851 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2852 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2853 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2856 | void PeerConnection::SetAssociatedRemoteStreams( |
| 2857 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 2858 | const std::vector<std::string>& stream_ids, |
| 2859 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 2860 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2861 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2862 | for (const std::string& stream_id : stream_ids) { |
| 2863 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2864 | remote_streams_->find(stream_id); |
| 2865 | if (!stream) { |
| 2866 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2867 | MediaStream::Create(stream_id)); |
| 2868 | remote_streams_->AddStream(stream); |
| 2869 | added_streams->push_back(stream); |
| 2870 | } |
| 2871 | media_streams.push_back(stream); |
| 2872 | } |
| 2873 | // Special case: "a=msid" missing, use random stream ID. |
| 2874 | if (media_streams.empty() && |
| 2875 | !(remote_description()->description()->msid_signaling() & |
| 2876 | cricket::kMsidSignalingMediaSection)) { |
| 2877 | if (!missing_msid_default_stream_) { |
| 2878 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 2879 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 2880 | added_streams->push_back(missing_msid_default_stream_); |
| 2881 | } |
| 2882 | media_streams.push_back(missing_msid_default_stream_); |
| 2883 | } |
| 2884 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 2885 | receiver->streams(); |
| 2886 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 2887 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 2888 | // update the stream-track relationships in a later step. We do this earlier, |
| 2889 | // changing the order of things, but the end-result is the same. |
| 2890 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 2891 | // instead. https://crbug.com/webrtc/9480 |
| 2892 | receiver->SetStreams(media_streams); |
| 2893 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2894 | } |
| 2895 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2896 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 2897 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2898 | transceiver, |
| 2899 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 2900 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2901 | RTC_DCHECK(transceiver->mid()); |
| 2902 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2903 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2904 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2905 | transceiver->internal()->receiver_internal()->streams(); |
| 2906 | // This will remove the remote track from the streams. |
| 2907 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 2908 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2909 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2910 | } |
| 2911 | |
| 2912 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 2913 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 2914 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2915 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 2916 | // streams, see if the stream was removed by checking if this was the last |
| 2917 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2918 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2919 | if (remote_stream->GetAudioTracks().empty() && |
| 2920 | remote_stream->GetVideoTracks().empty()) { |
| 2921 | remote_streams_->RemoveStream(remote_stream); |
| 2922 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2927 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2928 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2929 | const SessionDescriptionInterface& new_session, |
| 2930 | const SessionDescriptionInterface* old_local_description, |
| 2931 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2932 | RTC_DCHECK(IsUnifiedPlan()); |
| 2933 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2934 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2935 | if (new_session.GetType() == SdpType::kOffer) { |
| 2936 | auto bundle_group_or_error = |
| 2937 | GetEarlyBundleGroup(*new_session.description()); |
| 2938 | if (!bundle_group_or_error.ok()) { |
| 2939 | return bundle_group_or_error.MoveError(); |
| 2940 | } |
| 2941 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2942 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2943 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2944 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2945 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2946 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2947 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2948 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2949 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2950 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2951 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2952 | if (old_local_description && |
| 2953 | i < old_local_description->description()->contents().size()) { |
| 2954 | old_local_content = |
| 2955 | &old_local_description->description()->contents()[i]; |
| 2956 | } |
| 2957 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2958 | if (old_remote_description && |
| 2959 | i < old_remote_description->description()->contents().size()) { |
| 2960 | old_remote_content = |
| 2961 | &old_remote_description->description()->contents()[i]; |
| 2962 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2963 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2964 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2965 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2966 | if (!transceiver_or_error.ok()) { |
| 2967 | return transceiver_or_error.MoveError(); |
| 2968 | } |
| 2969 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2970 | RTCError error = |
| 2971 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2972 | if (!error.ok()) { |
| 2973 | return error; |
| 2974 | } |
| 2975 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2976 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2977 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2978 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 2979 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2980 | continue; |
| 2981 | } |
| 2982 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2983 | if (!error.ok()) { |
| 2984 | return error; |
| 2985 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2986 | } else { |
| 2987 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2988 | "Unknown section type."); |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | return RTCError::OK(); |
| 2993 | } |
| 2994 | |
| 2995 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2996 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2997 | transceiver, |
| 2998 | const cricket::ContentInfo& content, |
| 2999 | const cricket::ContentGroup* bundle_group) { |
| 3000 | RTC_DCHECK(IsUnifiedPlan()); |
| 3001 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3002 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3003 | if (content.rejected) { |
| 3004 | if (channel) { |
| 3005 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3006 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3007 | } |
| 3008 | } else { |
| 3009 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3010 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3011 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3012 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3013 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3014 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3015 | } |
| 3016 | if (!channel) { |
| 3017 | LOG_AND_RETURN_ERROR( |
| 3018 | RTCErrorType::INTERNAL_ERROR, |
| 3019 | "Failed to create channel for mid=" + content.name); |
| 3020 | } |
| 3021 | transceiver->internal()->SetChannel(channel); |
| 3022 | } |
| 3023 | } |
| 3024 | return RTCError::OK(); |
| 3025 | } |
| 3026 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3027 | RTCError PeerConnection::UpdateDataChannel( |
| 3028 | cricket::ContentSource source, |
| 3029 | const cricket::ContentInfo& content, |
| 3030 | const cricket::ContentGroup* bundle_group) { |
| 3031 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3032 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3033 | // will take care of rejecting it. |
| 3034 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3035 | } |
| 3036 | if (content.rejected) { |
| 3037 | DestroyDataChannel(); |
| 3038 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 3039 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3040 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3041 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3042 | "Failed to create data channel."); |
| 3043 | } |
| 3044 | } |
| 3045 | if (source == cricket::CS_REMOTE) { |
| 3046 | const MediaContentDescription* data_desc = content.media_description(); |
| 3047 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 3048 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | return RTCError::OK(); |
| 3053 | } |
| 3054 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3055 | // This method will extract any send encodings that were sent by the remote |
| 3056 | // connection. This is currently only relevant for Simulcast scenario (where |
| 3057 | // the number of layers may be communicated by the server). |
| 3058 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 3059 | const MediaContentDescription& desc) { |
| 3060 | if (!desc.HasSimulcast()) { |
| 3061 | return {}; |
| 3062 | } |
| 3063 | std::vector<RtpEncodingParameters> result; |
| 3064 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 3065 | |
| 3066 | // This is a remote description, the parameters we are after should appear |
| 3067 | // as receive streams. |
| 3068 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 3069 | RTC_DCHECK(!alternatives.empty()); |
| 3070 | // There is currently no way to specify or choose from alternatives. |
| 3071 | // We will always use the first alternative, which is the most preferred. |
| 3072 | const SimulcastLayer& layer = alternatives[0]; |
| 3073 | RtpEncodingParameters parameters; |
| 3074 | parameters.rid = layer.rid; |
| 3075 | parameters.active = !layer.is_paused; |
| 3076 | result.push_back(parameters); |
| 3077 | } |
| 3078 | |
| 3079 | return result; |
| 3080 | } |
| 3081 | |
| 3082 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 3083 | const std::vector<SimulcastLayer>& layers, |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3084 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3085 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3086 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3087 | std::vector<std::string> disabled_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3088 | |
| 3089 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 3090 | // So we will iterate over the send encodings rather than the layers. |
| 3091 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3092 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 3093 | [&encoding](const SimulcastLayer& layer) { |
| 3094 | return layer.rid == encoding.rid; |
| 3095 | }); |
| 3096 | // A layer that cannot be found may have been removed by the remote party. |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3097 | if (iter == layers.end()) { |
| 3098 | disabled_layers.push_back(encoding.rid); |
| 3099 | continue; |
| 3100 | } |
| 3101 | |
| 3102 | encoding.active = !iter->is_paused; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3103 | } |
| 3104 | |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3105 | RTCError result = sender->SetParametersInternal(parameters); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3106 | if (result.ok()) { |
| 3107 | result = sender->DisableEncodingLayers(disabled_layers); |
| 3108 | } |
| 3109 | |
| 3110 | return result; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3111 | } |
| 3112 | |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3113 | static bool SimulcastIsRejected( |
| 3114 | const ContentInfo* local_content, |
| 3115 | const MediaContentDescription& answer_media_desc) { |
| 3116 | bool simulcast_offered = local_content && |
| 3117 | local_content->media_description() && |
| 3118 | local_content->media_description()->HasSimulcast(); |
| 3119 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 3120 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 3121 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 3122 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 3123 | } |
| 3124 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3125 | static RTCError DisableSimulcastInSender( |
| 3126 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3127 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3128 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3129 | if (parameters.encodings.size() <= 1) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3130 | return RTCError::OK(); |
| 3131 | } |
| 3132 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3133 | std::vector<std::string> disabled_layers; |
| 3134 | std::transform( |
| 3135 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 3136 | std::back_inserter(disabled_layers), |
| 3137 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 3138 | return sender->DisableEncodingLayers(disabled_layers); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3139 | } |
| 3140 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3141 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3142 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3143 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3144 | size_t mline_index, |
| 3145 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3146 | const ContentInfo* old_local_content, |
| 3147 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3148 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3149 | // If this is an offer then the m= section might be recycled. If the m= |
| 3150 | // section is being recycled (defined as: rejected in the current local or |
| 3151 | // remote description and not rejected in new description), dissociate the |
| 3152 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3153 | // and discard the mapping between the transceiver and its m= section index. |
| 3154 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3155 | old_remote_content)) { |
| 3156 | // We want to dissociate the transceiver that has the rejected mid. |
| 3157 | const std::string& old_mid = |
| 3158 | (old_local_content && old_local_content->rejected) |
| 3159 | ? old_local_content->name |
| 3160 | : old_remote_content->name; |
| 3161 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3162 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3163 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3164 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3165 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3166 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3167 | } |
| 3168 | } |
| 3169 | const MediaContentDescription* media_desc = content.media_description(); |
| 3170 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3171 | if (source == cricket::CS_LOCAL) { |
| 3172 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3173 | // mapping between transceivers and m= section indices established when |
| 3174 | // creating the offer. |
| 3175 | if (!transceiver) { |
| 3176 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3177 | } |
| 3178 | if (!transceiver) { |
| 3179 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3180 | "Unknown transceiver"); |
| 3181 | } |
| 3182 | } else { |
| 3183 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3184 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3185 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3186 | // When simulcast is requested, a transceiver cannot be associated because |
| 3187 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3188 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3189 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3190 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3191 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3192 | } |
| 3193 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3194 | // recvonly direction. |
| 3195 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3196 | RTC_LOG(LS_INFO) << "Adding " |
| 3197 | << cricket::MediaTypeToString(media_desc->type()) |
| 3198 | << " transceiver for MID=" << content.name |
| 3199 | << " at i=" << mline_index |
| 3200 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3201 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3202 | std::vector<RtpEncodingParameters> send_encodings = |
| 3203 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3204 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3205 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3206 | std::string receiver_id; |
| 3207 | if (!media_desc->streams().empty()) { |
| 3208 | receiver_id = media_desc->streams()[0].id; |
| 3209 | } else { |
| 3210 | receiver_id = rtc::CreateRandomUuid(); |
| 3211 | } |
| 3212 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3213 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3214 | transceiver->internal()->set_direction( |
| 3215 | RtpTransceiverDirection::kRecvOnly); |
| 3216 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3217 | |
| 3218 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3219 | // This can happen when simulcast is not supported on the remote party. |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3220 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 3221 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3222 | RTCError error = |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3223 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3224 | if (!error.ok()) { |
| 3225 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3226 | return std::move(error); |
| 3227 | } |
| 3228 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3229 | } |
| 3230 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3231 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3232 | LOG_AND_RETURN_ERROR( |
| 3233 | RTCErrorType::INVALID_PARAMETER, |
| 3234 | "Transceiver type does not match media description type."); |
| 3235 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3236 | if (media_desc->HasSimulcast()) { |
| 3237 | std::vector<SimulcastLayer> layers = |
| 3238 | source == cricket::CS_LOCAL |
| 3239 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3240 | : media_desc->simulcast_description() |
| 3241 | .receive_layers() |
| 3242 | .GetAllLayers(); |
| 3243 | RTCError error = UpdateSimulcastLayerStatusInSender( |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3244 | layers, transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3245 | if (!error.ok()) { |
| 3246 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3247 | return std::move(error); |
| 3248 | } |
| 3249 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3250 | // Associate the found or created RtpTransceiver with the m= section by |
| 3251 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3252 | // section, and establish a mapping between the transceiver and the index of |
| 3253 | // the m= section. |
| 3254 | transceiver->internal()->set_mid(content.name); |
| 3255 | transceiver->internal()->set_mline_index(mline_index); |
| 3256 | return std::move(transceiver); |
| 3257 | } |
| 3258 | |
| 3259 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3260 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3261 | RTC_DCHECK(IsUnifiedPlan()); |
| 3262 | for (auto transceiver : transceivers_) { |
| 3263 | if (transceiver->mid() == mid) { |
| 3264 | return transceiver; |
| 3265 | } |
| 3266 | } |
| 3267 | return nullptr; |
| 3268 | } |
| 3269 | |
| 3270 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3271 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3272 | RTC_DCHECK(IsUnifiedPlan()); |
| 3273 | for (auto transceiver : transceivers_) { |
| 3274 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3275 | return transceiver; |
| 3276 | } |
| 3277 | } |
| 3278 | return nullptr; |
| 3279 | } |
| 3280 | |
| 3281 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3282 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3283 | cricket::MediaType media_type) const { |
| 3284 | RTC_DCHECK(IsUnifiedPlan()); |
| 3285 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3286 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3287 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3288 | // associated with any m= section and are not stopped, find the first such |
| 3289 | // RtpTransceiver. |
| 3290 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3291 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3292 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3293 | !transceiver->stopped()) { |
| 3294 | return transceiver; |
| 3295 | } |
| 3296 | } |
| 3297 | return nullptr; |
| 3298 | } |
| 3299 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3300 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3301 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3302 | transceiver, |
| 3303 | const SessionDescriptionInterface* sdesc) const { |
| 3304 | RTC_DCHECK(transceiver); |
| 3305 | RTC_DCHECK(sdesc); |
| 3306 | if (IsUnifiedPlan()) { |
| 3307 | if (!transceiver->internal()->mid()) { |
| 3308 | // This transceiver is not associated with a media section yet. |
| 3309 | return nullptr; |
| 3310 | } |
| 3311 | return sdesc->description()->GetContentByName( |
| 3312 | *transceiver->internal()->mid()); |
| 3313 | } else { |
| 3314 | // Plan B only allows at most one audio and one video section, so use the |
| 3315 | // first media section of that type. |
| 3316 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3317 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3321 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3322 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3323 | return configuration_; |
| 3324 | } |
| 3325 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3326 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 3327 | RTCError* error) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3328 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3329 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3330 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3331 | if (IsClosed()) { |
| 3332 | RTC_LOG(LS_ERROR) << "SetConfiguration: PeerConnection is closed."; |
| 3333 | return SafeSetError(RTCErrorType::INVALID_STATE, error); |
| 3334 | } |
| 3335 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3336 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3337 | // size is not allowed, and changing the set of ICE servers will not result |
| 3338 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3339 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3340 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3341 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 3342 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3343 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3344 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3345 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3346 | if (local_description() && |
| 3347 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3348 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3349 | "SetLocalDescription."; |
| 3350 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3351 | } |
| 3352 | |
| 3353 | if (remote_description() && |
| 3354 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3355 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3356 | "SetRemoteDescription."; |
| 3357 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3358 | } |
| 3359 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3360 | if (local_description() && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3361 | configuration.use_media_transport_for_data_channels != |
| 3362 | configuration_.use_media_transport_for_data_channels) { |
| 3363 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3364 | "after calling SetLocalDescription."; |
| 3365 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3366 | } |
| 3367 | |
| 3368 | if (remote_description() && |
| 3369 | configuration.use_media_transport_for_data_channels != |
| 3370 | configuration_.use_media_transport_for_data_channels) { |
| 3371 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3372 | "after calling SetRemoteDescription."; |
| 3373 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3374 | } |
| 3375 | |
| 3376 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3377 | configuration.crypto_options != configuration_.crypto_options) { |
| 3378 | RTC_LOG(LS_ERROR) << "Can't change crypto_options after calling " |
| 3379 | "SetLocalDescription."; |
| 3380 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3381 | } |
| 3382 | |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3383 | if (configuration.use_media_transport_for_data_channels || |
| 3384 | configuration.use_media_transport) { |
| 3385 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 3386 | << "Media transport requires MaxBundle policy."; |
| 3387 | } |
| 3388 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3389 | // The simplest (and most future-compatible) way to tell if the config was |
| 3390 | // modified in an invalid way is to copy each property we do support |
| 3391 | // modifying, then use operator==. There are far more properties we don't |
| 3392 | // support modifying than those we do, and more could be added. |
| 3393 | RTCConfiguration modified_config = configuration_; |
| 3394 | modified_config.servers = configuration.servers; |
| 3395 | modified_config.type = configuration.type; |
| 3396 | modified_config.ice_candidate_pool_size = |
| 3397 | configuration.ice_candidate_pool_size; |
| 3398 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3399 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 3400 | modified_config.ice_check_interval_strong_connectivity = |
| 3401 | configuration.ice_check_interval_strong_connectivity; |
| 3402 | modified_config.ice_check_interval_weak_connectivity = |
| 3403 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 3404 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 3405 | modified_config.ice_unwritable_min_checks = |
| 3406 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 3407 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3408 | modified_config.stun_candidate_keepalive_interval = |
| 3409 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3410 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 3411 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3412 | modified_config.active_reset_srtp_params = |
| 3413 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3414 | modified_config.use_media_transport = configuration.use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3415 | modified_config.use_media_transport_for_data_channels = |
| 3416 | configuration.use_media_transport_for_data_channels; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3417 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3418 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3419 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3420 | } |
| 3421 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 3422 | // Validate the modified configuration. |
| 3423 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 3424 | if (!validate_error.ok()) { |
| 3425 | return SafeSetError(std::move(validate_error), error); |
| 3426 | } |
| 3427 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3428 | // Note that this isn't possible through chromium, since it's an unsigned |
| 3429 | // short in WebIDL. |
| 3430 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 3431 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3432 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 3433 | } |
| 3434 | |
| 3435 | // Parse ICE servers before hopping to network thread. |
| 3436 | cricket::ServerAddresses stun_servers; |
| 3437 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 3438 | RTCErrorType parse_error = |
| 3439 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 3440 | if (parse_error != RTCErrorType::NONE) { |
| 3441 | return SafeSetError(parse_error, error); |
| 3442 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3443 | // Note if STUN or TURN servers were supplied. |
| 3444 | if (!stun_servers.empty()) { |
| 3445 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 3446 | } |
| 3447 | if (!turn_servers.empty()) { |
| 3448 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 3449 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3450 | |
| 3451 | // In theory this shouldn't fail. |
| 3452 | if (!network_thread()->Invoke<bool>( |
| 3453 | RTC_FROM_HERE, |
| 3454 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 3455 | stun_servers, turn_servers, modified_config.type, |
| 3456 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3457 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3458 | modified_config.turn_customizer, |
| 3459 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3460 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3461 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 3462 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3463 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3464 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 3465 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 3466 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3467 | if (modified_config.servers != configuration_.servers || |
| 3468 | modified_config.type != configuration_.type || |
| 3469 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3470 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3471 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3472 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 3473 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 3474 | transport_controller_->SetMediaTransportSettings( |
| 3475 | modified_config.use_media_transport, |
| 3476 | modified_config.use_media_transport_for_data_channels); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3477 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3478 | if (configuration_.active_reset_srtp_params != |
| 3479 | modified_config.active_reset_srtp_params) { |
| 3480 | transport_controller_->SetActiveResetSrtpParams( |
| 3481 | modified_config.active_reset_srtp_params); |
| 3482 | } |
| 3483 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3484 | configuration_ = modified_config; |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3485 | use_media_transport_ = configuration.use_media_transport; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3486 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3489 | bool PeerConnection::AddIceCandidate( |
| 3490 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 3491 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3492 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3493 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3494 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3495 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3496 | return false; |
| 3497 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3498 | |
| 3499 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3500 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3501 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3502 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3503 | return false; |
| 3504 | } |
| 3505 | |
| 3506 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3507 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3508 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3509 | return false; |
| 3510 | } |
| 3511 | |
| 3512 | bool valid = false; |
| 3513 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 3514 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3515 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3516 | return false; |
| 3517 | } |
| 3518 | |
| 3519 | // Add this candidate to the remote session description. |
| 3520 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3521 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3522 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3523 | return false; |
| 3524 | } |
| 3525 | |
| 3526 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3527 | bool result = UseCandidate(ice_candidate); |
| 3528 | if (result) { |
| 3529 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 3530 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 3531 | } else { |
| 3532 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 3533 | } |
| 3534 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3535 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3536 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3537 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3538 | return true; |
| 3539 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3542 | bool PeerConnection::RemoveIceCandidates( |
| 3543 | const std::vector<cricket::Candidate>& candidates) { |
| 3544 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3545 | if (IsClosed()) { |
| 3546 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 3547 | return false; |
| 3548 | } |
| 3549 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3550 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3551 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 3552 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3553 | return false; |
| 3554 | } |
| 3555 | |
| 3556 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3557 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3558 | return false; |
| 3559 | } |
| 3560 | |
| 3561 | size_t number_removed = |
| 3562 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3563 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3564 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3565 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3566 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3567 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3571 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3572 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3573 | RTC_LOG(LS_ERROR) |
| 3574 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3575 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3576 | } |
| 3577 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3580 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3581 | if (!worker_thread()->IsCurrent()) { |
| 3582 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3583 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3584 | } |
| 3585 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3586 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3587 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3588 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3589 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3590 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3591 | "min_bitrate_bps <= 0"); |
| 3592 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3593 | if (has_start) { |
| 3594 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3595 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3596 | "start_bitrate_bps < min_bitrate_bps"); |
| 3597 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3598 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3599 | "curent_bitrate_bps < 0"); |
| 3600 | } |
| 3601 | } |
| 3602 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3603 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3604 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3605 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3606 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3607 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3608 | "max_bitrate_bps < min_bitrate_bps"); |
| 3609 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3610 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3611 | "max_bitrate_bps < 0"); |
| 3612 | } |
| 3613 | } |
| 3614 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3615 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 10:31:54 -0700 | [diff] [blame] | 3616 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3617 | |
| 3618 | return RTCError::OK(); |
| 3619 | } |
| 3620 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3621 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3622 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3623 | bitrate_allocation_strategy) { |
| 3624 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3625 | if (!worker_thread->IsCurrent()) { |
| 3626 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3627 | bitrate_allocation_strategy.release(); |
| 3628 | auto functor = [this, strategy_raw]() { |
| 3629 | call_->SetBitrateAllocationStrategy( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3630 | absl::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3631 | }; |
| 3632 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3633 | return; |
| 3634 | } |
| 3635 | RTC_DCHECK(call_.get()); |
| 3636 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3637 | } |
| 3638 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3639 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3640 | if (!worker_thread()->IsCurrent()) { |
| 3641 | worker_thread()->Invoke<void>( |
| 3642 | RTC_FROM_HERE, |
| 3643 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3644 | return; |
| 3645 | } |
| 3646 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3647 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3648 | audio_state->SetPlayout(playout); |
| 3649 | } |
| 3650 | |
| 3651 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3652 | if (!worker_thread()->IsCurrent()) { |
| 3653 | worker_thread()->Invoke<void>( |
| 3654 | RTC_FROM_HERE, |
| 3655 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3656 | return; |
| 3657 | } |
| 3658 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3659 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3660 | audio_state->SetRecording(recording); |
| 3661 | } |
| 3662 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3663 | std::unique_ptr<rtc::SSLCertificate> |
| 3664 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3665 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3666 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3667 | return nullptr; |
| 3668 | } |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 3669 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3670 | } |
| 3671 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3672 | std::unique_ptr<rtc::SSLCertChain> |
| 3673 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3674 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3675 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3676 | return nullptr; |
| 3677 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3678 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3679 | audio_transceiver->internal()->channel()->transport_name()); |
| 3680 | } |
| 3681 | |
| 3682 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3683 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3684 | for (auto transceiver : transceivers_) { |
| 3685 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3686 | return transceiver; |
| 3687 | } |
| 3688 | } |
| 3689 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3690 | } |
| 3691 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3692 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3693 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3694 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3695 | const size_t max_size = (max_size_bytes < 0) |
| 3696 | ? RtcEventLog::kUnlimitedOutput |
| 3697 | : rtc::saturated_cast<size_t>(max_size_bytes); |
Bjorn Terelius | 8b55602 | 2018-11-27 15:43:01 +0100 | [diff] [blame] | 3698 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 3699 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 3700 | output_period_ms = 5000; |
| 3701 | } |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3702 | return StartRtcEventLog( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3703 | absl::make_unique<RtcEventLogOutputFile>(file, max_size), |
Bjorn Terelius | 8b55602 | 2018-11-27 15:43:01 +0100 | [diff] [blame] | 3704 | output_period_ms); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3707 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3708 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3709 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3710 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3711 | bool operator()() { |
| 3712 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3713 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3714 | PeerConnection* const pc; |
| 3715 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3716 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3717 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3718 | return worker_thread()->Invoke<bool>( |
| 3719 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3723 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3724 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3725 | } |
| 3726 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 3727 | rtc::scoped_refptr<DtlsTransportInterface> |
| 3728 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
| 3729 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3730 | } |
| 3731 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3732 | rtc::scoped_refptr<DtlsTransport> |
| 3733 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
| 3734 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3735 | } |
| 3736 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 3737 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 3738 | const { |
| 3739 | return sctp_transport_; |
| 3740 | } |
| 3741 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3742 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3743 | return pending_local_description_ ? pending_local_description_.get() |
| 3744 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
| 3747 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3748 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3749 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3752 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3753 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3754 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3758 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3759 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3763 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3764 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3768 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3769 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3770 | } |
| 3771 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3772 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 3773 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3774 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3775 | // Update stats here so that we have the most recent stats for tracks and |
| 3776 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3777 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3778 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3779 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3780 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3781 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3782 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3783 | transceiver->Stop(); |
| 3784 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 3785 | |
| 3786 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3787 | // the transport controller below. |
| 3788 | if (stats_collector_) { |
| 3789 | stats_collector_->WaitForPendingRequest(); |
| 3790 | } |
| 3791 | |
| 3792 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3793 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3794 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3795 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3796 | // The event log is used in the transport controller, which must be outlived |
| 3797 | // by the former. CreateOffer by the peer connection is implemented |
| 3798 | // asynchronously and if the peer connection is closed without resetting the |
| 3799 | // WebRTC session description factory, the session description factory would |
| 3800 | // call the transport controller. |
| 3801 | webrtc_session_desc_factory_.reset(); |
| 3802 | transport_controller_.reset(); |
| 3803 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3804 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3805 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3806 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3807 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3808 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 3809 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3810 | call_.reset(); |
| 3811 | // The event log must outlive call (and any other object that uses it). |
| 3812 | event_log_.reset(); |
| 3813 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3814 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3815 | // The .h file says that observer can be discarded after close() returns. |
| 3816 | // Make sure this is true. |
| 3817 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3820 | void PeerConnection::OnMessage(rtc::Message* msg) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 3821 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3822 | switch (msg->message_id) { |
| 3823 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3824 | SetSessionDescriptionMsg* param = |
| 3825 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3826 | param->observer->OnSuccess(); |
| 3827 | delete param; |
| 3828 | break; |
| 3829 | } |
| 3830 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3831 | SetSessionDescriptionMsg* param = |
| 3832 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3833 | param->observer->OnFailure(std::move(param->error)); |
| 3834 | delete param; |
| 3835 | break; |
| 3836 | } |
| 3837 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3838 | CreateSessionDescriptionMsg* param = |
| 3839 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3840 | param->observer->OnFailure(std::move(param->error)); |
| 3841 | delete param; |
| 3842 | break; |
| 3843 | } |
| 3844 | case MSG_GETSTATS: { |
| 3845 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 3846 | StatsReports reports; |
| 3847 | stats_->GetStats(param->track, &reports); |
| 3848 | param->observer->OnComplete(reports); |
| 3849 | delete param; |
| 3850 | break; |
| 3851 | } |
| 3852 | case MSG_FREE_DATACHANNELS: { |
| 3853 | sctp_data_channels_to_free_.clear(); |
| 3854 | break; |
| 3855 | } |
| 3856 | case MSG_REPORT_USAGE_PATTERN: { |
| 3857 | ReportUsagePattern(); |
| 3858 | break; |
| 3859 | } |
| 3860 | default: |
| 3861 | RTC_NOTREACHED() << "Not implemented"; |
| 3862 | break; |
| 3863 | } |
| 3864 | } |
| 3865 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3866 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3867 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3868 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3869 | GetAudioTransceiver()->internal()->channel()); |
| 3870 | if (voice_channel) { |
| 3871 | return voice_channel->media_channel(); |
| 3872 | } else { |
| 3873 | return nullptr; |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3878 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3879 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3880 | GetVideoTransceiver()->internal()->channel()); |
| 3881 | if (video_channel) { |
| 3882 | return video_channel->media_channel(); |
| 3883 | } else { |
| 3884 | return nullptr; |
| 3885 | } |
| 3886 | } |
| 3887 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3888 | void PeerConnection::CreateAudioReceiver( |
| 3889 | MediaStreamInterface* stream, |
| 3890 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3891 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3892 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3893 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3894 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3895 | auto* audio_receiver = new AudioRtpReceiver( |
| 3896 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3897 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3898 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3899 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3900 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3901 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 3902 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3903 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3906 | void PeerConnection::CreateVideoReceiver( |
| 3907 | MediaStreamInterface* stream, |
| 3908 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3909 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3910 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3911 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3912 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3913 | auto* video_receiver = new VideoRtpReceiver( |
| 3914 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3915 | video_receiver->SetMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3916 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3917 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3918 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3919 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 3920 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3921 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3924 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3925 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3926 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3927 | const RtpSenderInfo& remote_sender_info) { |
| 3928 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3929 | if (!receiver) { |
| 3930 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3931 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3932 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3933 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3934 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3935 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3936 | } else { |
| 3937 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3938 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3939 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3942 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3943 | MediaStreamInterface* stream) { |
| 3944 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3945 | RTC_DCHECK(track); |
| 3946 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3947 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3948 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3949 | // We already have a sender for this track, so just change the stream_id |
| 3950 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3951 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3952 | return; |
| 3953 | } |
| 3954 | |
| 3955 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3956 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3957 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3958 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3959 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3960 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3961 | // which will connect the sender to the underlying transport. This can |
| 3962 | // occur if a local session description that contains the ID of the sender |
| 3963 | // is set before AddStream is called. It can also occur if the local |
| 3964 | // session description is not changed and RemoveStream is called, and |
| 3965 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3966 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3967 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3968 | if (sender_info) { |
| 3969 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3974 | // indefinitely, when we have unified plan SDP. |
| 3975 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3976 | MediaStreamInterface* stream) { |
| 3977 | RTC_DCHECK(!IsClosed()); |
| 3978 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3979 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3980 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3981 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3982 | return; |
| 3983 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3984 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3988 | MediaStreamInterface* stream) { |
| 3989 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3990 | RTC_DCHECK(track); |
| 3991 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3992 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3993 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3994 | // We already have a sender for this track, so just change the stream_id |
| 3995 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3996 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3997 | return; |
| 3998 | } |
| 3999 | |
| 4000 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4001 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4002 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4003 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4004 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 4005 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4006 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4007 | if (sender_info) { |
| 4008 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4009 | } |
| 4010 | } |
| 4011 | |
| 4012 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 4013 | MediaStreamInterface* stream) { |
| 4014 | RTC_DCHECK(!IsClosed()); |
| 4015 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4016 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4017 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4018 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4019 | return; |
| 4020 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4021 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4022 | } |
| 4023 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4024 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4025 | if (ice_connection_state_ == new_state) { |
| 4026 | return; |
| 4027 | } |
| 4028 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4029 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4030 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4031 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4032 | return; |
| 4033 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4034 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4035 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 4036 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4037 | RTC_DCHECK(ice_connection_state_ != |
| 4038 | PeerConnectionInterface::kIceConnectionClosed); |
| 4039 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4040 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4041 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4044 | void PeerConnection::SetStandardizedIceConnectionState( |
| 4045 | PeerConnectionInterface::IceConnectionState new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4046 | if (standardized_ice_connection_state_ == new_state) |
| 4047 | return; |
| 4048 | if (IsClosed()) |
| 4049 | return; |
| 4050 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 11:25:14 +0100 | [diff] [blame] | 4051 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4054 | void PeerConnection::SetConnectionState( |
| 4055 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4056 | if (connection_state_ == new_state) |
| 4057 | return; |
| 4058 | if (IsClosed()) |
| 4059 | return; |
| 4060 | connection_state_ = new_state; |
| 4061 | Observer()->OnConnectionChange(new_state); |
| 4062 | } |
| 4063 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4064 | void PeerConnection::OnIceGatheringChange( |
| 4065 | PeerConnectionInterface::IceGatheringState new_state) { |
| 4066 | if (IsClosed()) { |
| 4067 | return; |
| 4068 | } |
| 4069 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4070 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 4073 | void PeerConnection::OnIceCandidate( |
| 4074 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4075 | if (IsClosed()) { |
| 4076 | return; |
| 4077 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4078 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 4079 | if (candidate->candidate().type() == LOCAL_PORT_TYPE && |
| 4080 | candidate->candidate().address().IsPrivateIP()) { |
| 4081 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 4082 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4083 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4086 | void PeerConnection::OnIceCandidatesRemoved( |
| 4087 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4088 | if (IsClosed()) { |
| 4089 | return; |
| 4090 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4091 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4092 | } |
| 4093 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4094 | void PeerConnection::ChangeSignalingState( |
| 4095 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4096 | if (signaling_state_ == signaling_state) { |
| 4097 | return; |
| 4098 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4099 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 4100 | << GetSignalingStateString(signaling_state_) |
| 4101 | << " New state: " |
| 4102 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4103 | signaling_state_ = signaling_state; |
| 4104 | if (signaling_state == kClosed) { |
| 4105 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4106 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4107 | standardized_ice_connection_state_ = |
| 4108 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4109 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 4110 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4111 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 4112 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4113 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4114 | } |
| 4115 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4116 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4119 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4120 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4121 | if (IsClosed()) { |
| 4122 | return; |
| 4123 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4124 | AddAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4125 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4126 | } |
| 4127 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4128 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4129 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4130 | if (IsClosed()) { |
| 4131 | return; |
| 4132 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4133 | RemoveAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4134 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4138 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4139 | if (IsClosed()) { |
| 4140 | return; |
| 4141 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4142 | AddVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4143 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4147 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4148 | if (IsClosed()) { |
| 4149 | return; |
| 4150 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4151 | RemoveVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4152 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4153 | } |
| 4154 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4155 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4156 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4157 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4158 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4159 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4160 | } |
| 4161 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4162 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4163 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4164 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4165 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4166 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4167 | msg->error = std::move(error); |
| 4168 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4169 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4173 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4174 | RTCError error) { |
| 4175 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4176 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4177 | msg->error = std::move(error); |
| 4178 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4179 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4180 | } |
| 4181 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4182 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4183 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4184 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4185 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4186 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4187 | if (IsUnifiedPlan()) { |
| 4188 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4189 | } else { |
| 4190 | GetOptionsForPlanBOffer(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 with the |
| 4194 | // second condition. Otherwise the RTP data channels would be successfully |
| 4195 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4196 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4197 | // people won't try to use them. |
| 4198 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4199 | session_options->data_channel_type = data_channel_type(); |
| 4200 | } |
| 4201 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4202 | // Apply ICE restart flag and renomination flag. |
| 4203 | for (auto& options : session_options->media_description_options) { |
| 4204 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 4205 | options.transport_options.enable_ice_renomination = |
| 4206 | configuration_.enable_ice_renomination; |
| 4207 | } |
| 4208 | |
| 4209 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4210 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4211 | session_options->pooled_ice_credentials = |
| 4212 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4213 | RTC_FROM_HERE, |
| 4214 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4215 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 4216 | session_options->offer_extmap_allow_mixed = |
| 4217 | configuration_.offer_extmap_allow_mixed; |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 4218 | |
| 4219 | if (configuration_.enable_dtls_srtp && |
| 4220 | !configuration_.enable_dtls_srtp.value()) { |
| 4221 | session_options->media_transport_settings = |
| 4222 | transport_controller_->GenerateOrGetLastMediaTransportOffer(); |
| 4223 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4227 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4228 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4229 | // Figure out transceiver directional preferences. |
| 4230 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4231 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4232 | |
| 4233 | // By default, generate sendrecv/recvonly m= sections. |
| 4234 | bool recv_audio = true; |
| 4235 | bool recv_video = true; |
| 4236 | |
| 4237 | // By default, only offer a new m= section if we have media to send with it. |
| 4238 | bool offer_new_audio_description = send_audio; |
| 4239 | bool offer_new_video_description = send_video; |
| 4240 | bool offer_new_data_description = HasDataChannels(); |
| 4241 | |
| 4242 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4243 | if (offer_answer_options.offer_to_receive_audio != |
| 4244 | RTCOfferAnswerOptions::kUndefined) { |
| 4245 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4246 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4247 | offer_new_audio_description || |
| 4248 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4249 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4250 | if (offer_answer_options.offer_to_receive_video != |
| 4251 | RTCOfferAnswerOptions::kUndefined) { |
| 4252 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4253 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4254 | offer_new_video_description || |
| 4255 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4258 | absl::optional<size_t> audio_index; |
| 4259 | absl::optional<size_t> video_index; |
| 4260 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4261 | // If a current description exists, generate m= sections in the same order, |
| 4262 | // using the first audio/video/data section that appears and rejecting |
| 4263 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4264 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4265 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4266 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4267 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4268 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4269 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4270 | } |
| 4271 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4272 | // Add audio/video/data m= sections to the end if needed. |
| 4273 | if (!audio_index && offer_new_audio_description) { |
| 4274 | session_options->media_description_options.push_back( |
| 4275 | cricket::MediaDescriptionOptions( |
| 4276 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4277 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4278 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4279 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4280 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4281 | if (!video_index && offer_new_video_description) { |
| 4282 | session_options->media_description_options.push_back( |
| 4283 | cricket::MediaDescriptionOptions( |
| 4284 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4285 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4286 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4287 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4288 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4289 | if (!data_index && offer_new_data_description) { |
| 4290 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4291 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4292 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4296 | !audio_index ? nullptr |
| 4297 | : &session_options->media_description_options[*audio_index]; |
| 4298 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4299 | !video_index ? nullptr |
| 4300 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4301 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4302 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4303 | audio_media_description_options, |
| 4304 | video_media_description_options, |
| 4305 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4306 | } |
| 4307 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4308 | static cricket::MediaDescriptionOptions |
| 4309 | GetMediaDescriptionOptionsForTransceiver( |
| 4310 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4311 | transceiver, |
| 4312 | const std::string& mid) { |
| 4313 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4314 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4315 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4316 | // This behavior is specified in JSEP. The gist is that: |
| 4317 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 4318 | // sendrecv. |
| 4319 | // 2. If the MSID is included, then it must be included in any subsequent |
| 4320 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4321 | if (transceiver->stopped() || |
| 4322 | (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 4323 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 4324 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4325 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4326 | |
| 4327 | cricket::SenderOptions sender_options; |
| 4328 | sender_options.track_id = transceiver->sender()->id(); |
| 4329 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 4330 | |
| 4331 | // The following sets up RIDs and Simulcast. |
| 4332 | // RIDs are included if Simulcast is requested or if any RID was specified. |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 4333 | RtpParameters send_parameters = |
| 4334 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4335 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 4336 | send_parameters.encodings.end(), |
| 4337 | [](const RtpEncodingParameters& encoding) { |
| 4338 | return !encoding.rid.empty(); |
| 4339 | }); |
| 4340 | |
Amit Hilbuch | b7446ed | 2019-01-28 12:25:25 -0800 | [diff] [blame] | 4341 | std::vector<RidDescription> send_rids; |
| 4342 | SimulcastLayerList send_layers; |
| 4343 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 4344 | if (encoding.rid.empty()) { |
| 4345 | continue; |
| 4346 | } |
| 4347 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 4348 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 4349 | } |
| 4350 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4351 | if (has_rids) { |
| 4352 | sender_options.rids = send_rids; |
| 4353 | } |
| 4354 | |
| 4355 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4356 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 4357 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 4358 | // simulcast, or simulcast is acheived by munging the SDP. |
| 4359 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 4360 | media_description_options.sender_options.push_back(sender_options); |
| 4361 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4362 | return media_description_options; |
| 4363 | } |
| 4364 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4365 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 4366 | static const ContentInfo* GetContentByIndex( |
| 4367 | const SessionDescriptionInterface* sdesc, |
| 4368 | size_t i) { |
| 4369 | if (!sdesc) { |
| 4370 | return nullptr; |
| 4371 | } |
| 4372 | const ContentInfos& contents = sdesc->description()->contents(); |
| 4373 | return (i < contents.size() ? &contents[i] : nullptr); |
| 4374 | } |
| 4375 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4376 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 4377 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4378 | cricket::MediaSessionOptions* session_options) { |
| 4379 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 4380 | // Offers) and 5.2.2 (Subsequent Offers). |
| 4381 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 4382 | const ContentInfos& local_contents = |
| 4383 | (local_description() ? local_description()->description()->contents() |
| 4384 | : ContentInfos()); |
| 4385 | const ContentInfos& remote_contents = |
| 4386 | (remote_description() ? remote_description()->description()->contents() |
| 4387 | : ContentInfos()); |
| 4388 | // The mline indices that can be recycled. New transceivers should reuse these |
| 4389 | // slots first. |
| 4390 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4391 | // First, go through each media section that exists in either the local or |
| 4392 | // remote description and generate a media section in this offer for the |
| 4393 | // associated transceiver. If a media section can be recycled, generate a |
| 4394 | // default, rejected media section here that can be later overwritten. |
| 4395 | for (size_t i = 0; |
| 4396 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 4397 | // Either |local_content| or |remote_content| is non-null. |
| 4398 | const ContentInfo* local_content = |
| 4399 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4400 | const ContentInfo* current_local_content = |
| 4401 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4402 | const ContentInfo* remote_content = |
| 4403 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4404 | const ContentInfo* current_remote_content = |
| 4405 | GetContentByIndex(current_remote_description(), i); |
| 4406 | bool had_been_rejected = |
| 4407 | (current_local_content && current_local_content->rejected) || |
| 4408 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4409 | const std::string& mid = |
| 4410 | (local_content ? local_content->name : remote_content->name); |
| 4411 | cricket::MediaType media_type = |
| 4412 | (local_content ? local_content->media_description()->type() |
| 4413 | : remote_content->media_description()->type()); |
| 4414 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4415 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4416 | auto transceiver = GetAssociatedTransceiver(mid); |
| 4417 | RTC_CHECK(transceiver); |
| 4418 | // 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] | 4419 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 4420 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4421 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4422 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 4423 | RtpTransceiverDirection::kInactive, |
| 4424 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4425 | recycleable_mline_indices.push(i); |
| 4426 | } else { |
| 4427 | session_options->media_description_options.push_back( |
| 4428 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 4429 | // CreateOffer shouldn't really cause any state changes in |
| 4430 | // PeerConnection, but we need a way to match new transceivers to new |
| 4431 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 4432 | // by recording the index of the media section generated for the |
| 4433 | // transceiver in the offer. |
| 4434 | transceiver->internal()->set_mline_index(i); |
| 4435 | } |
| 4436 | } else { |
| 4437 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4438 | RTC_CHECK(GetDataMid()); |
| 4439 | if (had_been_rejected || mid != *GetDataMid()) { |
| 4440 | session_options->media_description_options.push_back( |
| 4441 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 4442 | } else { |
| 4443 | session_options->media_description_options.push_back( |
| 4444 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 4445 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4446 | } |
| 4447 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4448 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4449 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 4450 | // and not associated). Reuse media sections marked as recyclable first, |
| 4451 | // otherwise append to the end of the offer. New media sections should be |
| 4452 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 4453 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4454 | if (transceiver->mid() || transceiver->stopped()) { |
| 4455 | continue; |
| 4456 | } |
| 4457 | size_t mline_index; |
| 4458 | if (!recycleable_mline_indices.empty()) { |
| 4459 | mline_index = recycleable_mline_indices.front(); |
| 4460 | recycleable_mline_indices.pop(); |
| 4461 | session_options->media_description_options[mline_index] = |
| 4462 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4463 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4464 | } else { |
| 4465 | mline_index = session_options->media_description_options.size(); |
| 4466 | session_options->media_description_options.push_back( |
| 4467 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4468 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4469 | } |
| 4470 | // See comment above for why CreateOffer changes the transceiver's state. |
| 4471 | transceiver->internal()->set_mline_index(mline_index); |
| 4472 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4473 | // Lastly, add a m-section if we have local data channels and an m section |
| 4474 | // does not already exist. |
| 4475 | if (!GetDataMid() && HasDataChannels()) { |
| 4476 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4477 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4478 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | void PeerConnection::GetOptionsForAnswer( |
| 4482 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4483 | cricket::MediaSessionOptions* session_options) { |
| 4484 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 4485 | |
| 4486 | if (IsUnifiedPlan()) { |
| 4487 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 4488 | } else { |
| 4489 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 4490 | } |
| 4491 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4492 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 4493 | // the RTP data channels would be successfully negotiated by default and the |
| 4494 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 4495 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 4496 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4497 | session_options->data_channel_type = data_channel_type(); |
| 4498 | } |
| 4499 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4500 | // Apply ICE renomination flag. |
| 4501 | for (auto& options : session_options->media_description_options) { |
| 4502 | options.transport_options.enable_ice_renomination = |
| 4503 | configuration_.enable_ice_renomination; |
| 4504 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 4505 | |
| 4506 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4507 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4508 | session_options->pooled_ice_credentials = |
| 4509 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4510 | RTC_FROM_HERE, |
| 4511 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4512 | port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4513 | } |
| 4514 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4515 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 4516 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4517 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4518 | // Figure out transceiver directional preferences. |
| 4519 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4520 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4521 | |
| 4522 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 4523 | // restricted by the direction in the offer. |
| 4524 | bool recv_audio = true; |
| 4525 | bool recv_video = true; |
| 4526 | |
| 4527 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4528 | if (offer_answer_options.offer_to_receive_audio != |
| 4529 | RTCOfferAnswerOptions::kUndefined) { |
| 4530 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 4531 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4532 | if (offer_answer_options.offer_to_receive_video != |
| 4533 | RTCOfferAnswerOptions::kUndefined) { |
| 4534 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4535 | } |
| 4536 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4537 | absl::optional<size_t> audio_index; |
| 4538 | absl::optional<size_t> video_index; |
| 4539 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 4540 | |
| 4541 | // Generate m= sections that match those in the offer. |
| 4542 | // Note that mediasession.cc will handle intersection our preferred |
| 4543 | // direction with the offered direction. |
| 4544 | GenerateMediaDescriptionOptions( |
| 4545 | remote_description(), |
| 4546 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4547 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 4548 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4549 | |
| 4550 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4551 | !audio_index ? nullptr |
| 4552 | : &session_options->media_description_options[*audio_index]; |
| 4553 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4554 | !video_index ? nullptr |
| 4555 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4556 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4557 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4558 | audio_media_description_options, |
| 4559 | video_media_description_options, |
| 4560 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4561 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4562 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4563 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 4564 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4565 | cricket::MediaSessionOptions* session_options) { |
| 4566 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 4567 | // Answers) and 5.3.2 (Subsequent Answers). |
| 4568 | RTC_DCHECK(remote_description()); |
| 4569 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 4570 | for (const ContentInfo& content : |
| 4571 | remote_description()->description()->contents()) { |
| 4572 | cricket::MediaType media_type = content.media_description()->type(); |
| 4573 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4574 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4575 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 4576 | RTC_CHECK(transceiver); |
| 4577 | session_options->media_description_options.push_back( |
| 4578 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 4579 | } else { |
| 4580 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 4581 | // Reject all data sections if data channels are disabled. |
| 4582 | // Reject a data section if it has already been rejected. |
| 4583 | // Reject all data sections except for the first one. |
| 4584 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 4585 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4586 | session_options->media_description_options.push_back( |
| 4587 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4588 | } else { |
| 4589 | session_options->media_description_options.push_back( |
| 4590 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4591 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4592 | } |
| 4593 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4594 | } |
| 4595 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4596 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 4597 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4598 | RtpTransceiverDirection audio_direction, |
| 4599 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4600 | absl::optional<size_t>* audio_index, |
| 4601 | absl::optional<size_t>* video_index, |
| 4602 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4603 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4604 | for (const cricket::ContentInfo& content : |
| 4605 | session_desc->description()->contents()) { |
| 4606 | if (IsAudioContent(&content)) { |
| 4607 | // If we already have an audio m= section, reject this extra one. |
| 4608 | if (*audio_index) { |
| 4609 | session_options->media_description_options.push_back( |
| 4610 | cricket::MediaDescriptionOptions( |
| 4611 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4612 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4613 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4614 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4615 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4616 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 4617 | content.name, audio_direction, |
| 4618 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4619 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4620 | } |
| 4621 | } else if (IsVideoContent(&content)) { |
| 4622 | // If we already have an video m= section, reject this extra one. |
| 4623 | if (*video_index) { |
| 4624 | session_options->media_description_options.push_back( |
| 4625 | cricket::MediaDescriptionOptions( |
| 4626 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4627 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4628 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4629 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4630 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4631 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 4632 | content.name, video_direction, |
| 4633 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4634 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4635 | } |
| 4636 | } else { |
| 4637 | RTC_DCHECK(IsDataContent(&content)); |
| 4638 | // If we already have an data m= section, reject this extra one. |
| 4639 | if (*data_index) { |
| 4640 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4641 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4642 | } else { |
| 4643 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4644 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4645 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4646 | } |
| 4647 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4648 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4649 | } |
| 4650 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4651 | cricket::MediaDescriptionOptions |
| 4652 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4653 | const std::string& mid) const { |
| 4654 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4655 | // expect sendrecv. |
| 4656 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4657 | RtpTransceiverDirection::kSendRecv, |
| 4658 | /*stopped=*/false); |
| 4659 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4660 | return options; |
| 4661 | } |
| 4662 | |
| 4663 | cricket::MediaDescriptionOptions |
| 4664 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4665 | const std::string& mid) const { |
| 4666 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4667 | RtpTransceiverDirection::kInactive, |
| 4668 | /*stopped=*/true); |
| 4669 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4670 | return options; |
| 4671 | } |
| 4672 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4673 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4674 | switch (data_channel_type_) { |
| 4675 | case cricket::DCT_RTP: |
| 4676 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4677 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4678 | } |
| 4679 | return rtp_data_channel_->content_name(); |
| 4680 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4681 | return sctp_mid_; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4682 | case cricket::DCT_MEDIA_TRANSPORT: |
| 4683 | return media_transport_data_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4684 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4685 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4689 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4690 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4691 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4692 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4693 | } |
| 4694 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4695 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4696 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4697 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4698 | cricket::MediaType media_type, |
| 4699 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4700 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4701 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4702 | std::vector<RtpSenderInfo>* current_senders = |
| 4703 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4704 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4705 | // 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] | 4706 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4707 | for (auto sender_it = current_senders->begin(); |
| 4708 | sender_it != current_senders->end(); |
| 4709 | /* incremented manually */) { |
| 4710 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4711 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4712 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4713 | std::string params_stream_id; |
| 4714 | if (params) { |
| 4715 | params_stream_id = |
| 4716 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4717 | : kDefaultStreamId); |
| 4718 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4719 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4720 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4721 | // 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] | 4722 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4723 | sender_exists) { |
| 4724 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4725 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4726 | OnRemoteSenderRemoved(info, media_type); |
| 4727 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4728 | } |
| 4729 | } |
| 4730 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4731 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4732 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4733 | if (!params.has_ssrcs()) { |
| 4734 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4735 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4736 | // create a default. |
| 4737 | default_sender_needed = true; |
| 4738 | break; |
| 4739 | } |
| 4740 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4741 | // |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] | 4742 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4743 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4744 | // not supported in Plan B, we just take the first here and create the |
| 4745 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4746 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4747 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4748 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4749 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4750 | uint32_t ssrc = params.first_ssrc(); |
| 4751 | |
| 4752 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4753 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4754 | if (!stream) { |
| 4755 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4756 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4757 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4758 | remote_streams_->AddStream(stream); |
| 4759 | new_streams->AddStream(stream); |
| 4760 | } |
| 4761 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4762 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4763 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4764 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4765 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4766 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4767 | } |
| 4768 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4769 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4770 | // Add default sender if necessary. |
| 4771 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4772 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4773 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4774 | if (!default_stream) { |
| 4775 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4776 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4777 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4778 | remote_streams_->AddStream(default_stream); |
| 4779 | new_streams->AddStream(default_stream); |
| 4780 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4781 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4782 | ? kDefaultAudioSenderId |
| 4783 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4784 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4785 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4786 | if (!default_sender_info) { |
| 4787 | current_senders->push_back( |
Ruslan Burakov | 7ea4605 | 2019-02-16 02:07:05 +0100 | [diff] [blame] | 4788 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4789 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4790 | } |
| 4791 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4792 | } |
| 4793 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4794 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4795 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4796 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4797 | << " receiver for track_id=" << sender_info.sender_id |
| 4798 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4799 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4800 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4801 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4802 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4803 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4804 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4805 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4806 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4807 | } |
| 4808 | } |
| 4809 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4810 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4811 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4812 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 4813 | << " receiver for track_id=" << sender_info.sender_id |
| 4814 | << " and stream_id=" << sender_info.stream_id; |
| 4815 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4816 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4817 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4818 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4819 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4820 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4821 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4822 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4823 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4824 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4825 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4826 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4827 | } |
| 4828 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4829 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4830 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4831 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4832 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4833 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4834 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4835 | // There's no guarantee the track is still available, e.g. the track may |
| 4836 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4837 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4838 | } |
| 4839 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4840 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4841 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4842 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4843 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4844 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4848 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4849 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4850 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4851 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4852 | streams_to_remove.push_back(stream); |
| 4853 | } |
| 4854 | } |
| 4855 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4856 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4857 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4858 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } |
| 4861 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4862 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4863 | const std::vector<cricket::StreamParams>& streams, |
| 4864 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4865 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4866 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4867 | // 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] | 4868 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4869 | for (auto sender_it = current_senders->begin(); |
| 4870 | sender_it != current_senders->end(); |
| 4871 | /* incremented manually */) { |
| 4872 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4873 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4874 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4875 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4876 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4877 | OnLocalSenderRemoved(info, media_type); |
| 4878 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4879 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4880 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4881 | } |
| 4882 | } |
| 4883 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4884 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4885 | for (const cricket::StreamParams& params : streams) { |
| 4886 | // 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] | 4887 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4888 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4889 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4890 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4891 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4892 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4893 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4894 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4895 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4896 | } |
| 4897 | } |
| 4898 | } |
| 4899 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4900 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4901 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4902 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4903 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4904 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4905 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4906 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4907 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4908 | return; |
| 4909 | } |
| 4910 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4911 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4912 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4913 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4914 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4915 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4916 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4917 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4918 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4919 | } |
| 4920 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4921 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4922 | cricket::MediaType media_type) { |
| 4923 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4924 | if (!sender) { |
| 4925 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4926 | // SessionDescriptions has been renegotiated. |
| 4927 | return; |
| 4928 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4929 | |
| 4930 | // A sender has been removed from the SessionDescription but it's still |
| 4931 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4932 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4933 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4934 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4935 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4936 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4937 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4938 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4939 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4943 | const cricket::StreamParamsVec& streams) { |
| 4944 | std::vector<std::string> existing_channels; |
| 4945 | |
| 4946 | // Find new and active data channels. |
| 4947 | for (const cricket::StreamParams& params : streams) { |
| 4948 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4949 | // we use the same naming of data channels as we do for |
| 4950 | // MediaStreams and Tracks. |
| 4951 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4952 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4953 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4954 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4955 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4956 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4957 | continue; |
| 4958 | } |
| 4959 | // Set the SSRC the data channel should use for sending. |
| 4960 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4961 | existing_channels.push_back(data_channel_it->first); |
| 4962 | } |
| 4963 | |
| 4964 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4965 | } |
| 4966 | |
| 4967 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4968 | const cricket::StreamParamsVec& streams) { |
| 4969 | std::vector<std::string> existing_channels; |
| 4970 | |
| 4971 | // Find new and active data channels. |
| 4972 | for (const cricket::StreamParams& params : streams) { |
| 4973 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4974 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4975 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4976 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4977 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4978 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4979 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4980 | // This is a new data channel. |
| 4981 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4982 | } else { |
| 4983 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4984 | } |
| 4985 | existing_channels.push_back(label); |
| 4986 | } |
| 4987 | |
| 4988 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4989 | } |
| 4990 | |
| 4991 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4992 | const std::vector<std::string>& active_channels, |
| 4993 | bool is_local_update) { |
| 4994 | auto it = rtp_data_channels_.begin(); |
| 4995 | while (it != rtp_data_channels_.end()) { |
| 4996 | DataChannel* data_channel = it->second; |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 4997 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4998 | ++it; |
| 4999 | continue; |
| 5000 | } |
| 5001 | |
| 5002 | if (is_local_update) { |
| 5003 | data_channel->SetSendSsrc(0); |
| 5004 | } else { |
| 5005 | data_channel->RemotePeerRequestClose(); |
| 5006 | } |
| 5007 | |
| 5008 | if (data_channel->state() == DataChannel::kClosed) { |
| 5009 | rtp_data_channels_.erase(it); |
| 5010 | it = rtp_data_channels_.begin(); |
| 5011 | } else { |
| 5012 | ++it; |
| 5013 | } |
| 5014 | } |
| 5015 | } |
| 5016 | |
| 5017 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 5018 | uint32_t remote_ssrc) { |
| 5019 | rtc::scoped_refptr<DataChannel> channel( |
| 5020 | InternalCreateDataChannel(label, nullptr)); |
| 5021 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5022 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5023 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5024 | return; |
| 5025 | } |
| 5026 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 5027 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5028 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5029 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 5033 | const std::string& label, |
| 5034 | const InternalDataChannelInit* config) { |
| 5035 | if (IsClosed()) { |
| 5036 | return nullptr; |
| 5037 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5038 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5039 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5040 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 5041 | return nullptr; |
| 5042 | } |
| 5043 | InternalDataChannelInit new_config = |
| 5044 | config ? (*config) : InternalDataChannelInit(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5045 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5046 | if (new_config.id < 0) { |
| 5047 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5048 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5049 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5050 | RTC_LOG(LS_ERROR) |
| 5051 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5052 | return nullptr; |
| 5053 | } |
| 5054 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5055 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5056 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5057 | return nullptr; |
| 5058 | } |
| 5059 | } |
| 5060 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5061 | rtc::scoped_refptr<DataChannel> channel( |
| 5062 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5063 | if (!channel) { |
| 5064 | sid_allocator_.ReleaseSid(new_config.id); |
| 5065 | return nullptr; |
| 5066 | } |
| 5067 | |
| 5068 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 5069 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5070 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 5071 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5072 | return nullptr; |
| 5073 | } |
| 5074 | rtp_data_channels_[channel->label()] = channel; |
| 5075 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5076 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5077 | sctp_data_channels_.push_back(channel); |
| 5078 | channel->SignalClosed.connect(this, |
| 5079 | &PeerConnection::OnSctpDataChannelClosed); |
| 5080 | } |
| 5081 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 5082 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5083 | return channel; |
| 5084 | } |
| 5085 | |
| 5086 | bool PeerConnection::HasDataChannels() const { |
| 5087 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 5088 | } |
| 5089 | |
| 5090 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 5091 | for (const auto& channel : sctp_data_channels_) { |
| 5092 | if (channel->id() < 0) { |
| 5093 | int sid; |
| 5094 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5095 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5096 | continue; |
| 5097 | } |
| 5098 | channel->SetSctpSid(sid); |
| 5099 | } |
| 5100 | } |
| 5101 | } |
| 5102 | |
| 5103 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 5104 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5105 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 5106 | ++it) { |
| 5107 | if (it->get() == channel) { |
| 5108 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5109 | // After the closing procedure is done, it's safe to use this ID for |
| 5110 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5111 | sid_allocator_.ReleaseSid(channel->id()); |
| 5112 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 5113 | // Since this method is triggered by a signal from the DataChannel, |
| 5114 | // we can't free it directly here; we need to free it asynchronously. |
| 5115 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5116 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 5117 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 5118 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5119 | return; |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5124 | void PeerConnection::OnDataChannelDestroyed() { |
| 5125 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 5126 | // DataChannel may callback to us and try to modify the list. |
| 5127 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 5128 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 5129 | for (const auto& kv : temp_rtp_dcs) { |
| 5130 | kv.second->OnTransportChannelDestroyed(); |
| 5131 | } |
| 5132 | |
| 5133 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 5134 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 5135 | for (const auto& channel : temp_sctp_dcs) { |
| 5136 | channel->OnTransportChannelDestroyed(); |
| 5137 | } |
| 5138 | } |
| 5139 | |
| 5140 | void PeerConnection::OnDataChannelOpenMessage( |
| 5141 | const std::string& label, |
| 5142 | const InternalDataChannelInit& config) { |
| 5143 | rtc::scoped_refptr<DataChannel> channel( |
| 5144 | InternalCreateDataChannel(label, &config)); |
| 5145 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5146 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5147 | return; |
| 5148 | } |
| 5149 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 5150 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5151 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5152 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 5153 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5154 | } |
| 5155 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5156 | bool PeerConnection::HandleOpenMessage_s( |
| 5157 | const cricket::ReceiveDataParams& params, |
| 5158 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5159 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 5160 | // Received OPEN message; parse and signal that a new data channel should |
| 5161 | // be created. |
| 5162 | std::string label; |
| 5163 | InternalDataChannelInit config; |
| 5164 | config.id = params.ssrc; |
| 5165 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 5166 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 5167 | << params.ssrc; |
| 5168 | return true; |
| 5169 | } |
| 5170 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5171 | OnDataChannelOpenMessage(label, config); |
| 5172 | return true; |
| 5173 | } |
| 5174 | return false; |
| 5175 | } |
| 5176 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5177 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5178 | PeerConnection::GetAudioTransceiver() const { |
| 5179 | // This method only works with Plan B SDP, where there is a single |
| 5180 | // audio/video transceiver. |
| 5181 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5182 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5183 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5184 | return transceiver; |
| 5185 | } |
| 5186 | } |
| 5187 | RTC_NOTREACHED(); |
| 5188 | return nullptr; |
| 5189 | } |
| 5190 | |
| 5191 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5192 | PeerConnection::GetVideoTransceiver() const { |
| 5193 | // This method only works with Plan B SDP, where there is a single |
| 5194 | // audio/video transceiver. |
| 5195 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5196 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5197 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5198 | return transceiver; |
| 5199 | } |
| 5200 | } |
| 5201 | RTC_NOTREACHED(); |
| 5202 | return nullptr; |
| 5203 | } |
| 5204 | |
| 5205 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 5206 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5207 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5208 | switch (type) { |
| 5209 | case cricket::MEDIA_TYPE_AUDIO: |
| 5210 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 5211 | case cricket::MEDIA_TYPE_VIDEO: |
| 5212 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 5213 | case cricket::MEDIA_TYPE_DATA: |
| 5214 | return false; |
| 5215 | } |
| 5216 | RTC_NOTREACHED(); |
| 5217 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5218 | } |
| 5219 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5220 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5221 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5222 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5223 | for (auto sender : transceiver->internal()->senders()) { |
| 5224 | if (sender->track() == track) { |
| 5225 | return sender; |
| 5226 | } |
| 5227 | } |
| 5228 | } |
| 5229 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5230 | } |
| 5231 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5232 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5233 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5234 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5235 | for (auto sender : transceiver->internal()->senders()) { |
| 5236 | if (sender->id() == sender_id) { |
| 5237 | return sender; |
| 5238 | } |
| 5239 | } |
| 5240 | } |
| 5241 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5242 | } |
| 5243 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5244 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 5245 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5246 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5247 | for (auto receiver : transceiver->internal()->receivers()) { |
| 5248 | if (receiver->id() == receiver_id) { |
| 5249 | return receiver; |
| 5250 | } |
| 5251 | } |
| 5252 | } |
| 5253 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5254 | } |
| 5255 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5256 | std::vector<PeerConnection::RtpSenderInfo>* |
| 5257 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 5258 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5259 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 5260 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5261 | ? &remote_audio_sender_infos_ |
| 5262 | : &remote_video_sender_infos_; |
| 5263 | } |
| 5264 | |
| 5265 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5266 | cricket::MediaType media_type) { |
| 5267 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5268 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5269 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 5270 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5271 | } |
| 5272 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5273 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 5274 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5275 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5276 | const std::string sender_id) const { |
| 5277 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5278 | if (sender_info.stream_id == stream_id && |
| 5279 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5280 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | return nullptr; |
| 5284 | } |
| 5285 | |
| 5286 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 5287 | for (const auto& channel : sctp_data_channels_) { |
| 5288 | if (channel->id() == sid) { |
| 5289 | return channel; |
| 5290 | } |
| 5291 | } |
| 5292 | return nullptr; |
| 5293 | } |
| 5294 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5295 | PeerConnection::InitializePortAllocatorResult |
| 5296 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5297 | const cricket::ServerAddresses& stun_servers, |
| 5298 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5299 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5300 | RTC_DCHECK_RUN_ON(network_thread()); |
| 5301 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 5302 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5303 | // To handle both internal and externally created port allocator, we will |
| 5304 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5305 | int port_allocator_flags = port_allocator_->flags(); |
| 5306 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5307 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5308 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5309 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5310 | // by experiment. |
| 5311 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5312 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 5313 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 5314 | .find("Disabled") == 0) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5315 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5316 | } |
| 5317 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5318 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5319 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5320 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5321 | } |
| 5322 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5323 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5324 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5325 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5326 | } |
| 5327 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5328 | if (configuration.candidate_network_policy == |
| 5329 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5330 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5331 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5332 | } |
| 5333 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5334 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5335 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5336 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5337 | } |
| 5338 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5339 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5340 | // No step delay is used while allocating ports. |
| 5341 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 5342 | port_allocator_->set_candidate_filter( |
| 5343 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 5344 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5345 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5346 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5347 | for (auto& turn_server : turn_servers_copy) { |
| 5348 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 5349 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5350 | // Call this last since it may create pooled allocator sessions using the |
| 5351 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5352 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5353 | stun_servers, std::move(turn_servers_copy), |
| 5354 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 5355 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5356 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame^] | 5357 | |
| 5358 | InitializePortAllocatorResult res; |
| 5359 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 5360 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5361 | } |
| 5362 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 5363 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5364 | const cricket::ServerAddresses& stun_servers, |
| 5365 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5366 | IceTransportsType type, |
| 5367 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 5368 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5369 | webrtc::TurnCustomizer* turn_customizer, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5370 | absl::optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5371 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5372 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5373 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5374 | // size is not allowed, and changing the set of ICE servers will not result |
| 5375 | // in new candidates being gathered. |
| 5376 | if (local_description()) { |
| 5377 | port_allocator_->FreezeCandidatePool(); |
| 5378 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5379 | // Add the custom tls turn servers if they exist. |
| 5380 | auto turn_servers_copy = turn_servers; |
| 5381 | for (auto& turn_server : turn_servers_copy) { |
| 5382 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5383 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5384 | // Call this last since it may create pooled allocator sessions using the |
| 5385 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 5386 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5387 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 5388 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5389 | } |
| 5390 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 5391 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5392 | return factory_->channel_manager(); |
| 5393 | } |
| 5394 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 5395 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5396 | std::unique_ptr<RtcEventLogOutput> output, |
| 5397 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5398 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5399 | if (!event_log_) { |
| 5400 | return false; |
| 5401 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5402 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5406 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5407 | if (event_log_) { |
| 5408 | event_log_->StopLogging(); |
| 5409 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5410 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 5411 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5412 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5413 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5414 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5415 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5416 | if (channel && channel->content_name() == content_name) { |
| 5417 | return channel; |
| 5418 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5419 | } |
| 5420 | if (rtp_data_channel() && |
| 5421 | rtp_data_channel()->content_name() == content_name) { |
| 5422 | return rtp_data_channel(); |
| 5423 | } |
| 5424 | return nullptr; |
| 5425 | } |
| 5426 | |
| 5427 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5428 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5429 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5430 | RTC_LOG(LS_INFO) |
| 5431 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5432 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5433 | return false; |
| 5434 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5435 | if (!sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5436 | 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] | 5437 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5438 | return false; |
| 5439 | } |
| 5440 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5441 | absl::optional<rtc::SSLRole> dtls_role; |
| 5442 | if (sctp_mid_) { |
| 5443 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 5444 | } else if (is_caller_) { |
| 5445 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5446 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5447 | if (dtls_role) { |
| 5448 | *role = *dtls_role; |
| 5449 | return true; |
| 5450 | } |
| 5451 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5455 | rtc::SSLRole* role) { |
| 5456 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5457 | RTC_LOG(LS_INFO) |
| 5458 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5459 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5460 | return false; |
| 5461 | } |
| 5462 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5463 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5464 | if (dtls_role) { |
| 5465 | *role = *dtls_role; |
| 5466 | return true; |
| 5467 | } |
| 5468 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5469 | } |
| 5470 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5471 | void PeerConnection::SetSessionError(SessionError error, |
| 5472 | const std::string& error_desc) { |
| 5473 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5474 | if (error != session_error_) { |
| 5475 | session_error_ = error; |
| 5476 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5477 | } |
| 5478 | } |
| 5479 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5480 | RTCError PeerConnection::UpdateSessionState( |
| 5481 | SdpType type, |
| 5482 | cricket::ContentSource source, |
| 5483 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5484 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5485 | |
| 5486 | // If there's already a pending error then no state transition should happen. |
| 5487 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5488 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5489 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5490 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5491 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5492 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | // Update the signaling state according to the specified state machine (see |
| 5496 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5497 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5498 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5499 | ? PeerConnectionInterface::kHaveLocalOffer |
| 5500 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5501 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5502 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5503 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 5504 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5505 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5506 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5507 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 5508 | } |
| 5509 | |
| 5510 | // Update internal objects according to the session description's media |
| 5511 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5512 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5513 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 5514 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5515 | } |
| 5516 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5517 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5518 | } |
| 5519 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5520 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5521 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5522 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5523 | const SessionDescriptionInterface* sdesc = |
| 5524 | (source == cricket::CS_LOCAL ? local_description() |
| 5525 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5526 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5527 | |
| 5528 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5529 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5530 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5531 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5532 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5533 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5534 | continue; |
| 5535 | } |
| 5536 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5537 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5538 | if (!content_desc) { |
| 5539 | continue; |
| 5540 | } |
| 5541 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5542 | bool success = (source == cricket::CS_LOCAL) |
| 5543 | ? channel->SetLocalContent(content_desc, type, &error) |
| 5544 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5545 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5546 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 5551 | if (rtp_data_channel_) { |
| 5552 | const ContentInfo* data_content = |
| 5553 | cricket::GetFirstDataContent(sdesc->description()); |
| 5554 | if (data_content && !data_content->rejected) { |
| 5555 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5556 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5557 | if (data_desc) { |
| 5558 | std::string error; |
| 5559 | bool success = |
| 5560 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5561 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5562 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5563 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5564 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5565 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5566 | } |
| 5567 | } |
| 5568 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5569 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5570 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 5571 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 5572 | if (sctp_transport_ && local_description() && remote_description() && |
| 5573 | cricket::GetFirstDataContent(local_description()->description()) && |
| 5574 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5575 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5576 | RTC_FROM_HERE, |
| 5577 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5578 | if (!success) { |
| 5579 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5580 | "Failed to push down SCTP parameters."); |
| 5581 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5582 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5583 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5584 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 5588 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5589 | RTC_DCHECK(local_description()); |
| 5590 | RTC_DCHECK(remote_description()); |
| 5591 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 5592 | // When we support "max-message-size", that would also be pushed down here. |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 5593 | return cricket_sctp_transport()->Start( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5594 | GetSctpPort(local_description()->description()), |
| 5595 | GetSctpPort(remote_description()->description())); |
| 5596 | } |
| 5597 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5598 | RTCError PeerConnection::PushdownTransportDescription( |
| 5599 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5600 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5601 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5602 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5603 | if (source == cricket::CS_LOCAL) { |
| 5604 | const SessionDescriptionInterface* sdesc = local_description(); |
| 5605 | RTC_DCHECK(sdesc); |
| 5606 | return transport_controller_->SetLocalDescription(type, |
| 5607 | sdesc->description()); |
| 5608 | } else { |
| 5609 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 5610 | RTC_DCHECK(sdesc); |
| 5611 | return transport_controller_->SetRemoteDescription(type, |
| 5612 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5613 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | bool PeerConnection::GetTransportDescription( |
| 5617 | const SessionDescription* description, |
| 5618 | const std::string& content_name, |
| 5619 | cricket::TransportDescription* tdesc) { |
| 5620 | if (!description || !tdesc) { |
| 5621 | return false; |
| 5622 | } |
| 5623 | const TransportInfo* transport_info = |
| 5624 | description->GetTransportInfoByName(content_name); |
| 5625 | if (!transport_info) { |
| 5626 | return false; |
| 5627 | } |
| 5628 | *tdesc = transport_info->description; |
| 5629 | return true; |
| 5630 | } |
| 5631 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5632 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 5633 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 5634 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5635 | switch (config.continual_gathering_policy) { |
| 5636 | case PeerConnectionInterface::GATHER_ONCE: |
| 5637 | gathering_policy = cricket::GATHER_ONCE; |
| 5638 | break; |
| 5639 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 5640 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 5641 | break; |
| 5642 | default: |
| 5643 | RTC_NOTREACHED(); |
| 5644 | gathering_policy = cricket::GATHER_ONCE; |
| 5645 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5646 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5647 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5648 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 5649 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5650 | ice_config.prioritize_most_likely_candidate_pairs = |
| 5651 | config.prioritize_most_likely_ice_candidate_pairs; |
| 5652 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5653 | RTCConfigurationToIceConfigOptionalInt( |
| 5654 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5655 | ice_config.continual_gathering_policy = gathering_policy; |
| 5656 | ice_config.presume_writable_when_fully_relayed = |
| 5657 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 5658 | ice_config.ice_check_interval_strong_connectivity = |
| 5659 | config.ice_check_interval_strong_connectivity; |
| 5660 | ice_config.ice_check_interval_weak_connectivity = |
| 5661 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5662 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-29 23:08:51 -0800 | [diff] [blame] | 5663 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 5664 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 5665 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5666 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5667 | ice_config.regather_all_networks_interval_range = |
| 5668 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5669 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5670 | return ice_config; |
| 5671 | } |
| 5672 | |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5673 | static absl::string_view GetTrackIdBySsrc( |
| 5674 | const SessionDescriptionInterface* session_description, |
| 5675 | uint32_t ssrc) { |
| 5676 | if (!session_description) { |
| 5677 | return {}; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5678 | } |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5679 | for (const cricket::ContentInfo& content : |
| 5680 | session_description->description()->contents()) { |
| 5681 | const cricket::MediaContentDescription& media = |
| 5682 | *content.media_description(); |
| 5683 | if (media.type() == cricket::MEDIA_TYPE_AUDIO || |
| 5684 | media.type() == cricket::MEDIA_TYPE_VIDEO) { |
| 5685 | const cricket::StreamParams* stream_params = |
| 5686 | cricket::GetStreamBySsrc(media.streams(), ssrc); |
| 5687 | if (stream_params) { |
| 5688 | return stream_params->id; |
| 5689 | } |
| 5690 | } |
| 5691 | } |
| 5692 | return {}; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5693 | } |
| 5694 | |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5695 | absl::string_view PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc) { |
| 5696 | return GetTrackIdBySsrc(local_description(), ssrc); |
| 5697 | } |
| 5698 | |
| 5699 | absl::string_view PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc) { |
| 5700 | return GetTrackIdBySsrc(remote_description(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5701 | } |
| 5702 | |
| 5703 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5704 | const rtc::CopyOnWriteBuffer& payload, |
| 5705 | cricket::SendDataResult* result) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5706 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
| 5707 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_, " |
| 5708 | "sctp_transport_, and media_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5709 | return false; |
| 5710 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5711 | if (media_transport_) { |
| 5712 | SendDataParams send_params; |
| 5713 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 5714 | send_params.ordered = params.ordered; |
| 5715 | if (params.max_rtx_count >= 0) { |
| 5716 | send_params.max_rtx_count = params.max_rtx_count; |
| 5717 | } else if (params.max_rtx_ms >= 0) { |
| 5718 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 5719 | } |
| 5720 | return media_transport_->SendData(params.sid, send_params, payload).ok(); |
| 5721 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5722 | return rtp_data_channel_ |
| 5723 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5724 | : network_thread()->Invoke<bool>( |
| 5725 | RTC_FROM_HERE, |
| 5726 | Bind(&cricket::SctpTransportInternal::SendData, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 5727 | cricket_sctp_transport(), params, payload, result)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5731 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5732 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5733 | // Don't log an error here, because DataChannels are expected to call |
| 5734 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5735 | // whether or not the underlying transport is ready. |
| 5736 | return false; |
| 5737 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5738 | if (media_transport_) { |
| 5739 | SignalMediaTransportWritable_s.connect(webrtc_data_channel, |
| 5740 | &DataChannel::OnChannelReady); |
| 5741 | SignalMediaTransportReceivedData_s.connect(webrtc_data_channel, |
| 5742 | &DataChannel::OnDataReceived); |
| 5743 | SignalMediaTransportChannelClosing_s.connect( |
| 5744 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5745 | SignalMediaTransportChannelClosed_s.connect( |
| 5746 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
| 5747 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5748 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5749 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5750 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5751 | &DataChannel::OnDataReceived); |
| 5752 | } else { |
| 5753 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5754 | &DataChannel::OnChannelReady); |
| 5755 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5756 | &DataChannel::OnDataReceived); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5757 | SignalSctpClosingProcedureStartedRemotely.connect( |
| 5758 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5759 | SignalSctpClosingProcedureComplete.connect( |
| 5760 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5761 | } |
| 5762 | return true; |
| 5763 | } |
| 5764 | |
| 5765 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5766 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5767 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5768 | RTC_LOG(LS_ERROR) |
| 5769 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5770 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5771 | return; |
| 5772 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5773 | if (media_transport_) { |
| 5774 | SignalMediaTransportWritable_s.disconnect(webrtc_data_channel); |
| 5775 | SignalMediaTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 5776 | SignalMediaTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 5777 | SignalMediaTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 5778 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5779 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5780 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5781 | } else { |
| 5782 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5783 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5784 | SignalSctpClosingProcedureStartedRemotely.disconnect(webrtc_data_channel); |
| 5785 | SignalSctpClosingProcedureComplete.disconnect(webrtc_data_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5786 | } |
| 5787 | } |
| 5788 | |
| 5789 | void PeerConnection::AddSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5790 | if (media_transport_) { |
Bjorn Mellem | f58e43e | 2019-02-22 10:31:48 -0800 | [diff] [blame] | 5791 | media_transport_->OpenChannel(sid); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5792 | return; |
| 5793 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5794 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5795 | RTC_LOG(LS_ERROR) |
| 5796 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5797 | return; |
| 5798 | } |
| 5799 | network_thread()->Invoke<void>( |
| 5800 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 5801 | cricket_sctp_transport(), sid)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | void PeerConnection::RemoveSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5805 | if (media_transport_) { |
| 5806 | media_transport_->CloseChannel(sid); |
| 5807 | return; |
| 5808 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5809 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5810 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5811 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5812 | return; |
| 5813 | } |
| 5814 | network_thread()->Invoke<void>( |
| 5815 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 5816 | cricket_sctp_transport(), sid)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | bool PeerConnection::ReadyToSendData() const { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5820 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5821 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5822 | (media_transport_ && media_transport_ready_to_send_data_) || |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5823 | sctp_ready_to_send_data_; |
| 5824 | } |
| 5825 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5826 | void PeerConnection::OnDataReceived(int channel_id, |
| 5827 | DataMessageType type, |
| 5828 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5829 | cricket::ReceiveDataParams params; |
| 5830 | params.sid = channel_id; |
| 5831 | params.type = ToCricketDataMessageType(type); |
| 5832 | media_transport_invoker_->AsyncInvoke<void>( |
| 5833 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 5834 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5835 | if (!HandleOpenMessage_s(params, buffer)) { |
| 5836 | SignalMediaTransportReceivedData_s(params, buffer); |
| 5837 | } |
| 5838 | }); |
| 5839 | } |
| 5840 | |
| 5841 | void PeerConnection::OnChannelClosing(int channel_id) { |
| 5842 | media_transport_invoker_->AsyncInvoke<void>( |
| 5843 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5844 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5845 | SignalMediaTransportChannelClosing_s(channel_id); |
| 5846 | }); |
| 5847 | } |
| 5848 | |
| 5849 | void PeerConnection::OnChannelClosed(int channel_id) { |
| 5850 | media_transport_invoker_->AsyncInvoke<void>( |
| 5851 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5852 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5853 | SignalMediaTransportChannelClosed_s(channel_id); |
| 5854 | }); |
| 5855 | } |
| 5856 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5857 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5858 | if (sctp_mid_ && transport_controller_) { |
| 5859 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5860 | if (dtls_transport) { |
| 5861 | return dtls_transport->transport_name(); |
| 5862 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5863 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5864 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5865 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5866 | } |
| 5867 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5868 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5869 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5870 | network_thread()->Invoke<void>( |
| 5871 | RTC_FROM_HERE, |
| 5872 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 5873 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5874 | return candidate_states_list; |
| 5875 | } |
| 5876 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5877 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5878 | const { |
| 5879 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5880 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5881 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5882 | if (channel) { |
| 5883 | transport_names_by_mid[channel->content_name()] = |
| 5884 | channel->transport_name(); |
| 5885 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5886 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5887 | if (rtp_data_channel_) { |
| 5888 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5889 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5890 | } |
| 5891 | if (sctp_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5892 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5893 | RTC_DCHECK(transport_name); |
| 5894 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5895 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5896 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5897 | } |
| 5898 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5899 | std::map<std::string, cricket::TransportStats> |
| 5900 | PeerConnection::GetTransportStatsByNames( |
| 5901 | const std::set<std::string>& transport_names) { |
| 5902 | if (!network_thread()->IsCurrent()) { |
| 5903 | return network_thread() |
| 5904 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5905 | RTC_FROM_HERE, |
| 5906 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5907 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5908 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5909 | for (const std::string& transport_name : transport_names) { |
| 5910 | cricket::TransportStats transport_stats; |
| 5911 | bool success = |
| 5912 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5913 | if (success) { |
| 5914 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5915 | } else { |
| 5916 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5917 | << transport_name; |
| 5918 | } |
| 5919 | } |
| 5920 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5921 | } |
| 5922 | |
| 5923 | bool PeerConnection::GetLocalCertificate( |
| 5924 | const std::string& transport_name, |
| 5925 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5926 | if (!certificate) { |
| 5927 | return false; |
| 5928 | } |
| 5929 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 5930 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5931 | } |
| 5932 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5933 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5934 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5935 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5936 | } |
| 5937 | |
| 5938 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5939 | return data_channel_type_; |
| 5940 | } |
| 5941 | |
| 5942 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5943 | return pending_ice_restarts_.find(content_name) != |
| 5944 | pending_ice_restarts_.end(); |
| 5945 | } |
| 5946 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5947 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5948 | return transport_controller_->NeedsIceRestart(content_name); |
| 5949 | } |
| 5950 | |
| 5951 | void PeerConnection::OnCertificateReady( |
| 5952 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5953 | transport_controller_->SetLocalCertificate(certificate); |
| 5954 | } |
| 5955 | |
| 5956 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5957 | SetSessionError(SessionError::kTransport, |
| 5958 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5959 | } |
| 5960 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 5961 | void PeerConnection::OnTransportControllerConnectionState( |
| 5962 | cricket::IceConnectionState state) { |
| 5963 | switch (state) { |
| 5964 | case cricket::kIceConnectionConnecting: |
| 5965 | // If the current state is Connected or Completed, then there were |
| 5966 | // writable channels but now there are not, so the next state must |
| 5967 | // be Disconnected. |
| 5968 | // kIceConnectionConnecting is currently used as the default, |
| 5969 | // un-connected state by the TransportController, so its only use is |
| 5970 | // detecting disconnections. |
| 5971 | if (ice_connection_state_ == |
| 5972 | PeerConnectionInterface::kIceConnectionConnected || |
| 5973 | ice_connection_state_ == |
| 5974 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5975 | SetIceConnectionState( |
| 5976 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5977 | } |
| 5978 | break; |
| 5979 | case cricket::kIceConnectionFailed: |
| 5980 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5981 | break; |
| 5982 | case cricket::kIceConnectionConnected: |
| 5983 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 5984 | "all transports are writable."; |
| 5985 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5986 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 5987 | break; |
| 5988 | case cricket::kIceConnectionCompleted: |
| 5989 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 5990 | "all transports are complete."; |
| 5991 | if (ice_connection_state_ != |
| 5992 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5993 | // If jumping directly from "checking" to "connected", |
| 5994 | // signal "connected" first. |
| 5995 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5996 | } |
| 5997 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5998 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 5999 | ReportTransportStats(); |
| 6000 | break; |
| 6001 | default: |
| 6002 | RTC_NOTREACHED(); |
| 6003 | } |
| 6004 | } |
| 6005 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6006 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 6007 | const std::string& transport_name, |
| 6008 | const cricket::Candidates& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6009 | int sdp_mline_index; |
| 6010 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6011 | RTC_LOG(LS_ERROR) |
| 6012 | << "OnTransportControllerCandidatesGathered: content name " |
| 6013 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6014 | return; |
| 6015 | } |
| 6016 | |
| 6017 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 6018 | citer != candidates.end(); ++citer) { |
| 6019 | // Use transport_name as the candidate media id. |
| 6020 | std::unique_ptr<JsepIceCandidate> candidate( |
| 6021 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 6022 | if (local_description()) { |
| 6023 | mutable_local_description()->AddCandidate(candidate.get()); |
| 6024 | } |
| 6025 | OnIceCandidate(std::move(candidate)); |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 6030 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6031 | // Sanity check. |
| 6032 | for (const cricket::Candidate& candidate : candidates) { |
| 6033 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6034 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6035 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6036 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6037 | return; |
| 6038 | } |
| 6039 | } |
| 6040 | |
| 6041 | if (local_description()) { |
| 6042 | mutable_local_description()->RemoveCandidates(candidates); |
| 6043 | } |
| 6044 | OnIceCandidatesRemoved(candidates); |
| 6045 | } |
| 6046 | |
| 6047 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 6048 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6049 | RTC_HISTOGRAM_ENUMERATION( |
| 6050 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 6051 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6052 | } |
| 6053 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6054 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6055 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6056 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6057 | if (channel && !channel->enabled()) { |
| 6058 | channel->Enable(true); |
| 6059 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6060 | } |
| 6061 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6062 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6063 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6064 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6065 | } |
| 6066 | |
| 6067 | // Returns the media index for a local ice candidate given the content name. |
| 6068 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 6069 | const std::string& content_name, |
| 6070 | int* sdp_mline_index) { |
| 6071 | if (!local_description() || !sdp_mline_index) { |
| 6072 | return false; |
| 6073 | } |
| 6074 | |
| 6075 | bool content_found = false; |
| 6076 | const ContentInfos& contents = local_description()->description()->contents(); |
| 6077 | for (size_t index = 0; index < contents.size(); ++index) { |
| 6078 | if (contents[index].name == content_name) { |
| 6079 | *sdp_mline_index = static_cast<int>(index); |
| 6080 | content_found = true; |
| 6081 | break; |
| 6082 | } |
| 6083 | } |
| 6084 | return content_found; |
| 6085 | } |
| 6086 | |
| 6087 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 6088 | const SessionDescriptionInterface* remote_desc) { |
| 6089 | if (!remote_desc) { |
| 6090 | return true; |
| 6091 | } |
| 6092 | bool ret = true; |
| 6093 | |
| 6094 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 6095 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 6096 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 6097 | const IceCandidateInterface* candidate = candidates->at(n); |
| 6098 | bool valid = false; |
| 6099 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 6100 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6101 | RTC_LOG(LS_INFO) |
| 6102 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6103 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6104 | } |
| 6105 | continue; |
| 6106 | } |
| 6107 | ret = UseCandidate(candidate); |
| 6108 | if (!ret) { |
| 6109 | break; |
| 6110 | } |
| 6111 | } |
| 6112 | } |
| 6113 | return ret; |
| 6114 | } |
| 6115 | |
| 6116 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 6117 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 6118 | size_t remote_content_size = |
| 6119 | remote_description()->description()->contents().size(); |
| 6120 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6121 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6122 | return false; |
| 6123 | } |
| 6124 | |
| 6125 | cricket::ContentInfo content = |
| 6126 | remote_description()->description()->contents()[mediacontent_index]; |
| 6127 | std::vector<cricket::Candidate> candidates; |
| 6128 | candidates.push_back(candidate->candidate()); |
| 6129 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6130 | RTCError error = |
| 6131 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6132 | if (error.ok()) { |
| 6133 | // Candidates successfully submitted for checking. |
| 6134 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6135 | ice_connection_state_ == |
| 6136 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6137 | // If state is New, then the session has just gotten its first remote ICE |
| 6138 | // candidates, so go to Checking. |
| 6139 | // If state is Disconnected, the session is re-using old candidates or |
| 6140 | // receiving additional ones, so go to Checking. |
| 6141 | // If state is Connected, stay Connected. |
| 6142 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6143 | // newly added transport, then the state actually _should_ move to |
| 6144 | // checking. Add a way to distinguish that case. |
| 6145 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6146 | } |
| 6147 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 6148 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6149 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6150 | } |
| 6151 | return true; |
| 6152 | } |
| 6153 | |
| 6154 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6155 | // Destroy video channel first since it may have a pointer to the |
| 6156 | // voice channel. |
| 6157 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6158 | if (!video_info || video_info->rejected) { |
| 6159 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6160 | } |
| 6161 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6162 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6163 | if (!audio_info || audio_info->rejected) { |
| 6164 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6168 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6169 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6170 | } |
| 6171 | } |
| 6172 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6173 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6174 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6175 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6176 | if (configuration_.bundle_policy == |
| 6177 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6178 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6179 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6180 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6181 | "max-bundle configured but session description " |
| 6182 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6183 | } |
| 6184 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 13:47:42 +0100 | [diff] [blame] | 6185 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6186 | } |
| 6187 | |
| 6188 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6189 | // Creating the media channels. Transports should already have been created |
| 6190 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6191 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6192 | if (voice && !voice->rejected && |
| 6193 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6194 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6195 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6196 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6197 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6198 | } |
| 6199 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 6200 | } |
| 6201 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6202 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6203 | if (video && !video->rejected && |
| 6204 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6205 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6206 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6207 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6208 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6209 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6210 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6211 | } |
| 6212 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6213 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6214 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6215 | !rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6216 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6217 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6218 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6219 | } |
| 6220 | } |
| 6221 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6222 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6223 | } |
| 6224 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6225 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6226 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6227 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6228 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 6229 | MediaTransportInterface* media_transport = nullptr; |
| 6230 | if (configuration_.use_media_transport) { |
| 6231 | media_transport = GetMediaTransport(mid); |
| 6232 | } |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6233 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6234 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6235 | call_.get(), configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 6236 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6237 | &ssrc_generator_, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6238 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6239 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6240 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6241 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6242 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6243 | voice_channel->SignalSentPacket.connect(this, |
| 6244 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6245 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6246 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6247 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6248 | } |
| 6249 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6250 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6251 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6252 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6253 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 6254 | MediaTransportInterface* media_transport = nullptr; |
| 6255 | if (configuration_.use_media_transport) { |
| 6256 | media_transport = GetMediaTransport(mid); |
| 6257 | } |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6258 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6259 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 6260 | call_.get(), configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 6261 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6262 | &ssrc_generator_, video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6263 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6264 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6265 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6266 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6267 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6268 | video_channel->SignalSentPacket.connect(this, |
| 6269 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6270 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6271 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6272 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6273 | } |
| 6274 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6275 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6276 | switch (data_channel_type_) { |
| 6277 | case cricket::DCT_MEDIA_TRANSPORT: |
| 6278 | if (network_thread()->Invoke<bool>( |
| 6279 | RTC_FROM_HERE, |
| 6280 | rtc::Bind(&PeerConnection::SetupMediaTransportForDataChannels_n, |
| 6281 | this, mid))) { |
| 6282 | for (const auto& channel : sctp_data_channels_) { |
| 6283 | channel->OnTransportChannelCreated(); |
| 6284 | } |
| 6285 | return true; |
| 6286 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6287 | return false; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6288 | case cricket::DCT_SCTP: |
| 6289 | if (!sctp_factory_) { |
| 6290 | RTC_LOG(LS_ERROR) |
| 6291 | << "Trying to create SCTP transport, but didn't compile with " |
| 6292 | "SCTP support (HAVE_SCTP)"; |
| 6293 | return false; |
| 6294 | } |
| 6295 | if (!network_thread()->Invoke<bool>( |
| 6296 | RTC_FROM_HERE, |
| 6297 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
| 6298 | return false; |
| 6299 | } |
| 6300 | for (const auto& channel : sctp_data_channels_) { |
| 6301 | channel->OnTransportChannelCreated(); |
| 6302 | } |
| 6303 | return true; |
| 6304 | case cricket::DCT_RTP: |
| 6305 | default: |
| 6306 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 6307 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 6308 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6309 | SrtpRequired(), GetCryptoOptions(), &ssrc_generator_); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6310 | if (!rtp_data_channel_) { |
| 6311 | return false; |
| 6312 | } |
| 6313 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 6314 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6315 | rtp_data_channel_->SignalSentPacket.connect( |
| 6316 | this, &PeerConnection::OnSentPacket_w); |
| 6317 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 6318 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6319 | } |
| 6320 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6321 | return true; |
| 6322 | } |
| 6323 | |
| 6324 | Call::Stats PeerConnection::GetCallStats() { |
| 6325 | if (!worker_thread()->IsCurrent()) { |
| 6326 | return worker_thread()->Invoke<Call::Stats>( |
| 6327 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6328 | } |
| 6329 | if (call_) { |
| 6330 | return call_->GetStats(); |
| 6331 | } else { |
| 6332 | return Call::Stats(); |
| 6333 | } |
| 6334 | } |
| 6335 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6336 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6337 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6338 | RTC_DCHECK(sctp_factory_); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6339 | rtc::scoped_refptr<DtlsTransport> webrtc_dtls_transport = |
| 6340 | transport_controller_->LookupDtlsTransportByMid(mid); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6341 | cricket::DtlsTransportInternal* dtls_transport = |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6342 | webrtc_dtls_transport->internal(); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6343 | RTC_DCHECK(dtls_transport); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6344 | std::unique_ptr<cricket::SctpTransportInternal> cricket_sctp_transport = |
| 6345 | sctp_factory_->CreateSctpTransport(dtls_transport); |
| 6346 | RTC_DCHECK(cricket_sctp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6347 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6348 | cricket_sctp_transport->SignalReadyToSendData.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6349 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6350 | cricket_sctp_transport->SignalDataReceived.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6351 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6352 | // TODO(deadbeef): All we do here is AsyncInvoke to fire the signal on |
| 6353 | // another thread. Would be nice if there was a helper class similar to |
| 6354 | // sigslot::repeater that did this for us, eliminating a bunch of boilerplate |
| 6355 | // code. |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6356 | cricket_sctp_transport->SignalClosingProcedureStartedRemotely.connect( |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6357 | this, &PeerConnection::OnSctpClosingProcedureStartedRemotely_n); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6358 | cricket_sctp_transport->SignalClosingProcedureComplete.connect( |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6359 | this, &PeerConnection::OnSctpClosingProcedureComplete_n); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6360 | sctp_mid_ = mid; |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6361 | sctp_transport_ = new rtc::RefCountedObject<SctpTransport>( |
| 6362 | std::move(cricket_sctp_transport)); |
| 6363 | sctp_transport_->SetDtlsTransport(std::move(webrtc_dtls_transport)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6364 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6365 | } |
| 6366 | |
| 6367 | void PeerConnection::DestroySctpTransport_n() { |
| 6368 | RTC_DCHECK(network_thread()->IsCurrent()); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 6369 | sctp_transport_->Clear(); |
| 6370 | sctp_transport_ = nullptr; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6371 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6372 | sctp_invoker_.reset(nullptr); |
| 6373 | sctp_ready_to_send_data_ = false; |
| 6374 | } |
| 6375 | |
| 6376 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 6377 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6378 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6379 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6380 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6381 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6382 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6383 | // pending tasks will be cleared. |
| 6384 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 6385 | OnSctpTransportReadyToSendData_s(true); |
| 6386 | }); |
| 6387 | } |
| 6388 | |
| 6389 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 6390 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6391 | sctp_ready_to_send_data_ = ready; |
| 6392 | SignalSctpReadyToSendData(ready); |
| 6393 | } |
| 6394 | |
| 6395 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 6396 | const cricket::ReceiveDataParams& params, |
| 6397 | const rtc::CopyOnWriteBuffer& payload) { |
| 6398 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6399 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6400 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6401 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6402 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6403 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6404 | // pending tasks will be cleared. |
| 6405 | sctp_invoker_->AsyncInvoke<void>( |
| 6406 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 6407 | OnSctpTransportDataReceived_s(params, payload); |
| 6408 | }); |
| 6409 | } |
| 6410 | |
| 6411 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 6412 | const cricket::ReceiveDataParams& params, |
| 6413 | const rtc::CopyOnWriteBuffer& payload) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6414 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6415 | if (!HandleOpenMessage_s(params, payload)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6416 | SignalSctpDataReceived(params, payload); |
| 6417 | } |
| 6418 | } |
| 6419 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6420 | void PeerConnection::OnSctpClosingProcedureStartedRemotely_n(int sid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6421 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6422 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6423 | sctp_invoker_->AsyncInvoke<void>( |
| 6424 | RTC_FROM_HERE, signaling_thread(), |
| 6425 | rtc::Bind(&sigslot::signal1<int>::operator(), |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6426 | &SignalSctpClosingProcedureStartedRemotely, sid)); |
| 6427 | } |
| 6428 | |
| 6429 | void PeerConnection::OnSctpClosingProcedureComplete_n(int sid) { |
| 6430 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6431 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6432 | sctp_invoker_->AsyncInvoke<void>( |
| 6433 | RTC_FROM_HERE, signaling_thread(), |
| 6434 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 6435 | &SignalSctpClosingProcedureComplete, sid)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6436 | } |
| 6437 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6438 | bool PeerConnection::SetupMediaTransportForDataChannels_n( |
| 6439 | const std::string& mid) { |
| 6440 | media_transport_ = transport_controller_->GetMediaTransport(mid); |
| 6441 | if (!media_transport_) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 6442 | RTC_LOG(LS_ERROR) |
| 6443 | << "Media transport is not available for data channels, mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6444 | return false; |
| 6445 | } |
| 6446 | |
| 6447 | media_transport_invoker_ = absl::make_unique<rtc::AsyncInvoker>(); |
| 6448 | media_transport_->SetDataSink(this); |
| 6449 | media_transport_data_mid_ = mid; |
| 6450 | transport_controller_->SignalMediaTransportStateChanged.connect( |
| 6451 | this, &PeerConnection::OnMediaTransportStateChanged_n); |
| 6452 | // Check the initial state right away, in case transport is already writable. |
| 6453 | OnMediaTransportStateChanged_n(); |
| 6454 | return true; |
| 6455 | } |
| 6456 | |
| 6457 | void PeerConnection::TeardownMediaTransportForDataChannels_n() { |
| 6458 | if (!media_transport_) { |
| 6459 | return; |
| 6460 | } |
| 6461 | transport_controller_->SignalMediaTransportStateChanged.disconnect(this); |
| 6462 | media_transport_data_mid_.reset(); |
| 6463 | media_transport_->SetDataSink(nullptr); |
| 6464 | media_transport_invoker_ = nullptr; |
| 6465 | media_transport_ = nullptr; |
| 6466 | } |
| 6467 | |
| 6468 | void PeerConnection::OnMediaTransportStateChanged_n() { |
| 6469 | if (!media_transport_data_mid_ || |
| 6470 | transport_controller_->GetMediaTransportState( |
| 6471 | *media_transport_data_mid_) != MediaTransportState::kWritable) { |
| 6472 | return; |
| 6473 | } |
| 6474 | media_transport_invoker_->AsyncInvoke<void>( |
| 6475 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 6476 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6477 | media_transport_ready_to_send_data_ = true; |
| 6478 | SignalMediaTransportWritable_s(media_transport_ready_to_send_data_); |
| 6479 | }); |
| 6480 | } |
| 6481 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6482 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6483 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6484 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6485 | if (!bundle_enabled) |
| 6486 | return true; |
| 6487 | |
| 6488 | const cricket::ContentGroup* bundle_group = |
| 6489 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6490 | RTC_DCHECK(bundle_group != NULL); |
| 6491 | |
| 6492 | const cricket::ContentInfos& contents = desc->contents(); |
| 6493 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6494 | citer != contents.end(); ++citer) { |
| 6495 | const cricket::ContentInfo* content = (&*citer); |
| 6496 | RTC_DCHECK(content != NULL); |
| 6497 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 6498 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6499 | if (!HasRtcpMuxEnabled(content)) |
| 6500 | return false; |
| 6501 | } |
| 6502 | } |
| 6503 | // RTCP-MUX is enabled in all the contents. |
| 6504 | return true; |
| 6505 | } |
| 6506 | |
| 6507 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6508 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6509 | } |
| 6510 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6511 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6512 | std::set<std::string> mids; |
| 6513 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 11:32:20 -0800 | [diff] [blame] | 6514 | if (content.name.empty()) { |
| 6515 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6516 | "A media section is missing a MID attribute."); |
| 6517 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6518 | if (!mids.insert(content.name).second) { |
| 6519 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6520 | "Duplicate a=mid value '" + content.name + "'."); |
| 6521 | } |
| 6522 | } |
| 6523 | return RTCError::OK(); |
| 6524 | } |
| 6525 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6526 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6527 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6528 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6529 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6530 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6534 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6535 | } |
| 6536 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6537 | SdpType type = sdesc->GetType(); |
| 6538 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6539 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6540 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 6541 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6542 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6543 | } |
| 6544 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6545 | RTCError error = ValidateMids(*sdesc->description()); |
| 6546 | if (!error.ok()) { |
| 6547 | return error; |
| 6548 | } |
| 6549 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6550 | // Verify crypto settings. |
| 6551 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6552 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6553 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6554 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6555 | if (!crypto_error.ok()) { |
| 6556 | return crypto_error; |
| 6557 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6558 | } |
| 6559 | |
| 6560 | // Verify ice-ufrag and ice-pwd. |
| 6561 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6562 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6563 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6564 | } |
| 6565 | |
| 6566 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6567 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6568 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6569 | } |
| 6570 | |
| 6571 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6572 | // m-lines that do not rtcp-mux enabled. |
| 6573 | |
| 6574 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6575 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6576 | // With an answer we want to compare the new answer session description with |
| 6577 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6578 | const cricket::SessionDescription* offer_desc = |
| 6579 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6580 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6581 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6582 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6583 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6584 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6585 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6586 | } |
| 6587 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6588 | // The re-offers should respect the order of m= sections in current |
| 6589 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6590 | // With a re-offer, either the current local or current remote descriptions |
| 6591 | // could be the most up to date, so we would like to check against both of |
| 6592 | // them if they exist. It could be the case that one of them has a 0 port |
| 6593 | // for a media section, but the other does not. This is important to check |
| 6594 | // against in the case that we are recycling an m= section. |
| 6595 | const cricket::SessionDescription* current_desc = nullptr; |
| 6596 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6597 | if (local_description()) { |
| 6598 | current_desc = local_description()->description(); |
| 6599 | if (remote_description()) { |
| 6600 | secondary_current_desc = remote_description()->description(); |
| 6601 | } |
| 6602 | } else if (remote_description()) { |
| 6603 | current_desc = remote_description()->description(); |
| 6604 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6605 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6606 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6607 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6608 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6609 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6610 | } |
| 6611 | } |
| 6612 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6613 | if (IsUnifiedPlan()) { |
| 6614 | // Ensure that each audio and video media section has at most one |
| 6615 | // "StreamParams". This will return an error if receiving a session |
| 6616 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6617 | // same type. With Unified Plan, there can only be at most one track per |
| 6618 | // media section. |
| 6619 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 6620 | const MediaContentDescription& desc = *content.description; |
| 6621 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6622 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6623 | desc.streams().size() > 1u) { |
| 6624 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6625 | "Media section has more than one track specified " |
| 6626 | "with a=ssrc lines which is not supported with " |
| 6627 | "Unified Plan."); |
| 6628 | } |
| 6629 | } |
| 6630 | } |
| 6631 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6632 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6633 | } |
| 6634 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6635 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6636 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6637 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6638 | return (state == PeerConnectionInterface::kStable) || |
| 6639 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6640 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6641 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6642 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6643 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6644 | } |
| 6645 | } |
| 6646 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6647 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6648 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6649 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6650 | return (state == PeerConnectionInterface::kStable) || |
| 6651 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6652 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6653 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6654 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6655 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6656 | } |
| 6657 | } |
| 6658 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6659 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6660 | switch (error) { |
| 6661 | case SessionError::kNone: |
| 6662 | return "ERROR_NONE"; |
| 6663 | case SessionError::kContent: |
| 6664 | return "ERROR_CONTENT"; |
| 6665 | case SessionError::kTransport: |
| 6666 | return "ERROR_TRANSPORT"; |
| 6667 | } |
| 6668 | RTC_NOTREACHED(); |
| 6669 | return ""; |
| 6670 | } |
| 6671 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6672 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 6673 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6674 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6675 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 6676 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6677 | } |
| 6678 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6679 | void PeerConnection::ReportSdpFormatReceived( |
| 6680 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6681 | int num_audio_mlines = 0; |
| 6682 | int num_video_mlines = 0; |
| 6683 | int num_audio_tracks = 0; |
| 6684 | int num_video_tracks = 0; |
| 6685 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6686 | cricket::MediaType media_type = content.media_description()->type(); |
| 6687 | int num_tracks = std::max( |
| 6688 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6689 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6690 | num_audio_mlines += 1; |
| 6691 | num_audio_tracks += num_tracks; |
| 6692 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6693 | num_video_mlines += 1; |
| 6694 | num_video_tracks += num_tracks; |
| 6695 | } |
| 6696 | } |
| 6697 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6698 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6699 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6700 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6701 | format = kSdpFormatReceivedComplexPlanB; |
| 6702 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6703 | format = kSdpFormatReceivedSimple; |
| 6704 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6705 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6706 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6707 | } |
| 6708 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6709 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6710 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6711 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6712 | } |
| 6713 | |
| 6714 | void PeerConnection::ReportUsagePattern() const { |
| 6715 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6716 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6717 | usage_event_accumulator_, |
| 6718 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6719 | const int bad_bits = |
| 6720 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED) | |
| 6721 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6722 | const int good_bits = |
| 6723 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED) | |
| 6724 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6725 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6726 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6727 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6728 | // If called after close(), we can't report, because observer may have |
| 6729 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6730 | if (observer_) { |
| 6731 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6732 | } else { |
| 6733 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6734 | << usage_event_accumulator_ |
| 6735 | << " observed after observer shutdown"; |
| 6736 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6737 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6738 | } |
| 6739 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6740 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 6741 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6742 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6743 | switch (answer.description()->msid_signaling()) { |
| 6744 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6745 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6746 | break; |
| 6747 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6748 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6749 | break; |
| 6750 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6751 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6752 | break; |
| 6753 | case cricket::kMsidSignalingMediaSection | |
| 6754 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6755 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6756 | break; |
| 6757 | default: |
| 6758 | RTC_NOTREACHED(); |
| 6759 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6760 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 6761 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6762 | } |
| 6763 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6764 | // We need to check the local/remote description for the Transport instead of |
| 6765 | // the session, because a new Transport added during renegotiation may have |
| 6766 | // them unset while the session has them set from the previous negotiation. |
| 6767 | // Not doing so may trigger the auto generation of transport description and |
| 6768 | // mess up DTLS identity information, ICE credential, etc. |
| 6769 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 6770 | const IceCandidateInterface* candidate, |
| 6771 | const SessionDescriptionInterface* remote_desc, |
| 6772 | bool* valid) { |
| 6773 | *valid = true; |
| 6774 | |
| 6775 | const SessionDescriptionInterface* current_remote_desc = |
| 6776 | remote_desc ? remote_desc : remote_description(); |
| 6777 | |
| 6778 | if (!current_remote_desc) { |
| 6779 | return false; |
| 6780 | } |
| 6781 | |
| 6782 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 6783 | size_t remote_content_size = |
| 6784 | current_remote_desc->description()->contents().size(); |
| 6785 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6786 | RTC_LOG(LS_ERROR) |
| 6787 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 6788 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6789 | |
| 6790 | *valid = false; |
| 6791 | return false; |
| 6792 | } |
| 6793 | |
| 6794 | cricket::ContentInfo content = |
| 6795 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 6796 | |
| 6797 | const std::string transport_name = GetTransportName(content.name); |
| 6798 | if (transport_name.empty()) { |
| 6799 | return false; |
| 6800 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6801 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | bool PeerConnection::SrtpRequired() const { |
| 6805 | return dtls_enabled_ || |
| 6806 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 6807 | } |
| 6808 | |
| 6809 | void PeerConnection::OnTransportControllerGatheringState( |
| 6810 | cricket::IceGatheringState state) { |
| 6811 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6812 | if (state == cricket::kIceGatheringGathering) { |
| 6813 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 6814 | } else if (state == cricket::kIceGatheringComplete) { |
| 6815 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 6816 | } |
| 6817 | } |
| 6818 | |
| 6819 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6820 | std::map<std::string, std::set<cricket::MediaType>> |
| 6821 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6822 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6823 | if (transceiver->internal()->channel()) { |
| 6824 | const std::string& transport_name = |
| 6825 | transceiver->internal()->channel()->transport_name(); |
| 6826 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 6827 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6828 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6829 | } |
| 6830 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6831 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 6832 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6833 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6834 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6835 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6836 | if (transport_name) { |
| 6837 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6838 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6839 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6840 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6841 | for (const auto& entry : media_types_by_transport_name) { |
| 6842 | const std::string& transport_name = entry.first; |
| 6843 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6844 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6845 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6846 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6847 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6848 | } |
| 6849 | } |
| 6850 | } |
| 6851 | // Walk through the ConnectionInfos to gather best connection usage |
| 6852 | // for IPv4 and IPv6. |
| 6853 | void PeerConnection::ReportBestConnectionState( |
| 6854 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6855 | for (const cricket::TransportChannelStats& channel_stats : |
| 6856 | stats.channel_stats) { |
| 6857 | for (const cricket::ConnectionInfo& connection_info : |
| 6858 | channel_stats.connection_infos) { |
| 6859 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6860 | continue; |
| 6861 | } |
| 6862 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6863 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6864 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6865 | |
| 6866 | // Increment the counter for IceCandidatePairType. |
| 6867 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6868 | (local.type() == RELAY_PORT_TYPE && |
| 6869 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6870 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 6871 | GetIceCandidatePairCounter(local, remote), |
| 6872 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6873 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6874 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 6875 | GetIceCandidatePairCounter(local, remote), |
| 6876 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6877 | } else { |
| 6878 | RTC_CHECK(0); |
| 6879 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6880 | |
| 6881 | // Increment the counter for IP type. |
| 6882 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6883 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6884 | kBestConnections_IPv4, |
| 6885 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6886 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6887 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6888 | kBestConnections_IPv6, |
| 6889 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6890 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 6891 | RTC_CHECK(!local.address().hostname().empty() && |
| 6892 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | return; |
| 6896 | } |
| 6897 | } |
| 6898 | } |
| 6899 | |
| 6900 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6901 | const cricket::TransportStats& stats, |
| 6902 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6903 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6904 | return; |
| 6905 | } |
| 6906 | |
| 6907 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6908 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6909 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6910 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6911 | return; |
| 6912 | } |
| 6913 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6914 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6915 | for (cricket::MediaType media_type : media_types) { |
| 6916 | switch (media_type) { |
| 6917 | case cricket::MEDIA_TYPE_AUDIO: |
| 6918 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6919 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 6920 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6921 | break; |
| 6922 | case cricket::MEDIA_TYPE_VIDEO: |
| 6923 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6924 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 6925 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6926 | break; |
| 6927 | case cricket::MEDIA_TYPE_DATA: |
| 6928 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6929 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 6930 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6931 | break; |
| 6932 | default: |
| 6933 | RTC_NOTREACHED(); |
| 6934 | continue; |
| 6935 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6936 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6937 | } |
| 6938 | |
| 6939 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6940 | for (cricket::MediaType media_type : media_types) { |
| 6941 | switch (media_type) { |
| 6942 | case cricket::MEDIA_TYPE_AUDIO: |
| 6943 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6944 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 6945 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6946 | break; |
| 6947 | case cricket::MEDIA_TYPE_VIDEO: |
| 6948 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6949 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 6950 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6951 | break; |
| 6952 | case cricket::MEDIA_TYPE_DATA: |
| 6953 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6954 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 6955 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6956 | break; |
| 6957 | default: |
| 6958 | RTC_NOTREACHED(); |
| 6959 | continue; |
| 6960 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6961 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6962 | } |
| 6963 | } |
| 6964 | |
| 6965 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6966 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6967 | RTC_DCHECK(call_); |
| 6968 | call_->OnSentPacket(sent_packet); |
| 6969 | } |
| 6970 | |
| 6971 | const std::string PeerConnection::GetTransportName( |
| 6972 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6973 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6974 | if (channel) { |
| 6975 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6976 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6977 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6978 | RTC_DCHECK(sctp_mid_); |
| 6979 | if (content_name == *sctp_mid_) { |
| 6980 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6981 | } |
| 6982 | } |
| 6983 | // Return an empty string if failed to retrieve the transport name. |
| 6984 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6985 | } |
| 6986 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6987 | void PeerConnection::DestroyTransceiverChannel( |
| 6988 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6989 | transceiver) { |
| 6990 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6991 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6992 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6993 | if (channel) { |
| 6994 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6995 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6996 | } |
| 6997 | } |
| 6998 | |
| 6999 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7000 | if (rtp_data_channel_) { |
| 7001 | OnDataChannelDestroyed(); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7002 | DestroyChannelInterface(rtp_data_channel_); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7003 | rtp_data_channel_ = nullptr; |
| 7004 | } |
| 7005 | |
| 7006 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 7007 | // grab a reference to this PeerConnection. If this is called from the |
| 7008 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 7009 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 7010 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 7011 | |
| 7012 | if (sctp_transport_) { |
| 7013 | OnDataChannelDestroyed(); |
| 7014 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 7015 | [this] { DestroySctpTransport_n(); }); |
| 7016 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7017 | |
| 7018 | if (media_transport_) { |
| 7019 | OnDataChannelDestroyed(); |
| 7020 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 7021 | RTC_DCHECK_RUN_ON(network_thread()); |
| 7022 | TeardownMediaTransportForDataChannels_n(); |
| 7023 | }); |
| 7024 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7025 | } |
| 7026 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7027 | void PeerConnection::DestroyChannelInterface( |
| 7028 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7029 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7030 | switch (channel->media_type()) { |
| 7031 | case cricket::MEDIA_TYPE_AUDIO: |
| 7032 | channel_manager()->DestroyVoiceChannel( |
| 7033 | static_cast<cricket::VoiceChannel*>(channel)); |
| 7034 | break; |
| 7035 | case cricket::MEDIA_TYPE_VIDEO: |
| 7036 | channel_manager()->DestroyVideoChannel( |
| 7037 | static_cast<cricket::VideoChannel*>(channel)); |
| 7038 | break; |
| 7039 | case cricket::MEDIA_TYPE_DATA: |
| 7040 | channel_manager()->DestroyRtpDataChannel( |
| 7041 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 7042 | break; |
| 7043 | default: |
| 7044 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 7045 | break; |
| 7046 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7047 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7048 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7049 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7050 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7051 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 7052 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 7053 | MediaTransportInterface* media_transport) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 7054 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7055 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7056 | auto base_channel = GetChannel(mid); |
| 7057 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7058 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7059 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7060 | if (sctp_transport_ && mid == sctp_mid_) { |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 7061 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7062 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 7063 | |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 7064 | if (use_media_transport_) { |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 7065 | // Only pass media transport to call object if media transport is used |
| 7066 | // for media (and not data channel). |
| 7067 | call_->MediaTransportChange(media_transport); |
| 7068 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 7069 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7070 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7071 | } |
| 7072 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7073 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 7074 | // In earlier production code, the pointer was not cleared on close, |
| 7075 | // which might have led to undefined behavior if the observer was not |
| 7076 | // deallocated, or strange crashes if it was. |
| 7077 | // We use CHECK in order to catch such behavior if it exists. |
| 7078 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 7079 | RTC_CHECK(observer_); |
| 7080 | return observer_; |
| 7081 | } |
| 7082 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 7083 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 7084 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 7085 | // after it has been removed. |
| 7086 | return configuration_.crypto_options.has_value() |
| 7087 | ? *configuration_.crypto_options |
| 7088 | : factory_->options().crypto_options; |
| 7089 | } |
| 7090 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 7091 | void PeerConnection::ClearStatsCache() { |
| 7092 | if (stats_collector_) { |
| 7093 | stats_collector_->ClearCachedStatsReport(); |
| 7094 | } |
| 7095 | } |
| 7096 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7097 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 7098 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 7099 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7100 | } |
| 7101 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 7102 | } // namespace webrtc |