henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [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 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/peer_connection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 14 | #include <limits> |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 15 | #include <memory> |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 16 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 17 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 11:04:15 | [diff] [blame] | 18 | #include <utility> |
| 19 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 20 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 21 | #include "absl/algorithm/container.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 20:02:22 | [diff] [blame] | 22 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [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" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 27 | #include "api/rtc_error.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 10:24:53 | [diff] [blame] | 28 | #include "api/rtc_event_log/rtc_event_log.h" |
Niels Möller | d8b9ed7 | 2019-05-08 11:53:51 | [diff] [blame] | 29 | #include "api/rtc_event_log_output_file.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 30 | #include "api/rtp_parameters.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 31 | #include "api/uma_metrics.h" |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 32 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 33 | #include "call/call.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 34 | #include "logging/rtc_event_log/ice_logger.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 35 | #include "media/base/rid_description.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 36 | #include "media/sctp/sctp_transport.h" |
Ruslan Burakov | 501bfba | 2019-02-11 09:29:19 | [diff] [blame] | 37 | #include "pc/audio_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 38 | #include "pc/audio_track.h" |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 39 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 40 | #include "pc/channel_manager.h" |
| 41 | #include "pc/dtmf_sender.h" |
| 42 | #include "pc/media_stream.h" |
| 43 | #include "pc/media_stream_observer.h" |
| 44 | #include "pc/remote_audio_source.h" |
| 45 | #include "pc/rtp_media_utils.h" |
| 46 | #include "pc/rtp_receiver.h" |
| 47 | #include "pc/rtp_sender.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 48 | #include "pc/sctp_transport.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 49 | #include "pc/sctp_utils.h" |
| 50 | #include "pc/sdp_utils.h" |
| 51 | #include "pc/stream_collection.h" |
Ruslan Burakov | 501bfba | 2019-02-11 09:29:19 | [diff] [blame] | 52 | #include "pc/video_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 53 | #include "pc/video_track.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 54 | #include "rtc_base/bind.h" |
| 55 | #include "rtc_base/checks.h" |
| 56 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 57 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 58 | #include "rtc_base/strings/string_builder.h" |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 59 | #include "rtc_base/system/fallthrough.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 60 | #include "rtc_base/trace_event.h" |
| 61 | #include "system_wrappers/include/clock.h" |
| 62 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 63 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 64 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 65 | using cricket::ContentInfo; |
| 66 | using cricket::ContentInfos; |
| 67 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 68 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 69 | using cricket::RidDescription; |
| 70 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 71 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 72 | using cricket::SimulcastDescription; |
| 73 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 74 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 75 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 76 | using cricket::TransportInfo; |
| 77 | |
| 78 | using cricket::LOCAL_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 79 | using cricket::PRFLX_PORT_TYPE; |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 80 | using cricket::RELAY_PORT_TYPE; |
| 81 | using cricket::STUN_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 82 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 83 | namespace webrtc { |
| 84 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 85 | // Error messages |
| 86 | const char kBundleWithoutRtcpMux[] = |
| 87 | "rtcp-mux must be enabled when BUNDLE " |
| 88 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 89 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 90 | const char kInvalidSdp[] = "Invalid session description."; |
| 91 | const char kMlineMismatchInAnswer[] = |
| 92 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 93 | "answer."; |
| 94 | const char kMlineMismatchInSubsequentOffer[] = |
| 95 | "The order of m-lines in subsequent offer doesn't match order from " |
| 96 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 97 | const char kSdpWithoutDtlsFingerprint[] = |
| 98 | "Called with SDP without DTLS fingerprint."; |
| 99 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 100 | const char kSdpWithoutIceUfragPwd[] = |
| 101 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 102 | const char kSessionError[] = "Session error code: "; |
| 103 | const char kSessionErrorDesc[] = "Session error description: "; |
| 104 | const char kDtlsSrtpSetupFailureRtp[] = |
| 105 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 106 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 107 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 108 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 109 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 110 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 111 | // Field trials. |
| 112 | // Controls datagram transport support. |
| 113 | const char kDatagramTransportFieldTrial[] = "WebRTC-DatagramTransport"; |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 114 | // Controls datagram transport data channel support. |
| 115 | const char kDatagramTransportDataChannelFieldTrial[] = |
| 116 | "WebRTC-DatagramTransportDataChannels"; |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 117 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 118 | // UMA metric names. |
| 119 | const char kSimulcastVersionApplyLocalDescription[] = |
| 120 | "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription"; |
| 121 | const char kSimulcastVersionApplyRemoteDescription[] = |
| 122 | "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription"; |
| 123 | const char kSimulcastNumberOfEncodings[] = |
| 124 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
| 125 | const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled"; |
| 126 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 127 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 128 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 129 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 130 | |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 131 | // The length of RTCP CNAMEs. |
| 132 | static const int kRtcpCnameLength = 16; |
| 133 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 134 | enum { |
| 135 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 136 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 137 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 138 | MSG_GETSTATS, |
| 139 | MSG_FREE_DATACHANNELS, |
| 140 | MSG_REPORT_USAGE_PATTERN, |
| 141 | }; |
| 142 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 143 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 144 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 145 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 146 | explicit SetSessionDescriptionMsg( |
| 147 | webrtc::SetSessionDescriptionObserver* observer) |
| 148 | : observer(observer) {} |
| 149 | |
| 150 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 151 | RTCError error; |
| 152 | }; |
| 153 | |
| 154 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 155 | explicit CreateSessionDescriptionMsg( |
| 156 | webrtc::CreateSessionDescriptionObserver* observer) |
| 157 | : observer(observer) {} |
| 158 | |
| 159 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 160 | RTCError error; |
| 161 | }; |
| 162 | |
| 163 | struct GetStatsMsg : public rtc::MessageData { |
| 164 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 165 | webrtc::MediaStreamTrackInterface* track) |
| 166 | : observer(observer), track(track) {} |
| 167 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 168 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 169 | }; |
| 170 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 171 | // Check if we can send |new_stream| on a PeerConnection. |
| 172 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 173 | webrtc::MediaStreamInterface* new_stream) { |
| 174 | if (!new_stream || !current_streams) { |
| 175 | return false; |
| 176 | } |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 177 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 178 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 179 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
deadbeef | 5e97fb5 | 2015-10-15 19:49:08 | [diff] [blame] | 185 | // If the direction is "recvonly" or "inactive", treat the description |
| 186 | // as containing no streams. |
| 187 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 188 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 189 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 22:57:10 | [diff] [blame] | 190 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 19:49:08 | [diff] [blame] | 191 | ? desc->streams() |
| 192 | : std::vector<cricket::StreamParams>(); |
| 193 | } |
| 194 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 195 | bool IsValidOfferToReceiveMedia(int value) { |
| 196 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 197 | return (value >= Options::kUndefined) && |
| 198 | (value <= Options::kMaxOfferToReceiveMedia); |
| 199 | } |
| 200 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 201 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 202 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 203 | const std::vector<rtc::scoped_refptr< |
| 204 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 205 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 08:58:37 | [diff] [blame] | 206 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 207 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 208 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 209 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 210 | if (audio_media_description_options) { |
| 211 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 22:45:38 | [diff] [blame] | 212 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 213 | } |
| 214 | } else { |
| 215 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 216 | if (video_media_description_options) { |
| 217 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 218 | sender->id(), sender->internal()->stream_ids(), {}, |
| 219 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 220 | } |
| 221 | } |
zhihuang | a77e6bb | 2017-08-15 01:17:48 | [diff] [blame] | 222 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 223 | } |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 224 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 225 | // Add options to |session_options| from |rtp_data_channels|. |
| 226 | void AddRtpDataChannelOptions( |
| 227 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 228 | rtp_data_channels, |
| 229 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 230 | if (!data_media_description_options) { |
| 231 | return; |
| 232 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 233 | // Check for data channels. |
| 234 | for (const auto& kv : rtp_data_channels) { |
| 235 | const DataChannel* channel = kv.second; |
| 236 | if (channel->state() == DataChannel::kConnecting || |
| 237 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 238 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 239 | // the data channel's label. |
| 240 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 241 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 246 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 247 | PeerConnectionInterface::IceTransportsType type) { |
| 248 | switch (type) { |
| 249 | case PeerConnectionInterface::kNone: |
| 250 | return cricket::CF_NONE; |
| 251 | case PeerConnectionInterface::kRelay: |
| 252 | return cricket::CF_RELAY; |
| 253 | case PeerConnectionInterface::kNoHost: |
| 254 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 255 | case PeerConnectionInterface::kAll: |
| 256 | return cricket::CF_ALL; |
| 257 | default: |
nisse | c80e741 | 2017-01-11 13:56:46 | [diff] [blame] | 258 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 259 | } |
| 260 | return cricket::CF_NONE; |
| 261 | } |
| 262 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 263 | std::string GetSignalingStateString( |
| 264 | PeerConnectionInterface::SignalingState state) { |
| 265 | switch (state) { |
| 266 | case PeerConnectionInterface::kStable: |
| 267 | return "kStable"; |
| 268 | case PeerConnectionInterface::kHaveLocalOffer: |
| 269 | return "kHaveLocalOffer"; |
| 270 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 271 | return "kHavePrAnswer"; |
| 272 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 273 | return "kHaveRemoteOffer"; |
| 274 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 275 | return "kHaveRemotePrAnswer"; |
| 276 | case PeerConnectionInterface::kClosed: |
| 277 | return "kClosed"; |
| 278 | } |
| 279 | RTC_NOTREACHED(); |
| 280 | return ""; |
| 281 | } |
deadbeef | 0a6c4ca | 2015-10-06 18:38:28 | [diff] [blame] | 282 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 283 | IceCandidatePairType GetIceCandidatePairCounter( |
| 284 | const cricket::Candidate& local, |
| 285 | const cricket::Candidate& remote) { |
| 286 | const auto& l = local.type(); |
| 287 | const auto& r = remote.type(); |
| 288 | const auto& host = LOCAL_PORT_TYPE; |
| 289 | const auto& srflx = STUN_PORT_TYPE; |
| 290 | const auto& relay = RELAY_PORT_TYPE; |
| 291 | const auto& prflx = PRFLX_PORT_TYPE; |
| 292 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 293 | bool local_hostname = |
| 294 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 295 | bool remote_hostname = !remote.address().hostname().empty() && |
| 296 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 297 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 298 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 299 | if (local_hostname) { |
| 300 | if (remote_hostname) { |
| 301 | return kIceCandidatePairHostNameHostName; |
| 302 | } else if (remote_private) { |
| 303 | return kIceCandidatePairHostNameHostPrivate; |
| 304 | } else { |
| 305 | return kIceCandidatePairHostNameHostPublic; |
| 306 | } |
| 307 | } else if (local_private) { |
| 308 | if (remote_hostname) { |
| 309 | return kIceCandidatePairHostPrivateHostName; |
| 310 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 311 | return kIceCandidatePairHostPrivateHostPrivate; |
| 312 | } else { |
| 313 | return kIceCandidatePairHostPrivateHostPublic; |
| 314 | } |
| 315 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 316 | if (remote_hostname) { |
| 317 | return kIceCandidatePairHostPublicHostName; |
| 318 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 319 | return kIceCandidatePairHostPublicHostPrivate; |
| 320 | } else { |
| 321 | return kIceCandidatePairHostPublicHostPublic; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | if (l == host && r == srflx) |
| 326 | return kIceCandidatePairHostSrflx; |
| 327 | if (l == host && r == relay) |
| 328 | return kIceCandidatePairHostRelay; |
| 329 | if (l == host && r == prflx) |
| 330 | return kIceCandidatePairHostPrflx; |
| 331 | if (l == srflx && r == host) |
| 332 | return kIceCandidatePairSrflxHost; |
| 333 | if (l == srflx && r == srflx) |
| 334 | return kIceCandidatePairSrflxSrflx; |
| 335 | if (l == srflx && r == relay) |
| 336 | return kIceCandidatePairSrflxRelay; |
| 337 | if (l == srflx && r == prflx) |
| 338 | return kIceCandidatePairSrflxPrflx; |
| 339 | if (l == relay && r == host) |
| 340 | return kIceCandidatePairRelayHost; |
| 341 | if (l == relay && r == srflx) |
| 342 | return kIceCandidatePairRelaySrflx; |
| 343 | if (l == relay && r == relay) |
| 344 | return kIceCandidatePairRelayRelay; |
| 345 | if (l == relay && r == prflx) |
| 346 | return kIceCandidatePairRelayPrflx; |
| 347 | if (l == prflx && r == host) |
| 348 | return kIceCandidatePairPrflxHost; |
| 349 | if (l == prflx && r == srflx) |
| 350 | return kIceCandidatePairPrflxSrflx; |
| 351 | if (l == prflx && r == relay) |
| 352 | return kIceCandidatePairPrflxRelay; |
| 353 | return kIceCandidatePairMax; |
| 354 | } |
| 355 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 356 | // Logic to decide if an m= section can be recycled. This means that the new |
| 357 | // m= section is not rejected, but the old local or remote m= section is |
| 358 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 359 | // of the old remote and old local descriptions in no particular order. |
| 360 | // We need to check both the old local and remote because either |
| 361 | // could be the most current from the latest negotation. |
| 362 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 363 | const ContentInfo& content, |
| 364 | const ContentInfo* old_content_one, |
| 365 | const ContentInfo* old_content_two) { |
| 366 | return type == SdpType::kOffer && !content.rejected && |
| 367 | ((old_content_one && old_content_one->rejected) || |
| 368 | (old_content_two && old_content_two->rejected)); |
| 369 | } |
| 370 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 371 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 372 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 373 | // less than |current_desc|. In the case of checking an answer's |
| 374 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 375 | // local or remote. In the case of checking an offer's |new_desc| we |
| 376 | // check against the local and remote descriptions stored from the last |
| 377 | // negotiation, because either of these could be the most up to date for |
| 378 | // possible rejected m sections. These are the |current_desc| and |
| 379 | // |secondary_current_desc|. |
| 380 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 381 | const SessionDescription* secondary_current_desc, |
| 382 | const SessionDescription& new_desc, |
| 383 | const SdpType type) { |
| 384 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 388 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 389 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 390 | if (secondary_current_desc && |
| 391 | i < secondary_current_desc->contents().size()) { |
| 392 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 393 | } |
| 394 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 395 | ¤t_desc.contents()[i], |
| 396 | secondary_content_info)) { |
| 397 | // For new offer descriptions, if the media section can be recycled, it's |
| 398 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 399 | continue; |
| 400 | } |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 401 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 402 | return false; |
| 403 | } |
| 404 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 405 | new_desc.contents()[i].media_description(); |
| 406 | const MediaContentDescription* current_desc_mdesc = |
| 407 | current_desc.contents()[i].media_description(); |
| 408 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | return true; |
| 413 | } |
| 414 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 415 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 416 | const SessionDescription& desc2) { |
| 417 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 418 | } |
| 419 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 420 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 421 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 08:43:20 | [diff] [blame] | 422 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 423 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 424 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 425 | static constexpr struct { |
| 426 | KeyExchangeProtocolType protocol_type; |
| 427 | cricket::MediaType media_type; |
| 428 | KeyExchangeProtocolMedia protocol_media; |
| 429 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 430 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 431 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 432 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 433 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 434 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 435 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 436 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 437 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 438 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 439 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 440 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 441 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 442 | }; |
| 443 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 444 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 445 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 13:15:26 | [diff] [blame] | 446 | |
Mirko Bonadei | ab49982 | 2018-09-11 08:43:20 | [diff] [blame] | 447 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 448 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 449 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 450 | i.protocol_media, |
| 451 | kEnumCounterKeyProtocolMediaTypeMax); |
| 452 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 13:15:26 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 456 | void NoteAddIceCandidateResult(int result) { |
| 457 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 458 | kAddIceCandidateMax); |
| 459 | } |
| 460 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 461 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 462 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 463 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 464 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 465 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 466 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 467 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 468 | const cricket::ContentGroup* bundle = |
| 469 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 470 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 471 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 472 | continue; |
| 473 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 09:56:14 | [diff] [blame] | 474 | // Note what media is used with each crypto protocol, for all sections. |
| 475 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 476 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 477 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 478 | const std::string& mid = content_info.name; |
| 479 | if (bundle && bundle->HasContentName(mid) && |
| 480 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 481 | // This isn't the first media section in the BUNDLE group, so it's not |
| 482 | // required to have crypto attributes, since only the crypto attributes |
| 483 | // from the first section actually get used. |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | // If the content isn't rejected or bundled into another m= section, crypto |
| 488 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 489 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 490 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 491 | if (!media || !tinfo) { |
| 492 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 493 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 494 | } |
| 495 | if (dtls_enabled) { |
| 496 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 497 | RTC_LOG(LS_WARNING) |
| 498 | << "Session description must have DTLS fingerprint if " |
| 499 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 500 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 501 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 502 | } |
| 503 | } else { |
| 504 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 505 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 506 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 507 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 511 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 515 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 516 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 517 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 518 | const cricket::ContentGroup* bundle = |
| 519 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 520 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 521 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 522 | continue; |
| 523 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 524 | const std::string& mid = content_info.name; |
| 525 | if (bundle && bundle->HasContentName(mid) && |
| 526 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 527 | // This isn't the first media section in the BUNDLE group, so it's not |
| 528 | // required to have ufrag/password, since only the ufrag/password from |
| 529 | // the first section actually get used. |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | // If the content isn't rejected or bundled into another m= section, |
| 534 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 535 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 536 | if (!tinfo) { |
| 537 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 538 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | if (tinfo->description.ice_ufrag.empty() || |
| 542 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 543 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 550 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 551 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 552 | const SessionDescriptionInterface* new_desc, |
| 553 | const std::string& content_name) { |
| 554 | if (!old_desc) { |
| 555 | return false; |
| 556 | } |
| 557 | const SessionDescription* new_sd = new_desc->description(); |
| 558 | const SessionDescription* old_sd = old_desc->description(); |
| 559 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 560 | if (!cinfo || cinfo->rejected) { |
| 561 | return false; |
| 562 | } |
| 563 | // If the content isn't rejected, check if ufrag and password has changed. |
| 564 | const cricket::TransportDescription* new_transport_desc = |
| 565 | new_sd->GetTransportDescriptionByName(content_name); |
| 566 | const cricket::TransportDescription* old_transport_desc = |
| 567 | old_sd->GetTransportDescriptionByName(content_name); |
| 568 | if (!new_transport_desc || !old_transport_desc) { |
| 569 | // No transport description exists. This is not an ICE restart. |
| 570 | return false; |
| 571 | } |
| 572 | if (cricket::IceCredentialsChanged( |
| 573 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 574 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 575 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 576 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 577 | return true; |
| 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 582 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 583 | // from an RTCError. |
| 584 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 585 | SdpType type, |
| 586 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 587 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 588 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 589 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 590 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 591 | } |
| 592 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 593 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 594 | std::string output = "streams=["; |
| 595 | const char* separator = ""; |
| 596 | for (const auto& stream_id : stream_ids) { |
| 597 | output.append(separator).append(stream_id); |
| 598 | separator = ", "; |
| 599 | } |
| 600 | output.append("]"); |
| 601 | return output; |
| 602 | } |
| 603 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 604 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 605 | int rtc_configuration_parameter) { |
| 606 | if (rtc_configuration_parameter == |
| 607 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 608 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 609 | } |
| 610 | return rtc_configuration_parameter; |
| 611 | } |
| 612 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 613 | void ReportSimulcastApiVersion(const char* name, |
| 614 | const SessionDescription& session) { |
| 615 | bool has_legacy = false; |
| 616 | bool has_spec_compliant = false; |
| 617 | for (const ContentInfo& content : session.contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 618 | if (!content.media_description()) { |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 619 | continue; |
| 620 | } |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 621 | has_spec_compliant |= content.media_description()->HasSimulcast(); |
| 622 | for (const StreamParams& sp : content.media_description()->streams()) { |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 623 | has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | if (has_legacy) { |
| 628 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy, |
| 629 | kSimulcastApiVersionMax); |
| 630 | } |
| 631 | if (has_spec_compliant) { |
| 632 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant, |
| 633 | kSimulcastApiVersionMax); |
| 634 | } |
| 635 | if (!has_legacy && !has_spec_compliant) { |
| 636 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone, |
| 637 | kSimulcastApiVersionMax); |
| 638 | } |
| 639 | } |
| 640 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 641 | const ContentInfo* FindTransceiverMSection( |
| 642 | RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver, |
| 643 | const SessionDescriptionInterface* session_description) { |
| 644 | return transceiver->mid() |
| 645 | ? session_description->description()->GetContentByName( |
| 646 | *transceiver->mid()) |
| 647 | : nullptr; |
| 648 | } |
| 649 | |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 650 | // Wraps a CreateSessionDescriptionObserver and an OperationsChain operation |
| 651 | // complete callback. When the observer is invoked, the wrapped observer is |
| 652 | // invoked followed by invoking the completion callback. |
| 653 | class CreateSessionDescriptionObserverOperationWrapper |
| 654 | : public CreateSessionDescriptionObserver { |
| 655 | public: |
| 656 | CreateSessionDescriptionObserverOperationWrapper( |
| 657 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer, |
| 658 | std::function<void()> operation_complete_callback) |
| 659 | : observer_(std::move(observer)), |
| 660 | operation_complete_callback_(std::move(operation_complete_callback)) { |
| 661 | RTC_DCHECK(observer_); |
| 662 | } |
| 663 | ~CreateSessionDescriptionObserverOperationWrapper() override { |
| 664 | RTC_DCHECK(was_called_); |
| 665 | } |
| 666 | |
| 667 | void OnSuccess(SessionDescriptionInterface* desc) override { |
| 668 | RTC_DCHECK(!was_called_); |
| 669 | #ifdef RTC_DCHECK_IS_ON |
| 670 | was_called_ = true; |
| 671 | #endif // RTC_DCHECK_IS_ON |
| 672 | // Completing the operation before invoking the observer allows the observer |
| 673 | // to execute SetLocalDescription() without delay. |
| 674 | operation_complete_callback_(); |
| 675 | observer_->OnSuccess(desc); |
| 676 | } |
| 677 | |
| 678 | void OnFailure(RTCError error) override { |
| 679 | RTC_DCHECK(!was_called_); |
| 680 | #ifdef RTC_DCHECK_IS_ON |
| 681 | was_called_ = true; |
| 682 | #endif // RTC_DCHECK_IS_ON |
| 683 | operation_complete_callback_(); |
| 684 | observer_->OnFailure(std::move(error)); |
| 685 | } |
| 686 | |
| 687 | private: |
| 688 | #ifdef RTC_DCHECK_IS_ON |
| 689 | bool was_called_ = false; |
| 690 | #endif // RTC_DCHECK_IS_ON |
| 691 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_; |
| 692 | std::function<void()> operation_complete_callback_; |
| 693 | }; |
| 694 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 695 | } // namespace |
| 696 | |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 697 | // Used by parameterless SetLocalDescription() to create an offer or answer. |
| 698 | // Upon completion of creating the session description, SetLocalDescription() is |
| 699 | // invoked with the result. |
| 700 | // For consistency with DoSetLocalDescription(), if the PeerConnection is |
| 701 | // destroyed midst operation, we DO NOT inform the |
| 702 | // |set_local_description_observer| that the operation failed. |
| 703 | // TODO(hbos): If/when we process SLD messages in ~PeerConnection, the |
| 704 | // consistent thing would be to inform the observer here. |
| 705 | class PeerConnection::ImplicitCreateSessionDescriptionObserver |
| 706 | : public CreateSessionDescriptionObserver { |
| 707 | public: |
| 708 | ImplicitCreateSessionDescriptionObserver( |
| 709 | rtc::WeakPtr<PeerConnection> pc, |
| 710 | rtc::scoped_refptr<SetSessionDescriptionObserver> |
| 711 | set_local_description_observer) |
| 712 | : pc_(std::move(pc)), |
| 713 | set_local_description_observer_( |
| 714 | std::move(set_local_description_observer)) {} |
| 715 | ~ImplicitCreateSessionDescriptionObserver() override { |
| 716 | RTC_DCHECK(was_called_); |
| 717 | } |
| 718 | |
| 719 | void SetOperationCompleteCallback( |
| 720 | std::function<void()> operation_complete_callback) { |
| 721 | operation_complete_callback_ = std::move(operation_complete_callback); |
| 722 | } |
| 723 | |
| 724 | bool was_called() const { return was_called_; } |
| 725 | |
| 726 | void OnSuccess(SessionDescriptionInterface* desc_ptr) override { |
| 727 | RTC_DCHECK(!was_called_); |
| 728 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 729 | was_called_ = true; |
| 730 | |
| 731 | // Abort early if |pc_| is no longer valid. |
| 732 | if (!pc_) { |
| 733 | operation_complete_callback_(); |
| 734 | return; |
| 735 | } |
| 736 | // DoSetLocalDescription() is currently implemented as a synchronous |
| 737 | // operation but where the |set_local_description_observer_|'s callbacks are |
| 738 | // invoked asynchronously in a post to PeerConnection::OnMessage(). |
| 739 | pc_->DoSetLocalDescription(std::move(desc), |
| 740 | std::move(set_local_description_observer_)); |
| 741 | // For backwards-compatability reasons, we declare the operation as |
| 742 | // completed here (rather than in PeerConnection::OnMessage()). This ensures |
| 743 | // that subsequent offer/answer operations can start immediately (without |
| 744 | // waiting for OnMessage()). |
| 745 | operation_complete_callback_(); |
| 746 | } |
| 747 | |
| 748 | void OnFailure(RTCError error) override { |
| 749 | RTC_DCHECK(!was_called_); |
| 750 | was_called_ = true; |
| 751 | |
| 752 | // Abort early if |pc_| is no longer valid. |
| 753 | if (!pc_) { |
| 754 | operation_complete_callback_(); |
| 755 | return; |
| 756 | } |
| 757 | // DoSetLocalDescription() reports its failures in a post. We do the |
| 758 | // same thing here for consistency. |
| 759 | pc_->PostSetSessionDescriptionFailure( |
| 760 | set_local_description_observer_, |
| 761 | RTCError(error.type(), |
| 762 | std::string("SetLocalDescription failed to create " |
| 763 | "session description - ") + |
| 764 | error.message())); |
| 765 | operation_complete_callback_(); |
| 766 | } |
| 767 | |
| 768 | private: |
| 769 | bool was_called_ = false; |
| 770 | rtc::WeakPtr<PeerConnection> pc_; |
| 771 | rtc::scoped_refptr<SetSessionDescriptionObserver> |
| 772 | set_local_description_observer_; |
| 773 | std::function<void()> operation_complete_callback_; |
| 774 | }; |
| 775 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 776 | class PeerConnection::LocalIceCredentialsToReplace { |
| 777 | public: |
| 778 | // Sets the ICE credentials that need restarting to the ICE credentials of |
| 779 | // the current and pending descriptions. |
| 780 | void SetIceCredentialsFromLocalDescriptions( |
| 781 | const SessionDescriptionInterface* current_local_description, |
| 782 | const SessionDescriptionInterface* pending_local_description) { |
| 783 | ice_credentials_.clear(); |
| 784 | if (current_local_description) { |
| 785 | AppendIceCredentialsFromSessionDescription(*current_local_description); |
| 786 | } |
| 787 | if (pending_local_description) { |
| 788 | AppendIceCredentialsFromSessionDescription(*pending_local_description); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | void ClearIceCredentials() { ice_credentials_.clear(); } |
| 793 | |
| 794 | // Returns true if we have ICE credentials that need restarting. |
| 795 | bool HasIceCredentials() const { return !ice_credentials_.empty(); } |
| 796 | |
| 797 | // Returns true if |local_description| shares no ICE credentials with the |
| 798 | // ICE credentials that need restarting. |
| 799 | bool SatisfiesIceRestart( |
| 800 | const SessionDescriptionInterface& local_description) const { |
| 801 | for (const auto& transport_info : |
| 802 | local_description.description()->transport_infos()) { |
| 803 | if (ice_credentials_.find(std::make_pair( |
| 804 | transport_info.description.ice_ufrag, |
| 805 | transport_info.description.ice_pwd)) != ice_credentials_.end()) { |
| 806 | return false; |
| 807 | } |
| 808 | } |
| 809 | return true; |
| 810 | } |
| 811 | |
| 812 | private: |
| 813 | void AppendIceCredentialsFromSessionDescription( |
| 814 | const SessionDescriptionInterface& desc) { |
| 815 | for (const auto& transport_info : desc.description()->transport_infos()) { |
| 816 | ice_credentials_.insert( |
| 817 | std::make_pair(transport_info.description.ice_ufrag, |
| 818 | transport_info.description.ice_pwd)); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | std::set<std::pair<std::string, std::string>> ice_credentials_; |
| 823 | }; |
| 824 | |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 825 | // Upon completion, posts a task to execute the callback of the |
| 826 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 827 | // point, the state of the peer connection might no longer reflect the effects |
| 828 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 829 | // modified during the post. |
| 830 | // TODO(hbos): Remove this class once we remove the version of |
| 831 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 832 | // SetSessionDescriptionObserver as an argument. |
| 833 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 834 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 835 | public: |
| 836 | SetRemoteDescriptionObserverAdapter( |
| 837 | rtc::scoped_refptr<PeerConnection> pc, |
| 838 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 839 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 840 | |
| 841 | // SetRemoteDescriptionObserverInterface implementation. |
| 842 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 843 | if (error.ok()) |
| 844 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 845 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 846 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | private: |
| 850 | rtc::scoped_refptr<PeerConnection> pc_; |
| 851 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 852 | }; |
| 853 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 854 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 855 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 856 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 857 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 858 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 | [diff] [blame] | 859 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 860 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 861 | BundlePolicy bundle_policy; |
| 862 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 863 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 864 | int ice_candidate_pool_size; |
| 865 | bool disable_ipv6; |
| 866 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 867 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 868 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 869 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 870 | absl::optional<int> screencast_min_bitrate; |
| 871 | absl::optional<bool> combined_audio_video_bwe; |
| 872 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 873 | TcpCandidatePolicy tcp_candidate_policy; |
| 874 | CandidateNetworkPolicy candidate_network_policy; |
| 875 | int audio_jitter_buffer_max_packets; |
| 876 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 877 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 878 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 879 | int ice_connection_receiving_timeout; |
| 880 | int ice_backup_candidate_pair_ping_interval; |
| 881 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 882 | bool prioritize_most_likely_ice_candidate_pairs; |
| 883 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 884 | bool prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 885 | PortPrunePolicy turn_port_prune_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 886 | bool presume_writable_when_fully_relayed; |
| 887 | bool enable_ice_renomination; |
| 888 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 889 | bool surface_ice_candidates_on_ice_transport_type_changed; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 890 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 891 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 892 | absl::optional<int> ice_check_min_interval; |
| 893 | absl::optional<int> ice_unwritable_timeout; |
| 894 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 895 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 896 | absl::optional<int> stun_candidate_keepalive_interval; |
| 897 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 898 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 899 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 900 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 901 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 16:43:21 | [diff] [blame] | 902 | bool use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 903 | bool use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 904 | absl::optional<bool> use_datagram_transport; |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 905 | absl::optional<bool> use_datagram_transport_for_data_channels; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 906 | absl::optional<bool> use_datagram_transport_for_data_channels_receive_only; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 907 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 908 | bool offer_extmap_allow_mixed; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 909 | std::string turn_logging_id; |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 910 | bool enable_implicit_rollback; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 911 | absl::optional<bool> allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 912 | }; |
| 913 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 914 | "Did you add something to RTCConfiguration and forget to " |
| 915 | "update operator==?"); |
| 916 | return type == o.type && servers == o.servers && |
| 917 | bundle_policy == o.bundle_policy && |
| 918 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 919 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 920 | candidate_network_policy == o.candidate_network_policy && |
| 921 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 922 | audio_jitter_buffer_fast_accelerate == |
| 923 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 924 | audio_jitter_buffer_min_delay_ms == |
| 925 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 926 | audio_jitter_buffer_enable_rtx_handling == |
| 927 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 928 | ice_connection_receiving_timeout == |
| 929 | o.ice_connection_receiving_timeout && |
| 930 | ice_backup_candidate_pair_ping_interval == |
| 931 | o.ice_backup_candidate_pair_ping_interval && |
| 932 | continual_gathering_policy == o.continual_gathering_policy && |
| 933 | certificates == o.certificates && |
| 934 | prioritize_most_likely_ice_candidate_pairs == |
| 935 | o.prioritize_most_likely_ice_candidate_pairs && |
| 936 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 937 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 938 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 939 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 940 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 941 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 942 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 943 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 944 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 945 | prune_turn_ports == o.prune_turn_ports && |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 946 | turn_port_prune_policy == o.turn_port_prune_policy && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 947 | presume_writable_when_fully_relayed == |
| 948 | o.presume_writable_when_fully_relayed && |
| 949 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 19:50:14 | [diff] [blame] | 950 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 951 | surface_ice_candidates_on_ice_transport_type_changed == |
| 952 | o.surface_ice_candidates_on_ice_transport_type_changed && |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 953 | ice_check_interval_strong_connectivity == |
| 954 | o.ice_check_interval_strong_connectivity && |
| 955 | ice_check_interval_weak_connectivity == |
| 956 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 957 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 958 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 959 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 960 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 961 | stun_candidate_keepalive_interval == |
| 962 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 963 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 964 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 965 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 966 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 16:43:21 | [diff] [blame] | 967 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 968 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 969 | use_media_transport_for_data_channels == |
| 970 | o.use_media_transport_for_data_channels && |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 971 | use_datagram_transport == o.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 972 | use_datagram_transport_for_data_channels == |
| 973 | o.use_datagram_transport_for_data_channels && |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 974 | use_datagram_transport_for_data_channels_receive_only == |
| 975 | o.use_datagram_transport_for_data_channels_receive_only && |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 976 | crypto_options == o.crypto_options && |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 977 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed && |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 978 | turn_logging_id == o.turn_logging_id && |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 979 | enable_implicit_rollback == o.enable_implicit_rollback && |
| 980 | allow_codec_switching == o.allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 984 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 985 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 19:44:26 | [diff] [blame] | 986 | } |
| 987 | |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 988 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 989 | std::string GenerateRtcpCname() { |
| 990 | std::string cname; |
| 991 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 992 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 993 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 994 | } |
| 995 | return cname; |
| 996 | } |
| 997 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 998 | bool ValidateOfferAnswerOptions( |
| 999 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 1000 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 1001 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 1002 | } |
| 1003 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 1004 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 1005 | // m= sections (in other words, nothing that involves a map/array). |
| 1006 | void ExtractSharedMediaSessionOptions( |
| 1007 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 1008 | cricket::MediaSessionOptions* session_options) { |
| 1009 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 1010 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
Mirta Dvornicic | 479a3c0 | 2019-06-04 13:38:50 | [diff] [blame] | 1011 | session_options->raw_packetization_for_video = |
| 1012 | rtc_options.raw_packetization_for_video; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 1013 | } |
zhihuang | a77e6bb | 2017-08-15 01:17:48 | [diff] [blame] | 1014 | |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 1015 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 1016 | std::unique_ptr<RtcEventLog> event_log, |
| 1017 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1018 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 1019 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1020 | event_log_ptr_(event_log_.get()), |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1021 | operations_chain_(rtc::OperationsChain::Create()), |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 1022 | datagram_transport_config_( |
| 1023 | field_trial::FindFullName(kDatagramTransportFieldTrial)), |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 1024 | datagram_transport_data_channel_config_( |
| 1025 | field_trial::FindFullName(kDatagramTransportDataChannelFieldTrial)), |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 1026 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1027 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 1028 | remote_streams_(StreamCollection::Create()), |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1029 | call_(std::move(call)), |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1030 | call_ptr_(call_.get()), |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1031 | data_channel_transport_(nullptr), |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1032 | local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()), |
| 1033 | weak_ptr_factory_(this) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1034 | |
| 1035 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1036 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1037 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1038 | |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1039 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 1040 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1041 | // Need to stop transceivers before destroying the stats collector because |
| 1042 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 1043 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1044 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1045 | transceiver->Stop(); |
| 1046 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1047 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1048 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 13:06:57 | [diff] [blame] | 1049 | if (stats_collector_) { |
| 1050 | stats_collector_->WaitForPendingRequest(); |
| 1051 | stats_collector_ = nullptr; |
| 1052 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1053 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1054 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 1055 | // the last stats request can still read from the channels. |
| 1056 | DestroyAllChannels(); |
| 1057 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1058 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1059 | |
| 1060 | webrtc_session_desc_factory_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1061 | sctp_factory_.reset(); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 1062 | data_channel_transport_invoker_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1063 | transport_controller_.reset(); |
| 1064 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 1065 | // port_allocator_ lives on the network thread and should be destroyed there. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1066 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 1067 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1068 | port_allocator_.reset(); |
| 1069 | }); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1070 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 1071 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1072 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1073 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1074 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1075 | event_log_.reset(); |
| 1076 | }); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1077 | |
| 1078 | // Process all pending notifications in the message queue. If we don't do |
| 1079 | // this, requests will linger and not know they succeeded or failed. |
| 1080 | rtc::MessageList list; |
| 1081 | signaling_thread()->Clear(this, rtc::MQID_ANY, &list); |
| 1082 | for (auto& msg : list) { |
| 1083 | if (msg.message_id == MSG_CREATE_SESSIONDESCRIPTION_FAILED) { |
| 1084 | // Processing CreateOffer() and CreateAnswer() messages ensures their |
| 1085 | // observers are invoked even if the PeerConnection is destroyed early. |
| 1086 | OnMessage(&msg); |
| 1087 | } else { |
| 1088 | // TODO(hbos): Consider processing all pending messages. This would mean |
| 1089 | // that SetLocalDescription() and SetRemoteDescription() observers are |
| 1090 | // informed of successes and failures; this is currently NOT the case. |
| 1091 | delete msg.pdata; |
| 1092 | } |
| 1093 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1094 | } |
| 1095 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1096 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1097 | // Destroy video channels first since they may have a pointer to a voice |
| 1098 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1099 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 1100 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1101 | DestroyTransceiverChannel(transceiver); |
| 1102 | } |
| 1103 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1104 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 1105 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1106 | DestroyTransceiverChannel(transceiver); |
| 1107 | } |
| 1108 | } |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 1109 | DestroyDataChannelTransport(); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1110 | } |
| 1111 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1112 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1113 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1114 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1115 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1116 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1117 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1118 | |
| 1119 | RTCError config_error = ValidateConfiguration(configuration); |
| 1120 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1121 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
| 1124 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1125 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1126 | RTC_LOG(LS_ERROR) |
| 1127 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 1128 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1129 | return false; |
| 1130 | } |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 1131 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1132 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1133 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1134 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 1135 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 1136 | return false; |
| 1137 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 1138 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1139 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 20:20:15 | [diff] [blame] | 1140 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1141 | port_allocator_ = std::move(dependencies.allocator); |
Qingsi Wang | 25ec888 | 2019-11-15 20:33:05 | [diff] [blame] | 1142 | ice_transport_factory_ = std::move(dependencies.ice_transport_factory); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 1143 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 1144 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 1145 | cricket::ServerAddresses stun_servers; |
| 1146 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1147 | |
| 1148 | RTCErrorType parse_error = |
| 1149 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 1150 | if (parse_error != RTCErrorType::NONE) { |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 1154 | // Add the turn logging id to all turn servers |
| 1155 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 1156 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 1157 | } |
| 1158 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 1159 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1160 | // there. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1161 | const auto pa_result = |
| 1162 | network_thread()->Invoke<InitializePortAllocatorResult>( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 1163 | RTC_FROM_HERE, |
| 1164 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1165 | stun_servers, turn_servers, configuration)); |
| 1166 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 1167 | // If initialization was successful, note if STUN or TURN servers |
| 1168 | // were supplied. |
| 1169 | if (!stun_servers.empty()) { |
| 1170 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1171 | } |
| 1172 | if (!turn_servers.empty()) { |
| 1173 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1174 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1175 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 1176 | // Send information about IPv4/IPv6 status. |
| 1177 | PeerConnectionAddressFamilyCounter address_family; |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1178 | if (pa_result.enable_ipv6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 1179 | address_family = kPeerConnection_IPv6; |
| 1180 | } else { |
| 1181 | address_family = kPeerConnection_IPv4; |
| 1182 | } |
| 1183 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 1184 | kPeerConnectionAddressFamilyCounter_Max); |
| 1185 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1186 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 1187 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1188 | // RFC 3264: The numeric value of the session id and version in the |
| 1189 | // o line MUST be representable with a "64 bit signed integer". |
| 1190 | // Due to this constraint session id |session_id_| is max limited to |
| 1191 | // LLONG_MAX. |
| 1192 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1193 | JsepTransportController::Config config; |
| 1194 | config.redetermine_role_on_ice_restart = |
| 1195 | configuration.redetermine_role_on_ice_restart; |
| 1196 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 1197 | config.disable_encryption = options.disable_encryption; |
| 1198 | config.bundle_policy = configuration.bundle_policy; |
| 1199 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1200 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 1201 | // stub. |
| 1202 | config.crypto_options = configuration.crypto_options.has_value() |
| 1203 | ? *configuration.crypto_options |
| 1204 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 1205 | config.transport_observer = this; |
Sebastian Jansson | 1b83a9e | 2019-09-18 16:22:12 | [diff] [blame] | 1206 | // It's safe to pass |this| and using |rtcp_invoker_| and the |call_| pointer |
| 1207 | // since the JsepTransportController instance is owned by this PeerConnection |
| 1208 | // instance and is destroyed before both |rtcp_invoker_| and the |call_| |
| 1209 | // pointer. |
| 1210 | config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet, |
| 1211 | int64_t packet_time_us) { |
| 1212 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1213 | rtcp_invoker_.AsyncInvoke<void>( |
| 1214 | RTC_FROM_HERE, worker_thread(), [this, packet, packet_time_us] { |
| 1215 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1216 | // |call_| is reset on the worker thread in the PeerConnection |
| 1217 | // destructor, so we check that it's still valid before propagating |
| 1218 | // the packet. |
| 1219 | if (call_) { |
| 1220 | call_->Receiver()->DeliverPacket(MediaType::ANY, packet, |
| 1221 | packet_time_us); |
| 1222 | } |
| 1223 | }); |
| 1224 | }; |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1225 | config.event_log = event_log_ptr_; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1226 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 1227 | config.enable_external_auth = true; |
| 1228 | #endif |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1229 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1230 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 1231 | use_datagram_transport_ = datagram_transport_config_.enabled && |
Bjorn A Mellem | 238aab9 | 2019-07-02 18:06:00 | [diff] [blame] | 1232 | configuration.use_datagram_transport.value_or( |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 1233 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 1234 | use_datagram_transport_for_data_channels_ = |
| 1235 | datagram_transport_data_channel_config_.enabled && |
| 1236 | configuration.use_datagram_transport_for_data_channels.value_or( |
| 1237 | datagram_transport_data_channel_config_.default_value); |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 1238 | use_datagram_transport_for_data_channels_receive_only_ = |
| 1239 | configuration.use_datagram_transport_for_data_channels_receive_only |
| 1240 | .value_or(datagram_transport_data_channel_config_.receive_only); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 1241 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_ || |
| 1242 | configuration.use_media_transport || |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 1243 | configuration.use_media_transport_for_data_channels) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1244 | if (!factory_->media_transport_factory()) { |
| 1245 | RTC_DCHECK(false) |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 1246 | << "PeerConnecton is initialized with use_media_transport = true or " |
| 1247 | << "use_media_transport_for_data_channels = true " |
| 1248 | << "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 1252 | if (configuration.use_media_transport || |
| 1253 | configuration.use_media_transport_for_data_channels) { |
| 1254 | // TODO(bugs.webrtc.org/9719): This check will eventually go away, when |
| 1255 | // RTP media transport is introduced. But until then, we require SDES to |
| 1256 | // be enabled. |
| 1257 | if (configuration.enable_dtls_srtp.has_value() && |
| 1258 | configuration.enable_dtls_srtp.value()) { |
| 1259 | RTC_LOG(LS_WARNING) |
| 1260 | << "When media transport is used, SDES must be enabled. Set " |
| 1261 | "configuration.enable_dtls_srtp to false. use_media_transport=" |
| 1262 | << configuration.use_media_transport |
| 1263 | << ", use_media_transport_for_data_channels=" |
| 1264 | << configuration.use_media_transport_for_data_channels; |
| 1265 | return false; |
| 1266 | } |
| 1267 | } |
| 1268 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 21:31:15 | [diff] [blame] | 1269 | config.use_media_transport_for_media = configuration.use_media_transport; |
| 1270 | config.use_media_transport_for_data_channels = |
| 1271 | configuration.use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 1272 | config.use_datagram_transport = use_datagram_transport_; |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 1273 | config.use_datagram_transport_for_data_channels = |
| 1274 | use_datagram_transport_for_data_channels_; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 1275 | config.use_datagram_transport_for_data_channels_receive_only = |
| 1276 | use_datagram_transport_for_data_channels_receive_only_; |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1277 | config.media_transport_factory = factory_->media_transport_factory(); |
| 1278 | } |
| 1279 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1280 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1281 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1282 | if (!configuration.certificates.empty()) { |
| 1283 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1284 | // just picking the first one. The decision should be made based on the DTLS |
| 1285 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1286 | certificate = configuration.certificates[0]; |
| 1287 | } |
| 1288 | |
| 1289 | if (options.disable_encryption) { |
| 1290 | dtls_enabled_ = false; |
| 1291 | } else { |
| 1292 | // Enable DTLS by default if we have an identity store or a certificate. |
| 1293 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
| 1294 | // |configuration| can override the default |dtls_enabled_| value. |
| 1295 | if (configuration.enable_dtls_srtp) { |
| 1296 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
| 1301 | |
| 1302 | if (use_datagram_transport_for_data_channels_) { |
| 1303 | if (configuration.enable_rtp_data_channel) { |
| 1304 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1305 | "use_datagram_transport_for_data_channels are " |
| 1306 | "incompatible and cannot both be set to true"; |
| 1307 | return false; |
| 1308 | } |
| 1309 | if (configuration.enable_dtls_srtp && !*configuration.enable_dtls_srtp) { |
| 1310 | RTC_LOG(LS_INFO) << "Using data channel transport with no fallback"; |
| 1311 | data_channel_type_ = cricket::DCT_DATA_CHANNEL_TRANSPORT; |
| 1312 | } else { |
| 1313 | RTC_LOG(LS_INFO) << "Using data channel transport with fallback to SCTP"; |
| 1314 | data_channel_type_ = cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP; |
| 1315 | config.sctp_factory = sctp_factory_.get(); |
| 1316 | } |
| 1317 | } else if (configuration.use_media_transport_for_data_channels) { |
| 1318 | if (configuration.enable_rtp_data_channel) { |
| 1319 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1320 | "use_media_transport_for_data_channels are " |
| 1321 | "incompatible and cannot both be set to true"; |
| 1322 | return false; |
| 1323 | } |
| 1324 | data_channel_type_ = cricket::DCT_MEDIA_TRANSPORT; |
| 1325 | } else if (configuration.enable_rtp_data_channel) { |
| 1326 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1327 | // set. It takes precendence over the disable_sctp_data_channels |
| 1328 | // PeerConnectionFactoryInterface::Options. |
| 1329 | data_channel_type_ = cricket::DCT_RTP; |
| 1330 | } else { |
| 1331 | // DTLS has to be enabled to use SCTP. |
| 1332 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1333 | data_channel_type_ = cricket::DCT_SCTP; |
| 1334 | config.sctp_factory = sctp_factory_.get(); |
| 1335 | } |
| 1336 | } |
| 1337 | |
Qingsi Wang | 25ec888 | 2019-11-15 20:33:05 | [diff] [blame] | 1338 | config.ice_transport_factory = ice_transport_factory_.get(); |
| 1339 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1340 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 20:20:15 | [diff] [blame] | 1341 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1342 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1343 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1344 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1345 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1346 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1347 | transport_controller_->SignalConnectionState.connect( |
| 1348 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1349 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1350 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1351 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1352 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 1353 | transport_controller_->SignalIceCandidateError.connect( |
| 1354 | this, &PeerConnection::OnTransportControllerCandidateError); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1355 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1356 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1357 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1358 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 1359 | transport_controller_->SignalIceCandidatePairChanged.connect( |
| 1360 | this, &PeerConnection::OnTransportControllerCandidateChanged); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1361 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1362 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1363 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1364 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1365 | configuration_ = configuration; |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1366 | use_media_transport_ = configuration.use_media_transport; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1367 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 1368 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1369 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1370 | video_options_.screencast_min_bitrate_kbps = |
| 1371 | configuration.screencast_min_bitrate; |
| 1372 | audio_options_.combined_audio_video_bwe = |
| 1373 | configuration.combined_audio_video_bwe; |
| 1374 | |
| 1375 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 1376 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1377 | |
| 1378 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 1379 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1380 | |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 1381 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1382 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1383 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 1384 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1385 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1386 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1387 | // Whether the certificate generator/certificate is null or not determines |
| 1388 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1389 | // the right instructions by clearing the variables if needed. |
| 1390 | if (!dtls_enabled_) { |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1391 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1392 | certificate = nullptr; |
| 1393 | } else if (certificate) { |
| 1394 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1395 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1399 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 1400 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1401 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1402 | this, &PeerConnection::OnCertificateReady); |
| 1403 | |
| 1404 | if (options.disable_encryption) { |
| 1405 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1406 | } |
| 1407 | |
| 1408 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1409 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-11 00:08:05 | [diff] [blame] | 1410 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1411 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1412 | // Add default audio/video transceivers for Plan B SDP. |
| 1413 | if (!IsUnifiedPlan()) { |
| 1414 | transceivers_.push_back( |
| 1415 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1416 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1417 | transceivers_.push_back( |
| 1418 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1419 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1420 | } |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 1421 | int delay_ms = |
| 1422 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 1423 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1424 | MSG_REPORT_USAGE_PATTERN, nullptr); |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 1425 | |
| 1426 | if (dependencies.video_bitrate_allocator_factory) { |
| 1427 | video_bitrate_allocator_factory_ = |
| 1428 | std::move(dependencies.video_bitrate_allocator_factory); |
| 1429 | } else { |
| 1430 | video_bitrate_allocator_factory_ = |
| 1431 | CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1432 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1433 | return true; |
| 1434 | } |
| 1435 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1436 | RTCError PeerConnection::ValidateConfiguration( |
| 1437 | const RTCConfiguration& config) const { |
| 1438 | if (config.ice_regather_interval_range && |
| 1439 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1440 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1441 | "ice_regather_interval_range specified but continual " |
| 1442 | "gathering policy is GATHER_ONCE"); |
| 1443 | } |
Qingsi Wang | dea6889 | 2018-03-27 17:55:21 | [diff] [blame] | 1444 | auto result = |
| 1445 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1446 | return result; |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1447 | } |
| 1448 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1449 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1450 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1451 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1452 | "Plan SdpSemantics. Please use GetSenders " |
| 1453 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1454 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1455 | } |
| 1456 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1457 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1458 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1459 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1460 | "Plan SdpSemantics. Please use GetReceivers " |
| 1461 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1462 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1463 | } |
| 1464 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 | [diff] [blame] | 1465 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1466 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1467 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1468 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1469 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1470 | if (IsClosed()) { |
| 1471 | return false; |
| 1472 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1473 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1474 | return false; |
| 1475 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1476 | |
| 1477 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1478 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1479 | observer->SignalAudioTrackAdded.connect(this, |
| 1480 | &PeerConnection::OnAudioTrackAdded); |
| 1481 | observer->SignalAudioTrackRemoved.connect( |
| 1482 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1483 | observer->SignalVideoTrackAdded.connect(this, |
| 1484 | &PeerConnection::OnVideoTrackAdded); |
| 1485 | observer->SignalVideoTrackRemoved.connect( |
| 1486 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1487 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1488 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1489 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1490 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1491 | } |
| 1492 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1493 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1494 | } |
| 1495 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 1496 | stats_->AddStream(local_stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1497 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1498 | return true; |
| 1499 | } |
| 1500 | |
| 1501 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1502 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1503 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1504 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1505 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1506 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1507 | if (!IsClosed()) { |
| 1508 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1509 | RemoveAudioTrack(track.get(), local_stream); |
| 1510 | } |
| 1511 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1512 | RemoveVideoTrack(track.get(), local_stream); |
| 1513 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1514 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1515 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1516 | stream_observers_.erase( |
| 1517 | std::remove_if( |
| 1518 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1519 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 1520 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1521 | }), |
| 1522 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1523 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1524 | if (IsClosed()) { |
| 1525 | return; |
| 1526 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1527 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1528 | } |
| 1529 | |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 1530 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1531 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1532 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1533 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 1534 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1535 | if (!track) { |
| 1536 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1537 | } |
| 1538 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1539 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1540 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1541 | "Track has invalid kind: " + track->kind()); |
| 1542 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1543 | if (IsClosed()) { |
| 1544 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1545 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1546 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1547 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1548 | LOG_AND_RETURN_ERROR( |
| 1549 | RTCErrorType::INVALID_PARAMETER, |
| 1550 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1551 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1552 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1553 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1554 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1555 | if (sender_or_error.ok()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1556 | UpdateNegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 23:48:17 | [diff] [blame] | 1557 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1558 | } |
| 1559 | return sender_or_error; |
| 1560 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1561 | |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1562 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1563 | PeerConnection::AddTrackPlanB( |
| 1564 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1565 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1566 | if (stream_ids.size() > 1u) { |
| 1567 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1568 | "AddTrack with more than one stream is not " |
| 1569 | "supported with Plan B semantics."); |
| 1570 | } |
| 1571 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1572 | if (adjusted_stream_ids.empty()) { |
| 1573 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1574 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1575 | cricket::MediaType media_type = |
| 1576 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1577 | ? cricket::MEDIA_TYPE_AUDIO |
| 1578 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1579 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1580 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1581 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1582 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1583 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1584 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 1585 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1586 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1587 | if (sender_info) { |
| 1588 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1589 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1590 | } else { |
| 1591 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1592 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1593 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1594 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 1595 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1596 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1597 | if (sender_info) { |
| 1598 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1599 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1600 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1601 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1602 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1603 | |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1604 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1605 | PeerConnection::AddTrackUnifiedPlan( |
| 1606 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1607 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1608 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1609 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1610 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1611 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1612 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1613 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1614 | transceiver->internal()->set_direction( |
| 1615 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1616 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1617 | transceiver->internal()->set_direction( |
| 1618 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1619 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1620 | transceiver->sender()->SetTrack(track); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1621 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1622 | } else { |
| 1623 | cricket::MediaType media_type = |
| 1624 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1625 | ? cricket::MEDIA_TYPE_AUDIO |
| 1626 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1627 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1628 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1629 | std::string sender_id = track->id(); |
| 1630 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1631 | // This can happen if this is the second time AddTrack has created a sender |
| 1632 | // for this track. |
| 1633 | if (FindSenderById(sender_id)) { |
| 1634 | sender_id = rtc::CreateRandomUuid(); |
| 1635 | } |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1636 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1637 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1638 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1639 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1640 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1641 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1642 | return transceiver->sender(); |
| 1643 | } |
| 1644 | |
| 1645 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1646 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1647 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1648 | RTC_DCHECK(track); |
| 1649 | for (auto transceiver : transceivers_) { |
| 1650 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 1651 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1652 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 1653 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1654 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1655 | return transceiver; |
| 1656 | } |
| 1657 | } |
| 1658 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1662 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 17:27:33 | [diff] [blame] | 1663 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1664 | } |
| 1665 | |
Steve Anton | 24db573 | 2018-07-23 17:27:33 | [diff] [blame] | 1666 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1667 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1668 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1669 | if (!sender) { |
| 1670 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1671 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1672 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1673 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1674 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1675 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1676 | if (IsUnifiedPlan()) { |
| 1677 | auto transceiver = FindTransceiverBySender(sender); |
| 1678 | if (!transceiver || !sender->track()) { |
| 1679 | return RTCError::OK(); |
| 1680 | } |
| 1681 | sender->SetTrack(nullptr); |
| 1682 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1683 | transceiver->internal()->set_direction( |
| 1684 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1685 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1686 | transceiver->internal()->set_direction( |
| 1687 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1688 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1689 | } else { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1690 | bool removed; |
| 1691 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1692 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1693 | } else { |
| 1694 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1695 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1696 | } |
| 1697 | if (!removed) { |
| 1698 | LOG_AND_RETURN_ERROR( |
| 1699 | RTCErrorType::INVALID_PARAMETER, |
| 1700 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1701 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1702 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1703 | UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1704 | return RTCError::OK(); |
| 1705 | } |
| 1706 | |
| 1707 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1708 | PeerConnection::FindTransceiverBySender( |
| 1709 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1710 | for (auto transceiver : transceivers_) { |
| 1711 | if (transceiver->sender() == sender) { |
| 1712 | return transceiver; |
| 1713 | } |
| 1714 | } |
| 1715 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1716 | } |
| 1717 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1718 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1719 | PeerConnection::AddTransceiver( |
| 1720 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1721 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1722 | } |
| 1723 | |
| 1724 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1725 | PeerConnection::AddTransceiver( |
| 1726 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1727 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1728 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1729 | RTC_CHECK(IsUnifiedPlan()) |
| 1730 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1731 | if (!track) { |
| 1732 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1733 | } |
| 1734 | cricket::MediaType media_type; |
| 1735 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1736 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1737 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1738 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1739 | } else { |
| 1740 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1741 | "Track kind is not audio or video"); |
| 1742 | } |
| 1743 | return AddTransceiver(media_type, track, init); |
| 1744 | } |
| 1745 | |
| 1746 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1747 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1748 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1749 | } |
| 1750 | |
| 1751 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1752 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1753 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1754 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1755 | RTC_CHECK(IsUnifiedPlan()) |
| 1756 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1757 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1758 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1759 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1760 | "media type is not audio or video"); |
| 1761 | } |
| 1762 | return AddTransceiver(media_type, nullptr, init); |
| 1763 | } |
| 1764 | |
| 1765 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1766 | PeerConnection::AddTransceiver( |
| 1767 | cricket::MediaType media_type, |
| 1768 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1769 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1770 | bool update_negotiation_needed) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1771 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1772 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1773 | if (track) { |
| 1774 | RTC_DCHECK_EQ(media_type, |
| 1775 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1776 | ? cricket::MEDIA_TYPE_AUDIO |
| 1777 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1778 | } |
| 1779 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 1780 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1781 | init.send_encodings.size(), 0, 7, 8); |
| 1782 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1783 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1784 | [](const RtpEncodingParameters& encoding) { |
| 1785 | return !encoding.rid.empty(); |
| 1786 | }); |
| 1787 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-07 01:09:52 | [diff] [blame] | 1788 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1789 | RTCErrorType::INVALID_PARAMETER, |
| 1790 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 | [diff] [blame] | 1791 | } |
| 1792 | |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 1793 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1794 | [](const RtpEncodingParameters& encoding) { |
| 1795 | return !IsLegalRsidName(encoding.rid); |
| 1796 | })) { |
| 1797 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1798 | "Invalid RID value provided."); |
| 1799 | } |
| 1800 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1801 | if (absl::c_any_of(init.send_encodings, |
| 1802 | [](const RtpEncodingParameters& encoding) { |
| 1803 | return encoding.ssrc.has_value(); |
| 1804 | })) { |
| 1805 | LOG_AND_RETURN_ERROR( |
| 1806 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1807 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | RtpParameters parameters; |
| 1811 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1812 | |
| 1813 | // Encodings are dropped from the tail if too many are provided. |
| 1814 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1815 | parameters.encodings.erase( |
| 1816 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1817 | parameters.encodings.end()); |
| 1818 | } |
| 1819 | |
| 1820 | // Single RID should be removed. |
| 1821 | if (parameters.encodings.size() == 1 && |
| 1822 | !parameters.encodings[0].rid.empty()) { |
| 1823 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1824 | parameters.encodings[0].rid.clear(); |
| 1825 | } |
| 1826 | |
| 1827 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1828 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1829 | rtc::UniqueStringGenerator rid_generator; |
| 1830 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1831 | encoding.rid = rid_generator(); |
| 1832 | } |
| 1833 | } |
| 1834 | |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1835 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1836 | LOG_AND_RETURN_ERROR( |
| 1837 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1838 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1839 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1840 | |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1841 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1842 | if (!result.ok()) { |
| 1843 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1844 | } |
| 1845 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1846 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1847 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1848 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1849 | // that sender ID is already in use. |
| 1850 | std::string sender_id = |
| 1851 | (track && !FindSenderById(track->id()) ? track->id() |
| 1852 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1853 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1854 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1855 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1856 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1857 | transceiver->internal()->set_direction(init.direction); |
| 1858 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1859 | if (update_negotiation_needed) { |
| 1860 | UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1861 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1862 | |
| 1863 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1864 | } |
| 1865 | |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1866 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1867 | PeerConnection::CreateSender( |
| 1868 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1869 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1870 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1871 | const std::vector<std::string>& stream_ids, |
| 1872 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1873 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1874 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1875 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1876 | RTC_DCHECK(!track || |
| 1877 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1878 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1879 | signaling_thread(), |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1880 | AudioRtpSender::Create(worker_thread(), id, stats_.get(), this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1881 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1882 | } else { |
| 1883 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1884 | RTC_DCHECK(!track || |
| 1885 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1886 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1887 | signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1888 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1889 | } |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1890 | bool set_track_succeeded = sender->SetTrack(track); |
| 1891 | RTC_DCHECK(set_track_succeeded); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1892 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1893 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1894 | return sender; |
| 1895 | } |
| 1896 | |
| 1897 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1898 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1899 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1900 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1901 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1902 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1903 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 1904 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1905 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1906 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1907 | } else { |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1908 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1909 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 1910 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1911 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1912 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1913 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1914 | return receiver; |
| 1915 | } |
| 1916 | |
| 1917 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1918 | PeerConnection::CreateAndAddTransceiver( |
| 1919 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1920 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1921 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1922 | // Ensure that the new sender does not have an ID that is already in use by |
| 1923 | // another sender. |
| 1924 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1925 | // could be invalid, but should not cause a crash). |
| 1926 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1927 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 1928 | signaling_thread(), |
| 1929 | new RtpTransceiver(sender, receiver, channel_manager())); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1930 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1931 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1932 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1933 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1934 | } |
| 1935 | |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1936 | void PeerConnection::OnNegotiationNeeded() { |
| 1937 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1938 | RTC_DCHECK(!IsClosed()); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1939 | UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1940 | } |
| 1941 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1942 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-19 00:58:44 | [diff] [blame] | 1943 | const std::string& kind, |
| 1944 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1945 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1946 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1947 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1948 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1949 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1950 | if (IsClosed()) { |
| 1951 | return nullptr; |
| 1952 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1953 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1954 | // Internally we need to have one stream with Plan B semantics, so we |
| 1955 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1956 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1957 | if (stream_id.empty()) { |
| 1958 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1959 | RTC_LOG(LS_INFO) |
| 1960 | << "No stream_id specified for sender. Generated stream ID: " |
| 1961 | << stream_ids[0]; |
| 1962 | } else { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1963 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1964 | } |
| 1965 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1966 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1967 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1968 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1969 | auto audio_sender = AudioRtpSender::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1970 | worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1971 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1972 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1973 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1974 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1975 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1976 | auto video_sender = |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1977 | VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1978 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1979 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1980 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1981 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1982 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1983 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1984 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1985 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1986 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1987 | |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1988 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1989 | } |
| 1990 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1991 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1992 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1993 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1994 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1995 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1996 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1997 | } |
| 1998 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1999 | } |
| 2000 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 2001 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 2002 | PeerConnection::GetSendersInternal() const { |
| 2003 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 2004 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2005 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 2006 | auto senders = transceiver->internal()->senders(); |
| 2007 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 2008 | } |
| 2009 | return all_senders; |
| 2010 | } |
| 2011 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 2012 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 2013 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 2014 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 2015 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 2016 | for (const auto& receiver : GetReceiversInternal()) { |
| 2017 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 2018 | } |
| 2019 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 2020 | } |
| 2021 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 2022 | std::vector< |
| 2023 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 2024 | PeerConnection::GetReceiversInternal() const { |
| 2025 | std::vector< |
| 2026 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 2027 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2028 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 2029 | auto receivers = transceiver->internal()->receivers(); |
| 2030 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 2031 | receivers.end()); |
| 2032 | } |
| 2033 | return all_receivers; |
| 2034 | } |
| 2035 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 2036 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2037 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2038 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 2039 | RTC_CHECK(IsUnifiedPlan()) |
| 2040 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 2041 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2042 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 2043 | all_transceivers.push_back(transceiver); |
| 2044 | } |
| 2045 | return all_transceivers; |
| 2046 | } |
| 2047 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2048 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 | [diff] [blame] | 2049 | MediaStreamTrackInterface* track, |
| 2050 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 2051 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2052 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2053 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2054 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2055 | return false; |
| 2056 | } |
| 2057 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 2058 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 2059 | // The StatsCollector is used to tell if a track is valid because it may |
| 2060 | // remember tracks that the PeerConnection previously removed. |
| 2061 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2062 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 2063 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 2064 | return false; |
| 2065 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 2066 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 2067 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2068 | return true; |
| 2069 | } |
| 2070 | |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 2071 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2072 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2073 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 2074 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2075 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 2076 | stats_collector_->GetStatsReport(callback); |
| 2077 | } |
| 2078 | |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2079 | void PeerConnection::GetStats( |
| 2080 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 2081 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 2082 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2083 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2084 | RTC_DCHECK(callback); |
| 2085 | RTC_DCHECK(stats_collector_); |
| 2086 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 2087 | if (selector) { |
| 2088 | for (const auto& proxy_transceiver : transceivers_) { |
| 2089 | for (const auto& proxy_sender : |
| 2090 | proxy_transceiver->internal()->senders()) { |
| 2091 | if (proxy_sender == selector) { |
| 2092 | internal_sender = proxy_sender->internal(); |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | if (internal_sender) |
| 2097 | break; |
| 2098 | } |
| 2099 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 2100 | // If there is no |internal_sender| then |selector| is either null or does not |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2101 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 2102 | // PeerConnection). This means that "all the stats objects representing the |
| 2103 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 2104 | // produces an empty stats report. |
| 2105 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 2106 | } |
| 2107 | |
| 2108 | void PeerConnection::GetStats( |
| 2109 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 2110 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 2111 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2112 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2113 | RTC_DCHECK(callback); |
| 2114 | RTC_DCHECK(stats_collector_); |
| 2115 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 2116 | if (selector) { |
| 2117 | for (const auto& proxy_transceiver : transceivers_) { |
| 2118 | for (const auto& proxy_receiver : |
| 2119 | proxy_transceiver->internal()->receivers()) { |
| 2120 | if (proxy_receiver == selector) { |
| 2121 | internal_receiver = proxy_receiver->internal(); |
| 2122 | break; |
| 2123 | } |
| 2124 | } |
| 2125 | if (internal_receiver) |
| 2126 | break; |
| 2127 | } |
| 2128 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 2129 | // If there is no |internal_receiver| then |selector| is either null or does |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2130 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 2131 | // the PeerConnection). This means that "all the stats objects representing |
| 2132 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 2133 | // selector produces an empty stats report. |
| 2134 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 2135 | } |
| 2136 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2137 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2138 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2139 | return signaling_state_; |
| 2140 | } |
| 2141 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2142 | PeerConnectionInterface::IceConnectionState |
| 2143 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2144 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2145 | return ice_connection_state_; |
| 2146 | } |
| 2147 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2148 | PeerConnectionInterface::IceConnectionState |
| 2149 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2150 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2151 | return standardized_ice_connection_state_; |
| 2152 | } |
| 2153 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 2154 | PeerConnectionInterface::PeerConnectionState |
| 2155 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2156 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 2157 | return connection_state_; |
| 2158 | } |
| 2159 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2160 | PeerConnectionInterface::IceGatheringState |
| 2161 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2162 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2163 | return ice_gathering_state_; |
| 2164 | } |
| 2165 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 2166 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2167 | const std::string& label, |
| 2168 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 09:17:47 | [diff] [blame] | 2169 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 2170 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 18:14:50 | [diff] [blame] | 2171 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2172 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 2173 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 2174 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 2175 | if (config) { |
| 2176 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 2177 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 2178 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2179 | InternalCreateDataChannel(label, internal_config.get())); |
| 2180 | if (!channel.get()) { |
| 2181 | return nullptr; |
| 2182 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2183 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 2184 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 2185 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2186 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2187 | UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 2188 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2189 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2190 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 2191 | } |
| 2192 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 2193 | void PeerConnection::RestartIce() { |
| 2194 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2195 | local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions( |
| 2196 | current_local_description_.get(), pending_local_description_.get()); |
| 2197 | UpdateNegotiationNeeded(); |
| 2198 | } |
| 2199 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2200 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 2201 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2202 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2203 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2204 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2205 | // lambda will execute immediately. |
| 2206 | operations_chain_->ChainOperation( |
| 2207 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2208 | observer_refptr = |
| 2209 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 2210 | options](std::function<void()> operations_chain_callback) { |
| 2211 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2212 | if (!this_weak_ptr) { |
| 2213 | observer_refptr->OnFailure( |
| 2214 | RTCError(RTCErrorType::INTERNAL_ERROR, |
| 2215 | "CreateOffer failed because the session was shut down")); |
| 2216 | operations_chain_callback(); |
| 2217 | return; |
| 2218 | } |
| 2219 | // The operation completes asynchronously when the wrapper is invoked. |
| 2220 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 2221 | observer_wrapper(new rtc::RefCountedObject< |
| 2222 | CreateSessionDescriptionObserverOperationWrapper>( |
| 2223 | std::move(observer_refptr), |
| 2224 | std::move(operations_chain_callback))); |
| 2225 | this_weak_ptr->DoCreateOffer(options, observer_wrapper); |
| 2226 | }); |
| 2227 | } |
| 2228 | |
| 2229 | void PeerConnection::DoCreateOffer( |
| 2230 | const RTCOfferAnswerOptions& options, |
| 2231 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 2232 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2233 | TRACE_EVENT0("webrtc", "PeerConnection::DoCreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2234 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2235 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2236 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 2237 | return; |
| 2238 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2239 | |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2240 | if (IsClosed()) { |
| 2241 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2242 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2243 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 2244 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2245 | return; |
| 2246 | } |
| 2247 | |
Steve Anton | 25ca0ac | 2019-06-25 17:40:48 | [diff] [blame] | 2248 | // If a session error has occurred the PeerConnection is in a possibly |
| 2249 | // inconsistent state so fail right away. |
| 2250 | if (session_error() != SessionError::kNone) { |
| 2251 | std::string error_message = GetSessionErrorMsg(); |
| 2252 | RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message; |
| 2253 | PostCreateSessionDescriptionFailure( |
| 2254 | observer, |
| 2255 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2256 | return; |
| 2257 | } |
| 2258 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2259 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2260 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2261 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2262 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 2263 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2264 | return; |
| 2265 | } |
| 2266 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2267 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 2268 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 2269 | if (IsUnifiedPlan()) { |
| 2270 | RTCError error = HandleLegacyOfferOptions(options); |
| 2271 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2272 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2273 | return; |
| 2274 | } |
| 2275 | } |
| 2276 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2277 | cricket::MediaSessionOptions session_options; |
| 2278 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 2279 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2280 | } |
| 2281 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2282 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 2283 | const RTCOfferAnswerOptions& options) { |
| 2284 | RTC_DCHECK(IsUnifiedPlan()); |
| 2285 | |
| 2286 | if (options.offer_to_receive_audio == 0) { |
| 2287 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2288 | cricket::MEDIA_TYPE_AUDIO); |
| 2289 | } else if (options.offer_to_receive_audio == 1) { |
| 2290 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 2291 | } else if (options.offer_to_receive_audio > 1) { |
| 2292 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2293 | "offer_to_receive_audio > 1 is not supported."); |
| 2294 | } |
| 2295 | |
| 2296 | if (options.offer_to_receive_video == 0) { |
| 2297 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2298 | cricket::MEDIA_TYPE_VIDEO); |
| 2299 | } else if (options.offer_to_receive_video == 1) { |
| 2300 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 2301 | } else if (options.offer_to_receive_video > 1) { |
| 2302 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2303 | "offer_to_receive_video > 1 is not supported."); |
| 2304 | } |
| 2305 | |
| 2306 | return RTCError::OK(); |
| 2307 | } |
| 2308 | |
| 2309 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2310 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2311 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 2312 | RtpTransceiverDirection new_direction = |
| 2313 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 2314 | if (new_direction != transceiver->direction()) { |
| 2315 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 2316 | << " transceiver (MID=" |
| 2317 | << transceiver->mid().value_or("<not set>") << ") from " |
| 2318 | << RtpTransceiverDirectionToString( |
| 2319 | transceiver->direction()) |
| 2320 | << " to " |
| 2321 | << RtpTransceiverDirectionToString(new_direction) |
| 2322 | << " since CreateOffer specified offer_to_receive=0"; |
| 2323 | transceiver->internal()->set_direction(new_direction); |
| 2324 | } |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 2329 | cricket::MediaType media_type) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 2330 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2331 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 2332 | RTC_LOG(LS_INFO) |
| 2333 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 2334 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2335 | RtpTransceiverInit init; |
| 2336 | init.direction = RtpTransceiverDirection::kRecvOnly; |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2337 | AddTransceiver(media_type, nullptr, init, |
| 2338 | /*update_negotiation_needed=*/false); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2343 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 2344 | std::vector< |
| 2345 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2346 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2347 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 2348 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2349 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 2350 | receiving_transceivers.push_back(transceiver); |
| 2351 | } |
| 2352 | } |
| 2353 | return receiving_transceivers; |
| 2354 | } |
| 2355 | |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2356 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 2357 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2358 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2359 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2360 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2361 | // lambda will execute immediately. |
| 2362 | operations_chain_->ChainOperation( |
| 2363 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2364 | observer_refptr = |
| 2365 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 2366 | options](std::function<void()> operations_chain_callback) { |
| 2367 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2368 | if (!this_weak_ptr) { |
| 2369 | observer_refptr->OnFailure(RTCError( |
| 2370 | RTCErrorType::INTERNAL_ERROR, |
| 2371 | "CreateAnswer failed because the session was shut down")); |
| 2372 | operations_chain_callback(); |
| 2373 | return; |
| 2374 | } |
| 2375 | // The operation completes asynchronously when the wrapper is invoked. |
| 2376 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 2377 | observer_wrapper(new rtc::RefCountedObject< |
| 2378 | CreateSessionDescriptionObserverOperationWrapper>( |
| 2379 | std::move(observer_refptr), |
| 2380 | std::move(operations_chain_callback))); |
| 2381 | this_weak_ptr->DoCreateAnswer(options, observer_wrapper); |
| 2382 | }); |
| 2383 | } |
| 2384 | |
| 2385 | void PeerConnection::DoCreateAnswer( |
| 2386 | const RTCOfferAnswerOptions& options, |
| 2387 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 2388 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2389 | TRACE_EVENT0("webrtc", "PeerConnection::DoCreateAnswer"); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2390 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2391 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2392 | return; |
| 2393 | } |
| 2394 | |
Steve Anton | 25ca0ac | 2019-06-25 17:40:48 | [diff] [blame] | 2395 | // If a session error has occurred the PeerConnection is in a possibly |
| 2396 | // inconsistent state so fail right away. |
| 2397 | if (session_error() != SessionError::kNone) { |
| 2398 | std::string error_message = GetSessionErrorMsg(); |
| 2399 | RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message; |
| 2400 | PostCreateSessionDescriptionFailure( |
| 2401 | observer, |
| 2402 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2403 | return; |
| 2404 | } |
| 2405 | |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 2406 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 2407 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 2408 | std::string error = |
| 2409 | "PeerConnection cannot create an answer in a state other than " |
| 2410 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2411 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2412 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 2413 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2414 | return; |
| 2415 | } |
| 2416 | |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 2417 | // The remote description should be set if we're in the right state. |
| 2418 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2419 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2420 | if (IsUnifiedPlan()) { |
| 2421 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2422 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2423 | "supported with Unified Plan semantics. Use the " |
| 2424 | "RtpTransceiver API instead."; |
| 2425 | } |
| 2426 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2427 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2428 | "supported with Unified Plan semantics. Use the " |
| 2429 | "RtpTransceiver API instead."; |
| 2430 | } |
| 2431 | } |
| 2432 | |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2433 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2434 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2435 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 2436 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | void PeerConnection::SetLocalDescription( |
| 2440 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2441 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2442 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2443 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2444 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2445 | // lambda will execute immediately. |
| 2446 | operations_chain_->ChainOperation( |
| 2447 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2448 | observer_refptr = |
| 2449 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 2450 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 2451 | std::function<void()> operations_chain_callback) mutable { |
| 2452 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2453 | if (!this_weak_ptr) { |
| 2454 | // For consistency with DoSetLocalDescription(), we DO NOT inform the |
| 2455 | // |observer_refptr| that the operation failed in this case. |
| 2456 | // TODO(hbos): If/when we process SLD messages in ~PeerConnection, |
| 2457 | // the consistent thing would be to inform the observer here. |
| 2458 | operations_chain_callback(); |
| 2459 | return; |
| 2460 | } |
| 2461 | this_weak_ptr->DoSetLocalDescription(std::move(desc), |
| 2462 | std::move(observer_refptr)); |
| 2463 | // DoSetLocalDescription() is currently implemented as a synchronous |
| 2464 | // operation but where the |observer|'s callbacks are invoked |
| 2465 | // asynchronously in a post to OnMessage(). |
| 2466 | // For backwards-compatability reasons, we declare the operation as |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 2467 | // completed here (rather than in OnMessage()). This ensures that |
| 2468 | // subsequent offer/answer operations can start immediately (without |
| 2469 | // waiting for OnMessage()). |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2470 | operations_chain_callback(); |
| 2471 | }); |
| 2472 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2473 | |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 2474 | void PeerConnection::SetLocalDescription( |
| 2475 | SetSessionDescriptionObserver* observer) { |
| 2476 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2477 | // The |create_sdp_observer| handles performing DoSetLocalDescription() with |
| 2478 | // the resulting description as well as completing the operation. |
| 2479 | rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver> |
| 2480 | create_sdp_observer( |
| 2481 | new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>( |
| 2482 | weak_ptr_factory_.GetWeakPtr(), |
| 2483 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer))); |
| 2484 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2485 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2486 | // lambda will execute immediately. |
| 2487 | operations_chain_->ChainOperation( |
| 2488 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2489 | create_sdp_observer](std::function<void()> operations_chain_callback) { |
| 2490 | // The |create_sdp_observer| is responsible for completing the |
| 2491 | // operation. |
| 2492 | create_sdp_observer->SetOperationCompleteCallback( |
| 2493 | std::move(operations_chain_callback)); |
| 2494 | // Abort early if |this_weak_ptr| is no longer valid. This triggers the |
| 2495 | // same code path as if DoCreateOffer() or DoCreateAnswer() failed. |
| 2496 | if (!this_weak_ptr) { |
| 2497 | create_sdp_observer->OnFailure(RTCError( |
| 2498 | RTCErrorType::INTERNAL_ERROR, |
| 2499 | "SetLocalDescription failed because the session was shut down")); |
| 2500 | return; |
| 2501 | } |
| 2502 | switch (this_weak_ptr->signaling_state()) { |
| 2503 | case PeerConnectionInterface::kStable: |
| 2504 | case PeerConnectionInterface::kHaveLocalOffer: |
| 2505 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 2506 | // TODO(hbos): If [LastCreatedOffer] exists and still represents the |
| 2507 | // current state of the system, use that instead of creating another |
| 2508 | // offer. |
| 2509 | this_weak_ptr->DoCreateOffer(RTCOfferAnswerOptions(), |
| 2510 | create_sdp_observer); |
| 2511 | break; |
| 2512 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 2513 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 2514 | // TODO(hbos): If [LastCreatedAnswer] exists and still represents |
| 2515 | // the current state of the system, use that instead of creating |
| 2516 | // another answer. |
| 2517 | this_weak_ptr->DoCreateAnswer(RTCOfferAnswerOptions(), |
| 2518 | create_sdp_observer); |
| 2519 | break; |
| 2520 | case PeerConnectionInterface::kClosed: |
| 2521 | create_sdp_observer->OnFailure(RTCError( |
| 2522 | RTCErrorType::INVALID_STATE, |
| 2523 | "SetLocalDescription called when PeerConnection is closed.")); |
| 2524 | break; |
| 2525 | } |
| 2526 | }); |
| 2527 | } |
| 2528 | |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2529 | void PeerConnection::DoSetLocalDescription( |
| 2530 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2531 | rtc::scoped_refptr<SetSessionDescriptionObserver> observer) { |
| 2532 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2533 | TRACE_EVENT0("webrtc", "PeerConnection::DoSetLocalDescription"); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2534 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2535 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2536 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2537 | return; |
| 2538 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2539 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2540 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2541 | PostSetSessionDescriptionFailure( |
| 2542 | observer, |
| 2543 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2544 | return; |
| 2545 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2546 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2547 | // If a session error has occurred the PeerConnection is in a possibly |
| 2548 | // inconsistent state so fail right away. |
| 2549 | if (session_error() != SessionError::kNone) { |
| 2550 | std::string error_message = GetSessionErrorMsg(); |
| 2551 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2552 | PostSetSessionDescriptionFailure( |
| 2553 | observer, |
| 2554 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2555 | return; |
| 2556 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2557 | |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2558 | // For SLD we support only explicit rollback. |
| 2559 | if (desc->GetType() == SdpType::kRollback) { |
| 2560 | if (IsUnifiedPlan()) { |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 2561 | RTCError error = Rollback(desc->GetType()); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2562 | if (error.ok()) { |
| 2563 | PostSetSessionDescriptionSuccess(observer); |
| 2564 | } else { |
| 2565 | PostSetSessionDescriptionFailure(observer, std::move(error)); |
| 2566 | } |
| 2567 | } else { |
| 2568 | PostSetSessionDescriptionFailure( |
| 2569 | observer, RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 2570 | "Rollback not supported in Plan B")); |
| 2571 | } |
| 2572 | return; |
| 2573 | } |
| 2574 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2575 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2576 | if (!error.ok()) { |
| 2577 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2578 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2579 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2580 | PostSetSessionDescriptionFailure( |
| 2581 | observer, |
| 2582 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2583 | return; |
| 2584 | } |
| 2585 | |
| 2586 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2587 | // which may destroy it before returning. |
| 2588 | const SdpType type = desc->GetType(); |
| 2589 | |
| 2590 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2591 | // |desc| may be destroyed at this point. |
| 2592 | |
| 2593 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2594 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2595 | // inconsistent state, so act conservatively here and set the session error |
| 2596 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2597 | SetSessionError(SessionError::kContent, error.message()); |
| 2598 | std::string error_message = |
| 2599 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2600 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2601 | PostSetSessionDescriptionFailure( |
| 2602 | observer, |
| 2603 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2604 | return; |
| 2605 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2606 | RTC_DCHECK(local_description()); |
| 2607 | |
| 2608 | PostSetSessionDescriptionSuccess(observer); |
| 2609 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 2610 | // MaybeStartGathering needs to be called after posting |
| 2611 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2612 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2613 | transport_controller_->MaybeStartGathering(); |
| 2614 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 2615 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2616 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2617 | // MaybeStartGathering... |
| 2618 | network_thread()->Invoke<void>( |
| 2619 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2620 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 2621 | // Make UMA notes about what was agreed to. |
| 2622 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2623 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2624 | |
| 2625 | if (IsUnifiedPlan()) { |
| 2626 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2627 | UpdateNegotiationNeeded(); |
| 2628 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2629 | is_negotiation_needed_) { |
| 2630 | Observer()->OnRenegotiationNeeded(); |
| 2631 | } |
| 2632 | } |
| 2633 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2634 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | RTCError PeerConnection::ApplyLocalDescription( |
| 2638 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2639 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2640 | RTC_DCHECK(desc); |
| 2641 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2642 | // Update stats here so that we have the most recent stats for tracks and |
| 2643 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 2644 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2645 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2646 | // Take a reference to the old local description since it's used below to |
| 2647 | // compare against the new local description. When setting the new local |
| 2648 | // description, grab ownership of the replaced session description in case it |
| 2649 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2650 | // of the method. |
| 2651 | const SessionDescriptionInterface* old_local_description = |
| 2652 | local_description(); |
| 2653 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2654 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 2655 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2656 | replaced_local_description = pending_local_description_ |
| 2657 | ? std::move(pending_local_description_) |
| 2658 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2659 | current_local_description_ = std::move(desc); |
| 2660 | pending_local_description_ = nullptr; |
| 2661 | current_remote_description_ = std::move(pending_remote_description_); |
| 2662 | } else { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2663 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2664 | pending_local_description_ = std::move(desc); |
| 2665 | } |
| 2666 | // The session description to apply now must be accessed by |
| 2667 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2668 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2669 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 2670 | // Report statistics about any use of simulcast. |
| 2671 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 2672 | *local_description()->description()); |
| 2673 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2674 | if (!is_caller_) { |
| 2675 | if (remote_description()) { |
| 2676 | // Remote description was applied first, so this PC is the callee. |
| 2677 | is_caller_ = false; |
| 2678 | } else { |
| 2679 | // Local description is applied first, so this PC is the caller. |
| 2680 | is_caller_ = true; |
| 2681 | } |
| 2682 | } |
| 2683 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2684 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2685 | if (!error.ok()) { |
| 2686 | return error; |
| 2687 | } |
| 2688 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2689 | if (IsUnifiedPlan()) { |
| 2690 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 2691 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2692 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2693 | if (!error.ok()) { |
| 2694 | return error; |
| 2695 | } |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2696 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2697 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2698 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2699 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2700 | // Note that code paths that don't set MID won't be able to use |
| 2701 | // information about DTLS transports. |
| 2702 | if (transceiver->mid()) { |
| 2703 | auto dtls_transport = |
| 2704 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2705 | transceiver->internal()->sender_internal()->set_transport( |
| 2706 | dtls_transport); |
| 2707 | transceiver->internal()->receiver_internal()->set_transport( |
| 2708 | dtls_transport); |
| 2709 | } |
| 2710 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2711 | const ContentInfo* content = |
| 2712 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2713 | if (!content) { |
| 2714 | continue; |
| 2715 | } |
| 2716 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2717 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2718 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2719 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2720 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2721 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2722 | // "recvonly", process the removal of a remote track for the media |
| 2723 | // description, given transceiver, removeList, and muteTracks. |
| 2724 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2725 | (transceiver->internal()->fired_direction() && |
| 2726 | RtpTransceiverDirectionHasRecv( |
| 2727 | *transceiver->internal()->fired_direction()))) { |
| 2728 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2729 | &removed_streams); |
| 2730 | } |
| 2731 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2732 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2733 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2734 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2735 | } |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2736 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2737 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2738 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2739 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2740 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2741 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2742 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2743 | } |
| 2744 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2745 | // Media channels will be created only when offer is set. These may use new |
| 2746 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2747 | if (type == SdpType::kOffer) { |
| 2748 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2749 | // description is applied. Restore back to old description. |
| 2750 | RTCError error = CreateChannels(*local_description()->description()); |
| 2751 | if (!error.ok()) { |
| 2752 | return error; |
| 2753 | } |
| 2754 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2755 | // Remove unused channels if MediaContentDescription is rejected. |
| 2756 | RemoveUnusedChannels(local_description()->description()); |
| 2757 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2758 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2759 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2760 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2761 | if (!error.ok()) { |
| 2762 | return error; |
| 2763 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2764 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2765 | if (remote_description()) { |
| 2766 | // Now that we have a local description, we can push down remote candidates. |
| 2767 | UseCandidatesInSessionDescription(remote_description()); |
| 2768 | } |
| 2769 | |
| 2770 | pending_ice_restarts_.clear(); |
| 2771 | if (session_error() != SessionError::kNone) { |
| 2772 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2773 | } |
| 2774 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2775 | // If setting the description decided our SSL role, allocate any necessary |
| 2776 | // SCTP sids. |
| 2777 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2778 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2779 | AllocateSctpSids(role); |
| 2780 | } |
| 2781 | |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2782 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2783 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2784 | const ContentInfo* content = |
| 2785 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2786 | if (!content) { |
| 2787 | continue; |
| 2788 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2789 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2790 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 18:32:27 | [diff] [blame] | 2791 | // 0 is a special value meaning "this sender has no associated send |
| 2792 | // stream". Need to call this so the sender won't attempt to configure |
| 2793 | // a no longer existing stream and run into DCHECKs in the lower |
| 2794 | // layers. |
| 2795 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2796 | } else { |
| 2797 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2798 | const std::vector<StreamParams>& streams = channel->local_streams(); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 2799 | transceiver->internal()->sender_internal()->set_stream_ids( |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2800 | streams[0].stream_ids()); |
| 2801 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2802 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | } else { |
| 2806 | // Plan B semantics. |
| 2807 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2808 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2809 | // local session description. |
| 2810 | const cricket::ContentInfo* audio_content = |
| 2811 | GetFirstAudioContent(local_description()->description()); |
| 2812 | if (audio_content) { |
| 2813 | if (audio_content->rejected) { |
| 2814 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2815 | } else { |
| 2816 | const cricket::AudioContentDescription* audio_desc = |
| 2817 | audio_content->media_description()->as_audio(); |
| 2818 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2819 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2820 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2821 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2822 | const cricket::ContentInfo* video_content = |
| 2823 | GetFirstVideoContent(local_description()->description()); |
| 2824 | if (video_content) { |
| 2825 | if (video_content->rejected) { |
| 2826 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2827 | } else { |
| 2828 | const cricket::VideoContentDescription* video_desc = |
| 2829 | video_content->media_description()->as_video(); |
| 2830 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2831 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2832 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2836 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2837 | if (data_content) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 2838 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 2839 | data_content->media_description()->as_rtp_data(); |
| 2840 | // rtp_data_desc will be null if this is an SCTP description. |
| 2841 | if (rtp_data_desc) { |
| 2842 | UpdateLocalRtpDataChannels(rtp_data_desc->streams()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 2846 | if (type == SdpType::kAnswer && |
| 2847 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 2848 | *current_local_description_)) { |
| 2849 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 2850 | } |
| 2851 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2852 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2853 | } |
| 2854 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2855 | // The SDP parser used to populate these values by default for the 'content |
| 2856 | // name' if an a=mid line was absent. |
| 2857 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2858 | switch (media_type) { |
| 2859 | case cricket::MEDIA_TYPE_AUDIO: |
| 2860 | return cricket::CN_AUDIO; |
| 2861 | case cricket::MEDIA_TYPE_VIDEO: |
| 2862 | return cricket::CN_VIDEO; |
| 2863 | case cricket::MEDIA_TYPE_DATA: |
| 2864 | return cricket::CN_DATA; |
| 2865 | } |
| 2866 | RTC_NOTREACHED(); |
| 2867 | return ""; |
| 2868 | } |
| 2869 | |
| 2870 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2871 | cricket::SessionDescription* new_remote_description) { |
| 2872 | RTC_DCHECK(new_remote_description); |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 2873 | const cricket::ContentInfos no_infos; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2874 | const cricket::ContentInfos& local_contents = |
| 2875 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 2876 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2877 | const cricket::ContentInfos& remote_contents = |
| 2878 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 2879 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2880 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2881 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2882 | if (!content.name.empty()) { |
| 2883 | continue; |
| 2884 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2885 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2886 | absl::string_view source_explanation; |
| 2887 | if (IsUnifiedPlan()) { |
| 2888 | if (i < local_contents.size()) { |
| 2889 | new_mid = local_contents[i].name; |
| 2890 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2891 | } else if (i < remote_contents.size()) { |
| 2892 | new_mid = remote_contents[i].name; |
| 2893 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2894 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2895 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2896 | source_explanation = "generated just now"; |
| 2897 | } |
| 2898 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2899 | new_mid = std::string( |
| 2900 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2901 | source_explanation = "to match pre-existing behavior"; |
| 2902 | } |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2903 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2904 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2905 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2906 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2907 | << " is missing an a=mid line. Filling in the value '" |
| 2908 | << new_mid << "' " << source_explanation << "."; |
| 2909 | } |
| 2910 | } |
| 2911 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2912 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 | [diff] [blame] | 2913 | SetSessionDescriptionObserver* observer, |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2914 | SessionDescriptionInterface* desc_ptr) { |
| 2915 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2916 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2917 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2918 | // lambda will execute immediately. |
| 2919 | operations_chain_->ChainOperation( |
| 2920 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2921 | observer_refptr = |
| 2922 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 2923 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 2924 | std::function<void()> operations_chain_callback) mutable { |
| 2925 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2926 | if (!this_weak_ptr) { |
| 2927 | // For consistency with SetRemoteDescriptionObserverAdapter, we DO NOT |
| 2928 | // inform the |observer_refptr| that the operation failed in this |
| 2929 | // case. |
| 2930 | // TODO(hbos): If/when we process SRD messages in ~PeerConnection, |
| 2931 | // the consistent thing would be to inform the observer here. |
| 2932 | operations_chain_callback(); |
| 2933 | return; |
| 2934 | } |
| 2935 | this_weak_ptr->DoSetRemoteDescription( |
| 2936 | std::move(desc), |
| 2937 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2938 | new SetRemoteDescriptionObserverAdapter( |
| 2939 | this_weak_ptr.get(), std::move(observer_refptr)))); |
| 2940 | // DoSetRemoteDescription() is currently implemented as a synchronous |
| 2941 | // operation but where SetRemoteDescriptionObserverAdapter ensures that |
| 2942 | // the |observer|'s callbacks are invoked asynchronously in a post to |
| 2943 | // OnMessage(). |
| 2944 | // For backwards-compatability reasons, we declare the operation as |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 2945 | // completed here (rather than in OnMessage()). This ensures that |
| 2946 | // subsequent offer/answer operations can start immediately (without |
| 2947 | // waiting for OnMessage()). |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2948 | operations_chain_callback(); |
| 2949 | }); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | void PeerConnection::SetRemoteDescription( |
| 2953 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2954 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2955 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2956 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2957 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2958 | // lambda will execute immediately. |
| 2959 | operations_chain_->ChainOperation( |
| 2960 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer, |
| 2961 | desc = std::move(desc)]( |
| 2962 | std::function<void()> operations_chain_callback) mutable { |
| 2963 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2964 | if (!this_weak_ptr) { |
| 2965 | // For consistency with DoSetRemoteDescription(), we DO inform the |
| 2966 | // |observer| that the operation failed in this case. |
| 2967 | observer->OnSetRemoteDescriptionComplete(RTCError( |
| 2968 | RTCErrorType::INVALID_STATE, |
| 2969 | "Failed to set remote offer sdp: failed because the session was " |
| 2970 | "shut down")); |
| 2971 | operations_chain_callback(); |
| 2972 | return; |
| 2973 | } |
| 2974 | this_weak_ptr->DoSetRemoteDescription(std::move(desc), |
| 2975 | std::move(observer)); |
| 2976 | // DoSetRemoteDescription() is currently implemented as a synchronous |
| 2977 | // operation. The |observer| will already have been informed that it |
| 2978 | // completed, and we can mark this operation as complete without any |
| 2979 | // loose ends. |
| 2980 | operations_chain_callback(); |
| 2981 | }); |
| 2982 | } |
| 2983 | |
| 2984 | void PeerConnection::DoSetRemoteDescription( |
| 2985 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2986 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
| 2987 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2988 | TRACE_EVENT0("webrtc", "PeerConnection::DoSetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2989 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2990 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2991 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2992 | return; |
| 2993 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2994 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2995 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 2996 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2997 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2998 | return; |
| 2999 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 3000 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3001 | // If a session error has occurred the PeerConnection is in a possibly |
| 3002 | // inconsistent state so fail right away. |
| 3003 | if (session_error() != SessionError::kNone) { |
| 3004 | std::string error_message = GetSessionErrorMsg(); |
| 3005 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 3006 | observer->OnSetRemoteDescriptionComplete( |
| 3007 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 3008 | return; |
| 3009 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3010 | if (IsUnifiedPlan()) { |
| 3011 | if (configuration_.enable_implicit_rollback) { |
| 3012 | if (desc->GetType() == SdpType::kOffer && |
| 3013 | signaling_state() == kHaveLocalOffer) { |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 3014 | Rollback(desc->GetType()); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | // Explicit rollback. |
| 3018 | if (desc->GetType() == SdpType::kRollback) { |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 3019 | observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType())); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3020 | return; |
| 3021 | } |
| 3022 | } else if (desc->GetType() == SdpType::kRollback) { |
| 3023 | observer->OnSetRemoteDescriptionComplete( |
| 3024 | RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 3025 | "Rollback not supported in Plan B")); |
| 3026 | return; |
| 3027 | } |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 3028 | if (desc->GetType() == SdpType::kOffer) { |
| 3029 | // Report to UMA the format of the received offer. |
| 3030 | ReportSdpFormatReceived(*desc); |
| 3031 | } |
| 3032 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 3033 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 3034 | // points. |
| 3035 | FillInMissingRemoteMids(desc->description()); |
| 3036 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3037 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 3038 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3039 | std::string error_message = GetSetDescriptionErrorMessage( |
| 3040 | cricket::CS_REMOTE, desc->GetType(), error); |
| 3041 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 3042 | observer->OnSetRemoteDescriptionComplete( |
| 3043 | RTCError(error.type(), std::move(error_message))); |
| 3044 | return; |
| 3045 | } |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 3046 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3047 | // Grab the description type before moving ownership to |
| 3048 | // ApplyRemoteDescription, which may destroy it before returning. |
| 3049 | const SdpType type = desc->GetType(); |
| 3050 | |
| 3051 | error = ApplyRemoteDescription(std::move(desc)); |
| 3052 | // |desc| may be destroyed at this point. |
| 3053 | |
| 3054 | if (!error.ok()) { |
| 3055 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 3056 | // inconsistent state, so act conservatively here and set the session error |
| 3057 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 3058 | SetSessionError(SessionError::kContent, error.message()); |
| 3059 | std::string error_message = |
| 3060 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 3061 | RTC_LOG(LS_ERROR) << error_message; |
| 3062 | observer->OnSetRemoteDescriptionComplete( |
| 3063 | RTCError(error.type(), std::move(error_message))); |
| 3064 | return; |
| 3065 | } |
| 3066 | RTC_DCHECK(remote_description()); |
| 3067 | |
| 3068 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3069 | // TODO(deadbeef): We already had to hop to the network thread for |
| 3070 | // MaybeStartGathering... |
| 3071 | network_thread()->Invoke<void>( |
| 3072 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3073 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 22:48:00 | [diff] [blame] | 3074 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 3075 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3076 | } |
| 3077 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 3078 | if (IsUnifiedPlan()) { |
| 3079 | bool was_negotiation_needed = is_negotiation_needed_; |
| 3080 | UpdateNegotiationNeeded(); |
| 3081 | if (signaling_state() == kStable && was_negotiation_needed && |
| 3082 | is_negotiation_needed_) { |
| 3083 | Observer()->OnRenegotiationNeeded(); |
| 3084 | } |
| 3085 | } |
| 3086 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3087 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 3088 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | RTCError PeerConnection::ApplyRemoteDescription( |
| 3092 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 3093 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3094 | RTC_DCHECK(desc); |
| 3095 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3096 | // Update stats here so that we have the most recent stats for tracks and |
| 3097 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 3098 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3099 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3100 | // Take a reference to the old remote description since it's used below to |
| 3101 | // compare against the new remote description. When setting the new remote |
| 3102 | // description, grab ownership of the replaced session description in case it |
| 3103 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 3104 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3105 | const SessionDescriptionInterface* old_remote_description = |
| 3106 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3107 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 3108 | SdpType type = desc->GetType(); |
| 3109 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3110 | replaced_remote_description = pending_remote_description_ |
| 3111 | ? std::move(pending_remote_description_) |
| 3112 | : std::move(current_remote_description_); |
| 3113 | current_remote_description_ = std::move(desc); |
| 3114 | pending_remote_description_ = nullptr; |
| 3115 | current_local_description_ = std::move(pending_local_description_); |
| 3116 | } else { |
| 3117 | replaced_remote_description = std::move(pending_remote_description_); |
| 3118 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3119 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3120 | // The session description to apply now must be accessed by |
| 3121 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3122 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3123 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 3124 | // Report statistics about any use of simulcast. |
| 3125 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 3126 | *remote_description()->description()); |
| 3127 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3128 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 3129 | if (!error.ok()) { |
| 3130 | return error; |
| 3131 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3132 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3133 | if (IsUnifiedPlan()) { |
| 3134 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3135 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 3136 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3137 | if (!error.ok()) { |
| 3138 | return error; |
| 3139 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3140 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3141 | // Media channels will be created only when offer is set. These may use new |
| 3142 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3143 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3144 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3145 | // description is applied. Restore back to old description. |
| 3146 | RTCError error = CreateChannels(*remote_description()->description()); |
| 3147 | if (!error.ok()) { |
| 3148 | return error; |
| 3149 | } |
| 3150 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3151 | // Remove unused channels if MediaContentDescription is rejected. |
| 3152 | RemoveUnusedChannels(remote_description()->description()); |
| 3153 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3154 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3155 | // NOTE: Candidates allocation will be initiated only when |
| 3156 | // SetLocalDescription is called. |
| 3157 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 3158 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3159 | if (!error.ok()) { |
| 3160 | return error; |
| 3161 | } |
| 3162 | |
| 3163 | if (local_description() && |
| 3164 | !UseCandidatesInSessionDescription(remote_description())) { |
| 3165 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 3166 | } |
| 3167 | |
| 3168 | if (old_remote_description) { |
| 3169 | for (const cricket::ContentInfo& content : |
| 3170 | old_remote_description->description()->contents()) { |
| 3171 | // Check if this new SessionDescription contains new ICE ufrag and |
| 3172 | // password that indicates the remote peer requests an ICE restart. |
| 3173 | // TODO(deadbeef): When we start storing both the current and pending |
| 3174 | // remote description, this should reset pending_ice_restarts and compare |
| 3175 | // against the current description. |
| 3176 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 3177 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 3178 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3179 | pending_ice_restarts_.insert(content.name); |
| 3180 | } |
| 3181 | } else { |
| 3182 | // We retain all received candidates only if ICE is not restarted. |
| 3183 | // When ICE is restarted, all previous candidates belong to an old |
| 3184 | // generation and should not be kept. |
| 3185 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 3186 | // description should only contain candidates from the last set remote |
| 3187 | // description plus any candidates added since then. We should remove |
| 3188 | // this once we're sure it won't break anything. |
| 3189 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 3190 | old_remote_description, content.name, mutable_remote_description()); |
| 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | if (session_error() != SessionError::kNone) { |
| 3196 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 3197 | } |
| 3198 | |
| 3199 | // Set the the ICE connection state to connecting since the connection may |
| 3200 | // become writable with peer reflexive candidates before any remote candidate |
| 3201 | // is signaled. |
| 3202 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 3203 | // is to have a new signal the indicates a change in checking state from the |
| 3204 | // transport and expose a new checking() member from transport that can be |
| 3205 | // read to determine the current checking state. The existing SignalConnecting |
| 3206 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 3207 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 21:32:17 | [diff] [blame] | 3208 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3209 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 3210 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 3211 | } |
| 3212 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3213 | // If setting the description decided our SSL role, allocate any necessary |
| 3214 | // SCTP sids. |
| 3215 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 3216 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3217 | AllocateSctpSids(role); |
| 3218 | } |
| 3219 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3220 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-17 00:14:42 | [diff] [blame] | 3221 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3222 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3223 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 3224 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3225 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3226 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3227 | const ContentInfo* content = |
| 3228 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 3229 | if (!content) { |
| 3230 | continue; |
| 3231 | } |
| 3232 | const MediaContentDescription* media_desc = content->media_description(); |
| 3233 | RtpTransceiverDirection local_direction = |
| 3234 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3235 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 3236 | // RTCSessionDescription: Set the associated remote streams given |
| 3237 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 3238 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 3239 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 3240 | std::vector<std::string> stream_ids; |
| 3241 | if (!media_desc->streams().empty()) { |
| 3242 | // The remote description has signaled the stream IDs. |
| 3243 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-11 01:15:20 | [diff] [blame] | 3244 | } |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3245 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 3246 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 3247 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 3248 | stream_ids, &added_streams, |
| 3249 | &removed_streams); |
| 3250 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 3251 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 3252 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 3253 | // process the addition of a remote track for the media description. |
| 3254 | if (!transceiver->fired_direction() || |
| 3255 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 3256 | RTC_LOG(LS_INFO) |
| 3257 | << "Processing the addition of a remote track for MID=" |
| 3258 | << content->name << "."; |
| 3259 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 10:25:05 | [diff] [blame] | 3260 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3261 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3262 | // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3263 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 3264 | // removal of a remote track for the media description, given transceiver, |
| 3265 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3266 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3267 | (transceiver->fired_direction() && |
| 3268 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 3269 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 3270 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3271 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3272 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3273 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3274 | // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3275 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3276 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3277 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3278 | // direction. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3279 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3280 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 3281 | if (transceiver->mid()) { |
| 3282 | auto dtls_transport = |
| 3283 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 3284 | transceiver->internal()->sender_internal()->set_transport( |
| 3285 | dtls_transport); |
| 3286 | transceiver->internal()->receiver_internal()->set_transport( |
| 3287 | dtls_transport); |
| 3288 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3289 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3290 | // 2.2.8.1.12: If the media description is rejected, and transceiver is |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3291 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3292 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3293 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 3294 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3295 | transceiver->Stop(); |
| 3296 | } |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 3297 | if (!content->rejected && |
| 3298 | RtpTransceiverDirectionHasRecv(local_direction)) { |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 3299 | if (!media_desc->streams().empty() && |
| 3300 | media_desc->streams()[0].has_ssrcs()) { |
Saurav Das | 7262fc2 | 2019-09-11 23:23:05 | [diff] [blame] | 3301 | uint32_t ssrc = media_desc->streams()[0].first_ssrc(); |
| 3302 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
| 3303 | } else { |
| 3304 | transceiver->internal() |
| 3305 | ->receiver_internal() |
| 3306 | ->SetupUnsignaledMediaChannel(); |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 3307 | } |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 3308 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3309 | } |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 3310 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3311 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3312 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 20:59:16 | [diff] [blame] | 3313 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3314 | observer->OnTrack(transceiver); |
| 3315 | observer->OnAddTrack(transceiver->receiver(), |
| 3316 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-11 01:15:20 | [diff] [blame] | 3317 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3318 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3319 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 3320 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3321 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3322 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3323 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3324 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3325 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3326 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3327 | } |
| 3328 | |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3329 | const cricket::ContentInfo* audio_content = |
| 3330 | GetFirstAudioContent(remote_description()->description()); |
| 3331 | const cricket::ContentInfo* video_content = |
| 3332 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3333 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3334 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3335 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3336 | GetFirstVideoContentDescription(remote_description()->description()); |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 3337 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 3338 | GetFirstRtpDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3339 | |
| 3340 | // Check if the descriptions include streams, just in case the peer supports |
| 3341 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3342 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3343 | (audio_desc && !audio_desc->streams().empty()) || |
| 3344 | (video_desc && !video_desc->streams().empty())) { |
| 3345 | remote_peer_supports_msid_ = true; |
| 3346 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3347 | |
| 3348 | // We wait to signal new streams until we finish processing the description, |
| 3349 | // since only at that point will new streams have all their tracks. |
| 3350 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 3351 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3352 | if (!IsUnifiedPlan()) { |
| 3353 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 3354 | // rejected media section, there is some cleanup logic that expects the |
| 3355 | // voice/ video channel to still be set. But in this method the voice/video |
| 3356 | // channel would have been destroyed by the SetRemoteDescription caller |
| 3357 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 3358 | // calls should be moved to right before the DestroyChannel calls to fix |
| 3359 | // this. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 3360 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3361 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 3362 | // and MediaStreams. |
| 3363 | if (audio_content) { |
| 3364 | if (audio_content->rejected) { |
| 3365 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 3366 | } else { |
| 3367 | bool default_audio_track_needed = |
| 3368 | !remote_peer_supports_msid_ && |
| 3369 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 3370 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 3371 | default_audio_track_needed, audio_desc->type(), |
| 3372 | new_streams); |
| 3373 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 3374 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3375 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3376 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 3377 | // and MediaStreams. |
| 3378 | if (video_content) { |
| 3379 | if (video_content->rejected) { |
| 3380 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 3381 | } else { |
| 3382 | bool default_video_track_needed = |
| 3383 | !remote_peer_supports_msid_ && |
| 3384 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 3385 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 3386 | default_video_track_needed, video_desc->type(), |
| 3387 | new_streams); |
| 3388 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 3389 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3390 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 3391 | // If this is an RTP data transport, update the DataChannels with the |
| 3392 | // information from the remote peer. |
| 3393 | if (rtp_data_desc) { |
| 3394 | UpdateRemoteRtpDataChannels(GetActiveStreams(rtp_data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3395 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3396 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3397 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3398 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3399 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 3400 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 3401 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3402 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3403 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 3404 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3405 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3406 | UpdateEndedRemoteMediaStreams(); |
| 3407 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3408 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 3409 | if (type == SdpType::kAnswer && |
| 3410 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 3411 | *current_local_description_)) { |
| 3412 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 3413 | } |
| 3414 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3415 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 23:42:33 | [diff] [blame] | 3416 | } |
| 3417 | |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3418 | void PeerConnection::SetAssociatedRemoteStreams( |
| 3419 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 3420 | const std::vector<std::string>& stream_ids, |
| 3421 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 3422 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3423 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 3424 | for (const std::string& stream_id : stream_ids) { |
| 3425 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3426 | remote_streams_->find(stream_id); |
| 3427 | if (!stream) { |
| 3428 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3429 | MediaStream::Create(stream_id)); |
| 3430 | remote_streams_->AddStream(stream); |
| 3431 | added_streams->push_back(stream); |
| 3432 | } |
| 3433 | media_streams.push_back(stream); |
| 3434 | } |
| 3435 | // Special case: "a=msid" missing, use random stream ID. |
| 3436 | if (media_streams.empty() && |
| 3437 | !(remote_description()->description()->msid_signaling() & |
| 3438 | cricket::kMsidSignalingMediaSection)) { |
| 3439 | if (!missing_msid_default_stream_) { |
| 3440 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 3441 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 3442 | added_streams->push_back(missing_msid_default_stream_); |
| 3443 | } |
| 3444 | media_streams.push_back(missing_msid_default_stream_); |
| 3445 | } |
| 3446 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 3447 | receiver->streams(); |
| 3448 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 3449 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 3450 | // update the stream-track relationships in a later step. We do this earlier, |
| 3451 | // changing the order of things, but the end-result is the same. |
| 3452 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 3453 | // instead. https://crbug.com/webrtc/9480 |
| 3454 | receiver->SetStreams(media_streams); |
| 3455 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3456 | } |
| 3457 | |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3458 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 3459 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3460 | transceiver, |
| 3461 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 3462 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3463 | RTC_DCHECK(transceiver->mid()); |
| 3464 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 3465 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3466 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3467 | transceiver->internal()->receiver_internal()->streams(); |
| 3468 | // This will remove the remote track from the streams. |
| 3469 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 3470 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3471 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3472 | } |
| 3473 | |
| 3474 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 3475 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 3476 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3477 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 3478 | // streams, see if the stream was removed by checking if this was the last |
| 3479 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3480 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3481 | if (remote_stream->GetAudioTracks().empty() && |
| 3482 | remote_stream->GetVideoTracks().empty()) { |
| 3483 | remote_streams_->RemoveStream(remote_stream); |
| 3484 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3485 | } |
| 3486 | } |
| 3487 | } |
| 3488 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3489 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 3490 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3491 | const SessionDescriptionInterface& new_session, |
| 3492 | const SessionDescriptionInterface* old_local_description, |
| 3493 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3494 | RTC_DCHECK(IsUnifiedPlan()); |
| 3495 | |
Steve Anton | 7464fca | 2018-01-19 19:10:37 | [diff] [blame] | 3496 | const cricket::ContentGroup* bundle_group = nullptr; |
| 3497 | if (new_session.GetType() == SdpType::kOffer) { |
| 3498 | auto bundle_group_or_error = |
| 3499 | GetEarlyBundleGroup(*new_session.description()); |
| 3500 | if (!bundle_group_or_error.ok()) { |
| 3501 | return bundle_group_or_error.MoveError(); |
| 3502 | } |
| 3503 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3504 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3505 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3506 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3507 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 3508 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3509 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 3510 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3511 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3512 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3513 | const cricket::ContentInfo* old_local_content = nullptr; |
| 3514 | if (old_local_description && |
| 3515 | i < old_local_description->description()->contents().size()) { |
| 3516 | old_local_content = |
| 3517 | &old_local_description->description()->contents()[i]; |
| 3518 | } |
| 3519 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 3520 | if (old_remote_description && |
| 3521 | i < old_remote_description->description()->contents().size()) { |
| 3522 | old_remote_content = |
| 3523 | &old_remote_description->description()->contents()[i]; |
| 3524 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3525 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3526 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 3527 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3528 | if (!transceiver_or_error.ok()) { |
| 3529 | return transceiver_or_error.MoveError(); |
| 3530 | } |
| 3531 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3532 | RTCError error = |
| 3533 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 3534 | if (!error.ok()) { |
| 3535 | return error; |
| 3536 | } |
| 3537 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3538 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 3539 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3540 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 3541 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3542 | continue; |
| 3543 | } |
| 3544 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 3545 | if (!error.ok()) { |
| 3546 | return error; |
| 3547 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3548 | } else { |
| 3549 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3550 | "Unknown section type."); |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | return RTCError::OK(); |
| 3555 | } |
| 3556 | |
| 3557 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 3558 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3559 | transceiver, |
| 3560 | const cricket::ContentInfo& content, |
| 3561 | const cricket::ContentGroup* bundle_group) { |
| 3562 | RTC_DCHECK(IsUnifiedPlan()); |
| 3563 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3564 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3565 | if (content.rejected) { |
| 3566 | if (channel) { |
| 3567 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3568 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3569 | } |
| 3570 | } else { |
| 3571 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3572 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3573 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3574 | } else { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3575 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3576 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3577 | } |
| 3578 | if (!channel) { |
| 3579 | LOG_AND_RETURN_ERROR( |
| 3580 | RTCErrorType::INTERNAL_ERROR, |
| 3581 | "Failed to create channel for mid=" + content.name); |
| 3582 | } |
| 3583 | transceiver->internal()->SetChannel(channel); |
| 3584 | } |
| 3585 | } |
| 3586 | return RTCError::OK(); |
| 3587 | } |
| 3588 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3589 | RTCError PeerConnection::UpdateDataChannel( |
| 3590 | cricket::ContentSource source, |
| 3591 | const cricket::ContentInfo& content, |
| 3592 | const cricket::ContentGroup* bundle_group) { |
| 3593 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 23:23:40 | [diff] [blame] | 3594 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3595 | // will take care of rejecting it. |
| 3596 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3597 | } |
| 3598 | if (content.rejected) { |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 3599 | RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid(); |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 3600 | DestroyDataChannelTransport(); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3601 | } else { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 3602 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 3603 | RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3604 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3605 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3606 | "Failed to create data channel."); |
| 3607 | } |
| 3608 | } |
| 3609 | if (source == cricket::CS_REMOTE) { |
| 3610 | const MediaContentDescription* data_desc = content.media_description(); |
| 3611 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 3612 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 3613 | } |
| 3614 | } |
| 3615 | } |
| 3616 | return RTCError::OK(); |
| 3617 | } |
| 3618 | |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3619 | // This method will extract any send encodings that were sent by the remote |
| 3620 | // connection. This is currently only relevant for Simulcast scenario (where |
| 3621 | // the number of layers may be communicated by the server). |
| 3622 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 3623 | const MediaContentDescription& desc) { |
| 3624 | if (!desc.HasSimulcast()) { |
| 3625 | return {}; |
| 3626 | } |
| 3627 | std::vector<RtpEncodingParameters> result; |
| 3628 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 3629 | |
| 3630 | // This is a remote description, the parameters we are after should appear |
| 3631 | // as receive streams. |
| 3632 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 3633 | RTC_DCHECK(!alternatives.empty()); |
| 3634 | // There is currently no way to specify or choose from alternatives. |
| 3635 | // We will always use the first alternative, which is the most preferred. |
| 3636 | const SimulcastLayer& layer = alternatives[0]; |
| 3637 | RtpEncodingParameters parameters; |
| 3638 | parameters.rid = layer.rid; |
| 3639 | parameters.active = !layer.is_paused; |
| 3640 | result.push_back(parameters); |
| 3641 | } |
| 3642 | |
| 3643 | return result; |
| 3644 | } |
| 3645 | |
| 3646 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 3647 | const std::vector<SimulcastLayer>& layers, |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3648 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3649 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3650 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3651 | std::vector<std::string> disabled_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3652 | |
| 3653 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 3654 | // So we will iterate over the send encodings rather than the layers. |
| 3655 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3656 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 3657 | [&encoding](const SimulcastLayer& layer) { |
| 3658 | return layer.rid == encoding.rid; |
| 3659 | }); |
| 3660 | // A layer that cannot be found may have been removed by the remote party. |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3661 | if (iter == layers.end()) { |
| 3662 | disabled_layers.push_back(encoding.rid); |
| 3663 | continue; |
| 3664 | } |
| 3665 | |
| 3666 | encoding.active = !iter->is_paused; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3667 | } |
| 3668 | |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3669 | RTCError result = sender->SetParametersInternal(parameters); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3670 | if (result.ok()) { |
| 3671 | result = sender->DisableEncodingLayers(disabled_layers); |
| 3672 | } |
| 3673 | |
| 3674 | return result; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3675 | } |
| 3676 | |
Amit Hilbuch | aabd036 | 2019-03-01 21:14:46 | [diff] [blame] | 3677 | static bool SimulcastIsRejected( |
| 3678 | const ContentInfo* local_content, |
| 3679 | const MediaContentDescription& answer_media_desc) { |
| 3680 | bool simulcast_offered = local_content && |
| 3681 | local_content->media_description() && |
| 3682 | local_content->media_description()->HasSimulcast(); |
| 3683 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 3684 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 3685 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 3686 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 3687 | } |
| 3688 | |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3689 | static RTCError DisableSimulcastInSender( |
| 3690 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3691 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3692 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3693 | if (parameters.encodings.size() <= 1) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3694 | return RTCError::OK(); |
| 3695 | } |
| 3696 | |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3697 | std::vector<std::string> disabled_layers; |
| 3698 | std::transform( |
| 3699 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 3700 | std::back_inserter(disabled_layers), |
| 3701 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 3702 | return sender->DisableEncodingLayers(disabled_layers); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3703 | } |
| 3704 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3705 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3706 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3707 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3708 | size_t mline_index, |
| 3709 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3710 | const ContentInfo* old_local_content, |
| 3711 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3712 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3713 | // If this is an offer then the m= section might be recycled. If the m= |
| 3714 | // section is being recycled (defined as: rejected in the current local or |
| 3715 | // remote description and not rejected in new description), dissociate the |
| 3716 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3717 | // and discard the mapping between the transceiver and its m= section index. |
| 3718 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3719 | old_remote_content)) { |
| 3720 | // We want to dissociate the transceiver that has the rejected mid. |
| 3721 | const std::string& old_mid = |
| 3722 | (old_local_content && old_local_content->rejected) |
| 3723 | ? old_local_content->name |
| 3724 | : old_remote_content->name; |
| 3725 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3726 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3727 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3728 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 3729 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3730 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3731 | } |
| 3732 | } |
| 3733 | const MediaContentDescription* media_desc = content.media_description(); |
| 3734 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3735 | if (source == cricket::CS_LOCAL) { |
| 3736 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3737 | // mapping between transceivers and m= section indices established when |
| 3738 | // creating the offer. |
| 3739 | if (!transceiver) { |
| 3740 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3741 | } |
| 3742 | if (!transceiver) { |
| 3743 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3744 | "Unknown transceiver"); |
| 3745 | } |
| 3746 | } else { |
| 3747 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3748 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3749 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 3750 | // When simulcast is requested, a transceiver cannot be associated because |
| 3751 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3752 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 3753 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3754 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3755 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3756 | } |
| 3757 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3758 | // recvonly direction. |
| 3759 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3760 | RTC_LOG(LS_INFO) << "Adding " |
| 3761 | << cricket::MediaTypeToString(media_desc->type()) |
| 3762 | << " transceiver for MID=" << content.name |
| 3763 | << " at i=" << mline_index |
| 3764 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 3765 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3766 | std::vector<RtpEncodingParameters> send_encodings = |
| 3767 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3768 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3769 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 3770 | std::string receiver_id; |
| 3771 | if (!media_desc->streams().empty()) { |
| 3772 | receiver_id = media_desc->streams()[0].id; |
| 3773 | } else { |
| 3774 | receiver_id = rtc::CreateRandomUuid(); |
| 3775 | } |
| 3776 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 3777 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3778 | transceiver->internal()->set_direction( |
| 3779 | RtpTransceiverDirection::kRecvOnly); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3780 | if (type == SdpType::kOffer) { |
| 3781 | transceiver_stable_states_by_transceivers_[transceiver] = |
| 3782 | TransceiverStableState(RtpTransceiverDirection::kRecvOnly, |
| 3783 | absl::nullopt, absl::nullopt, true); |
| 3784 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3785 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3786 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3787 | // This can happen when simulcast is not supported on the remote party. |
Amit Hilbuch | aabd036 | 2019-03-01 21:14:46 | [diff] [blame] | 3788 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 3789 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3790 | RTCError error = |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3791 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3792 | if (!error.ok()) { |
| 3793 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3794 | return std::move(error); |
| 3795 | } |
| 3796 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3797 | } |
| 3798 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3799 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3800 | LOG_AND_RETURN_ERROR( |
| 3801 | RTCErrorType::INVALID_PARAMETER, |
| 3802 | "Transceiver type does not match media description type."); |
| 3803 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3804 | if (media_desc->HasSimulcast()) { |
| 3805 | std::vector<SimulcastLayer> layers = |
| 3806 | source == cricket::CS_LOCAL |
| 3807 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3808 | : media_desc->simulcast_description() |
| 3809 | .receive_layers() |
| 3810 | .GetAllLayers(); |
| 3811 | RTCError error = UpdateSimulcastLayerStatusInSender( |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3812 | layers, transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3813 | if (!error.ok()) { |
| 3814 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3815 | return std::move(error); |
| 3816 | } |
| 3817 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3818 | if (type == SdpType::kOffer) { |
| 3819 | // Make sure we don't overwrite existing stable states and that the |
| 3820 | // state is really going to change when adding new record to the map. |
| 3821 | auto it = transceiver_stable_states_by_transceivers_.find(transceiver); |
| 3822 | if (it == transceiver_stable_states_by_transceivers_.end() && |
| 3823 | (transceiver->internal()->mid() != content.name || |
| 3824 | transceiver->internal()->mline_index() != mline_index)) { |
| 3825 | transceiver_stable_states_by_transceivers_[transceiver] = |
| 3826 | TransceiverStableState(transceiver->internal()->direction(), |
| 3827 | transceiver->internal()->mid(), |
| 3828 | transceiver->internal()->mline_index(), false); |
| 3829 | } |
| 3830 | } |
| 3831 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3832 | // Associate the found or created RtpTransceiver with the m= section by |
| 3833 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3834 | // section, and establish a mapping between the transceiver and the index of |
| 3835 | // the m= section. |
| 3836 | transceiver->internal()->set_mid(content.name); |
| 3837 | transceiver->internal()->set_mline_index(mline_index); |
| 3838 | return std::move(transceiver); |
| 3839 | } |
| 3840 | |
| 3841 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3842 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3843 | RTC_DCHECK(IsUnifiedPlan()); |
| 3844 | for (auto transceiver : transceivers_) { |
| 3845 | if (transceiver->mid() == mid) { |
| 3846 | return transceiver; |
| 3847 | } |
| 3848 | } |
| 3849 | return nullptr; |
| 3850 | } |
| 3851 | |
| 3852 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3853 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3854 | RTC_DCHECK(IsUnifiedPlan()); |
| 3855 | for (auto transceiver : transceivers_) { |
| 3856 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3857 | return transceiver; |
| 3858 | } |
| 3859 | } |
| 3860 | return nullptr; |
| 3861 | } |
| 3862 | |
| 3863 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3864 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3865 | cricket::MediaType media_type) const { |
| 3866 | RTC_DCHECK(IsUnifiedPlan()); |
| 3867 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3868 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3869 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3870 | // associated with any m= section and are not stopped, find the first such |
| 3871 | // RtpTransceiver. |
| 3872 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3873 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3874 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3875 | !transceiver->stopped()) { |
| 3876 | return transceiver; |
| 3877 | } |
| 3878 | } |
| 3879 | return nullptr; |
| 3880 | } |
| 3881 | |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 3882 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3883 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3884 | transceiver, |
| 3885 | const SessionDescriptionInterface* sdesc) const { |
| 3886 | RTC_DCHECK(transceiver); |
| 3887 | RTC_DCHECK(sdesc); |
| 3888 | if (IsUnifiedPlan()) { |
| 3889 | if (!transceiver->internal()->mid()) { |
| 3890 | // This transceiver is not associated with a media section yet. |
| 3891 | return nullptr; |
| 3892 | } |
| 3893 | return sdesc->description()->GetContentByName( |
| 3894 | *transceiver->internal()->mid()); |
| 3895 | } else { |
| 3896 | // Plan B only allows at most one audio and one video section, so use the |
| 3897 | // first media section of that type. |
| 3898 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3899 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 3900 | } |
| 3901 | } |
| 3902 | |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 3903 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 3904 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 3905 | return configuration_; |
| 3906 | } |
| 3907 | |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3908 | RTCError PeerConnection::SetConfiguration( |
| 3909 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 3910 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3911 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 3912 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3913 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3914 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 3915 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3916 | } |
| 3917 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 3918 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3919 | // size is not allowed, and changing the set of ICE servers will not result |
| 3920 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3921 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3922 | configuration_.ice_candidate_pool_size) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3923 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3924 | "Can't change candidate pool size after calling " |
| 3925 | "SetLocalDescription."); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 3926 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 3927 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 3928 | if (local_description() && |
| 3929 | configuration.use_media_transport != configuration_.use_media_transport) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3930 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3931 | "Can't change media_transport after calling " |
| 3932 | "SetLocalDescription."); |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | if (remote_description() && |
| 3936 | configuration.use_media_transport != configuration_.use_media_transport) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3937 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3938 | "Can't change media_transport after calling " |
| 3939 | "SetRemoteDescription."); |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 3940 | } |
| 3941 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 3942 | if (local_description() && |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 3943 | configuration.use_media_transport_for_data_channels != |
| 3944 | configuration_.use_media_transport_for_data_channels) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3945 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3946 | "Can't change media_transport_for_data_channels " |
| 3947 | "after calling SetLocalDescription."); |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | if (remote_description() && |
| 3951 | configuration.use_media_transport_for_data_channels != |
| 3952 | configuration_.use_media_transport_for_data_channels) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3953 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3954 | "Can't change media_transport_for_data_channels " |
| 3955 | "after calling SetRemoteDescription."); |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 3956 | } |
| 3957 | |
| 3958 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 3959 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3960 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3961 | "Can't change crypto_options after calling " |
| 3962 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 3963 | } |
| 3964 | |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 3965 | if (local_description() && configuration.use_datagram_transport != |
| 3966 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3967 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3968 | "Can't change use_datagram_transport " |
| 3969 | "after calling SetLocalDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | if (remote_description() && configuration.use_datagram_transport != |
| 3973 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3974 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3975 | "Can't change use_datagram_transport " |
| 3976 | "after calling SetRemoteDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 3977 | } |
| 3978 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 3979 | if (local_description() && |
| 3980 | configuration.use_datagram_transport_for_data_channels != |
| 3981 | configuration_.use_datagram_transport_for_data_channels) { |
| 3982 | LOG_AND_RETURN_ERROR( |
| 3983 | RTCErrorType::INVALID_MODIFICATION, |
| 3984 | "Can't change use_datagram_transport_for_data_channels " |
| 3985 | "after calling SetLocalDescription."); |
| 3986 | } |
| 3987 | |
| 3988 | if (remote_description() && |
| 3989 | configuration.use_datagram_transport_for_data_channels != |
| 3990 | configuration_.use_datagram_transport_for_data_channels) { |
| 3991 | LOG_AND_RETURN_ERROR( |
| 3992 | RTCErrorType::INVALID_MODIFICATION, |
| 3993 | "Can't change use_datagram_transport_for_data_channels " |
| 3994 | "after calling SetRemoteDescription."); |
| 3995 | } |
| 3996 | |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 3997 | if (local_description() && |
| 3998 | configuration.use_datagram_transport_for_data_channels_receive_only != |
| 3999 | configuration_ |
| 4000 | .use_datagram_transport_for_data_channels_receive_only) { |
| 4001 | LOG_AND_RETURN_ERROR( |
| 4002 | RTCErrorType::INVALID_MODIFICATION, |
| 4003 | "Can't change use_datagram_transport_for_data_channels_receive_only " |
| 4004 | "after calling SetLocalDescription."); |
| 4005 | } |
| 4006 | |
| 4007 | if (remote_description() && |
| 4008 | configuration.use_datagram_transport_for_data_channels_receive_only != |
| 4009 | configuration_ |
| 4010 | .use_datagram_transport_for_data_channels_receive_only) { |
| 4011 | LOG_AND_RETURN_ERROR( |
| 4012 | RTCErrorType::INVALID_MODIFICATION, |
| 4013 | "Can't change use_datagram_transport_for_data_channels_receive_only " |
| 4014 | "after calling SetRemoteDescription."); |
| 4015 | } |
| 4016 | |
Piotr (Peter) Slatala | 37227be | 2018-11-21 15:42:22 | [diff] [blame] | 4017 | if (configuration.use_media_transport_for_data_channels || |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 4018 | configuration.use_media_transport || |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 4019 | (configuration.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 4020 | *configuration.use_datagram_transport) || |
| 4021 | (configuration.use_datagram_transport_for_data_channels && |
| 4022 | *configuration.use_datagram_transport_for_data_channels)) { |
Piotr (Peter) Slatala | 37227be | 2018-11-21 15:42:22 | [diff] [blame] | 4023 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 4024 | << "Media transport requires MaxBundle policy."; |
| 4025 | } |
| 4026 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4027 | // The simplest (and most future-compatible) way to tell if the config was |
| 4028 | // modified in an invalid way is to copy each property we do support |
| 4029 | // modifying, then use operator==. There are far more properties we don't |
| 4030 | // support modifying than those we do, and more could be added. |
| 4031 | RTCConfiguration modified_config = configuration_; |
| 4032 | modified_config.servers = configuration.servers; |
| 4033 | modified_config.type = configuration.type; |
| 4034 | modified_config.ice_candidate_pool_size = |
| 4035 | configuration.ice_candidate_pool_size; |
| 4036 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 4037 | modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy; |
Qingsi Wang | bca1485 | 2019-06-26 21:56:02 | [diff] [blame] | 4038 | modified_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 4039 | configuration.surface_ice_candidates_on_ice_transport_type_changed; |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 4040 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 4041 | modified_config.ice_check_interval_strong_connectivity = |
| 4042 | configuration.ice_check_interval_strong_connectivity; |
| 4043 | modified_config.ice_check_interval_weak_connectivity = |
| 4044 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 4045 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 4046 | modified_config.ice_unwritable_min_checks = |
| 4047 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 4048 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 4049 | modified_config.stun_candidate_keepalive_interval = |
| 4050 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 4051 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 4052 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 4053 | modified_config.active_reset_srtp_params = |
| 4054 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 4055 | modified_config.use_media_transport = configuration.use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 4056 | modified_config.use_media_transport_for_data_channels = |
| 4057 | configuration.use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 4058 | modified_config.use_datagram_transport = configuration.use_datagram_transport; |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 4059 | modified_config.use_datagram_transport_for_data_channels = |
| 4060 | configuration.use_datagram_transport_for_data_channels; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 4061 | modified_config.use_datagram_transport_for_data_channels_receive_only = |
| 4062 | configuration.use_datagram_transport_for_data_channels_receive_only; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 4063 | modified_config.turn_logging_id = configuration.turn_logging_id; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 4064 | modified_config.allow_codec_switching = configuration.allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4065 | if (configuration != modified_config) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4066 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 4067 | "Modifying the configuration in an unsupported way."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4068 | } |
| 4069 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 4070 | // Validate the modified configuration. |
| 4071 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 4072 | if (!validate_error.ok()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4073 | return validate_error; |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 4074 | } |
| 4075 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4076 | // Note that this isn't possible through chromium, since it's an unsigned |
| 4077 | // short in WebIDL. |
| 4078 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 10:34:17 | [diff] [blame] | 4079 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4080 | return RTCError(RTCErrorType::INVALID_RANGE); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | // Parse ICE servers before hopping to network thread. |
| 4084 | cricket::ServerAddresses stun_servers; |
| 4085 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 4086 | RTCErrorType parse_error = |
| 4087 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 4088 | if (parse_error != RTCErrorType::NONE) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4089 | return RTCError(parse_error); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4090 | } |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 4091 | // Add the turn logging id to all turn servers |
| 4092 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 4093 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 4094 | } |
| 4095 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4096 | // Note if STUN or TURN servers were supplied. |
| 4097 | if (!stun_servers.empty()) { |
| 4098 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 4099 | } |
| 4100 | if (!turn_servers.empty()) { |
| 4101 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 4102 | } |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4103 | |
| 4104 | // In theory this shouldn't fail. |
| 4105 | if (!network_thread()->Invoke<bool>( |
| 4106 | RTC_FROM_HERE, |
| 4107 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 4108 | stun_servers, turn_servers, modified_config.type, |
| 4109 | modified_config.ice_candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 4110 | modified_config.GetTurnPortPrunePolicy(), |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 4111 | modified_config.turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4112 | modified_config.stun_candidate_keepalive_interval, |
| 4113 | static_cast<bool>(local_description())))) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4114 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4115 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4116 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 4117 | |
deadbeef | d1a38b5 | 2016-12-10 21:15:33 | [diff] [blame] | 4118 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 4119 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 4120 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4121 | if (modified_config.servers != configuration_.servers || |
| 4122 | modified_config.type != configuration_.type || |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 4123 | modified_config.GetTurnPortPrunePolicy() != |
| 4124 | configuration_.GetTurnPortPrunePolicy()) { |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4125 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 21:15:33 | [diff] [blame] | 4126 | } |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 4127 | |
Qingsi Wang | 9c98f0c | 2018-02-15 23:10:59 | [diff] [blame] | 4128 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 4129 | |
| 4130 | use_datagram_transport_ = datagram_transport_config_.enabled && |
| 4131 | modified_config.use_datagram_transport.value_or( |
| 4132 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 4133 | use_datagram_transport_for_data_channels_ = |
| 4134 | datagram_transport_data_channel_config_.enabled && |
| 4135 | modified_config.use_datagram_transport_for_data_channels.value_or( |
| 4136 | datagram_transport_data_channel_config_.default_value); |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 4137 | use_datagram_transport_for_data_channels_receive_only_ = |
| 4138 | modified_config.use_datagram_transport_for_data_channels_receive_only |
| 4139 | .value_or(datagram_transport_data_channel_config_.receive_only); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 21:31:15 | [diff] [blame] | 4140 | transport_controller_->SetMediaTransportSettings( |
| 4141 | modified_config.use_media_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 4142 | modified_config.use_media_transport_for_data_channels, |
Bjorn A Mellem | 7da4e56 | 2019-09-26 18:02:11 | [diff] [blame] | 4143 | use_datagram_transport_, use_datagram_transport_for_data_channels_, |
| 4144 | use_datagram_transport_for_data_channels_receive_only_); |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 4145 | |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 4146 | if (configuration_.active_reset_srtp_params != |
| 4147 | modified_config.active_reset_srtp_params) { |
| 4148 | transport_controller_->SetActiveResetSrtpParams( |
| 4149 | modified_config.active_reset_srtp_params); |
| 4150 | } |
| 4151 | |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 4152 | if (modified_config.allow_codec_switching.has_value()) { |
philipel | 01294f0 | 2019-11-14 12:03:25 | [diff] [blame] | 4153 | cricket::VideoMediaChannel* video_channel = video_media_channel(); |
| 4154 | if (video_channel) { |
| 4155 | video_channel->SetVideoCodecSwitchingEnabled( |
| 4156 | *modified_config.allow_codec_switching); |
| 4157 | } |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 4158 | } |
| 4159 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4160 | configuration_ = modified_config; |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 4161 | use_media_transport_ = configuration.use_media_transport; |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4162 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 4163 | } |
| 4164 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4165 | bool PeerConnection::AddIceCandidate( |
| 4166 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 4167 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 4168 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4169 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4170 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4171 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4172 | return false; |
| 4173 | } |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4174 | |
| 4175 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4176 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 4177 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4178 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4179 | return false; |
| 4180 | } |
| 4181 | |
| 4182 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4183 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4184 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4185 | return false; |
| 4186 | } |
| 4187 | |
| 4188 | bool valid = false; |
| 4189 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 4190 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4191 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4192 | return false; |
| 4193 | } |
| 4194 | |
| 4195 | // Add this candidate to the remote session description. |
| 4196 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4197 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4198 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4199 | return false; |
| 4200 | } |
| 4201 | |
| 4202 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4203 | bool result = UseCandidate(ice_candidate); |
| 4204 | if (result) { |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 4205 | NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED); |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4206 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 4207 | } else { |
| 4208 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 4209 | } |
| 4210 | return result; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4211 | } else { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4212 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4213 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4214 | return true; |
| 4215 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4216 | } |
| 4217 | |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 4218 | void PeerConnection::AddIceCandidate( |
| 4219 | std::unique_ptr<IceCandidateInterface> candidate, |
| 4220 | std::function<void(RTCError)> callback) { |
| 4221 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4222 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 4223 | // this operation will be queued to be invoked, otherwise the contents of the |
| 4224 | // lambda will execute immediately. |
| 4225 | operations_chain_->ChainOperation( |
| 4226 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 4227 | candidate = std::move(candidate), callback = std::move(callback)]( |
| 4228 | std::function<void()> operations_chain_callback) { |
| 4229 | if (!this_weak_ptr) { |
| 4230 | operations_chain_callback(); |
| 4231 | callback(RTCError( |
| 4232 | RTCErrorType::INVALID_STATE, |
| 4233 | "AddIceCandidate failed because the session was shut down")); |
| 4234 | return; |
| 4235 | } |
| 4236 | if (!this_weak_ptr->AddIceCandidate(candidate.get())) { |
| 4237 | operations_chain_callback(); |
| 4238 | // Fail with an error type and message consistent with Chromium. |
| 4239 | // TODO(hbos): Fail with error types according to spec. |
| 4240 | callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 4241 | "Error processing ICE candidate")); |
| 4242 | return; |
| 4243 | } |
| 4244 | operations_chain_callback(); |
| 4245 | callback(RTCError::OK()); |
| 4246 | }); |
| 4247 | } |
| 4248 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4249 | bool PeerConnection::RemoveIceCandidates( |
| 4250 | const std::vector<cricket::Candidate>& candidates) { |
| 4251 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4252 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4253 | if (IsClosed()) { |
| 4254 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 4255 | return false; |
| 4256 | } |
| 4257 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4258 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4259 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 4260 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4261 | return false; |
| 4262 | } |
| 4263 | |
| 4264 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4265 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4266 | return false; |
| 4267 | } |
| 4268 | |
| 4269 | size_t number_removed = |
| 4270 | mutable_remote_description()->RemoveCandidates(candidates); |
| 4271 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4272 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4273 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 4274 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4275 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 4279 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 4280 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4281 | RTC_LOG(LS_ERROR) |
| 4282 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 4283 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4284 | } |
| 4285 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4286 | } |
| 4287 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4288 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 4289 | if (!worker_thread()->IsCurrent()) { |
| 4290 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 4291 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4292 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 4293 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4294 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4295 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 4296 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 4297 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4298 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 4299 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4300 | "min_bitrate_bps <= 0"); |
| 4301 | } |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4302 | if (has_start) { |
| 4303 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4304 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4305 | "start_bitrate_bps < min_bitrate_bps"); |
| 4306 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4307 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4308 | "curent_bitrate_bps < 0"); |
| 4309 | } |
| 4310 | } |
| 4311 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 4312 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4313 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4314 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4315 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 4316 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4317 | "max_bitrate_bps < min_bitrate_bps"); |
| 4318 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 4319 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4320 | "max_bitrate_bps < 0"); |
| 4321 | } |
| 4322 | } |
| 4323 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4324 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 17:31:54 | [diff] [blame] | 4325 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4326 | |
| 4327 | return RTCError::OK(); |
| 4328 | } |
| 4329 | |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 4330 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 4331 | if (!worker_thread()->IsCurrent()) { |
| 4332 | worker_thread()->Invoke<void>( |
| 4333 | RTC_FROM_HERE, |
| 4334 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 4335 | return; |
| 4336 | } |
| 4337 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 4338 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 4339 | audio_state->SetPlayout(playout); |
| 4340 | } |
| 4341 | |
| 4342 | void PeerConnection::SetAudioRecording(bool recording) { |
| 4343 | if (!worker_thread()->IsCurrent()) { |
| 4344 | worker_thread()->Invoke<void>( |
| 4345 | RTC_FROM_HERE, |
| 4346 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 4347 | return; |
| 4348 | } |
| 4349 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 4350 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 4351 | audio_state->SetRecording(recording); |
| 4352 | } |
| 4353 | |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 4354 | std::unique_ptr<rtc::SSLCertificate> |
| 4355 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 4356 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 4357 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 4358 | return nullptr; |
| 4359 | } |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 4360 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 4361 | } |
| 4362 | |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4363 | std::unique_ptr<rtc::SSLCertChain> |
| 4364 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 4365 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 4366 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 4367 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4368 | return nullptr; |
| 4369 | } |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4370 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 4371 | audio_transceiver->internal()->channel()->transport_name()); |
| 4372 | } |
| 4373 | |
| 4374 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4375 | PeerConnection::GetFirstAudioTransceiver() const { |
| 4376 | for (auto transceiver : transceivers_) { |
| 4377 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4378 | return transceiver; |
| 4379 | } |
| 4380 | } |
| 4381 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4382 | } |
| 4383 | |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 4384 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 4385 | int64_t output_period_ms) { |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 4386 | return worker_thread()->Invoke<bool>( |
Danil Chapovalov | 116ffe7 | 2019-09-05 08:21:11 | [diff] [blame] | 4387 | RTC_FROM_HERE, |
| 4388 | [this, output = std::move(output), output_period_ms]() mutable { |
| 4389 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 4390 | }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 4391 | } |
| 4392 | |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 4393 | bool PeerConnection::StartRtcEventLog( |
| 4394 | std::unique_ptr<RtcEventLogOutput> output) { |
Niels Möller | 695cf6a | 2019-05-13 10:27:23 | [diff] [blame] | 4395 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 4396 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 4397 | output_period_ms = 5000; |
| 4398 | } |
| 4399 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 4400 | } |
| 4401 | |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 4402 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 4403 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 4404 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 4405 | } |
| 4406 | |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 4407 | rtc::scoped_refptr<DtlsTransportInterface> |
| 4408 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4409 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 4410 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 4411 | } |
| 4412 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 4413 | rtc::scoped_refptr<DtlsTransport> |
| 4414 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4415 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 4416 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 4417 | } |
| 4418 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 4419 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 4420 | const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4421 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 4422 | if (!sctp_mid_) { |
| 4423 | return nullptr; |
| 4424 | } |
| 4425 | return transport_controller_->GetSctpTransport(*sctp_mid_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 4426 | } |
| 4427 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4428 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4429 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4430 | return pending_local_description_ ? pending_local_description_.get() |
| 4431 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4435 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4436 | return pending_remote_description_ ? pending_remote_description_.get() |
| 4437 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4438 | } |
| 4439 | |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4440 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 4441 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4442 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4443 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 4447 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4448 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4449 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 4453 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4454 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4455 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 4459 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4460 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4461 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4462 | } |
| 4463 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4464 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 4465 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 4466 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4467 | // Update stats here so that we have the most recent stats for tracks and |
| 4468 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 4469 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4470 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4471 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4472 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 4473 | |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 4474 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 4475 | transceiver->Stop(); |
| 4476 | } |
Steve Anton | 25cfeb9 | 2018-04-26 18:44:00 | [diff] [blame] | 4477 | |
| 4478 | // Ensure that all asynchronous stats requests are completed before destroying |
| 4479 | // the transport controller below. |
| 4480 | if (stats_collector_) { |
| 4481 | stats_collector_->WaitForPendingRequest(); |
| 4482 | } |
| 4483 | |
| 4484 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 4485 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 4486 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4487 | |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 4488 | // The event log is used in the transport controller, which must be outlived |
| 4489 | // by the former. CreateOffer by the peer connection is implemented |
| 4490 | // asynchronously and if the peer connection is closed without resetting the |
| 4491 | // WebRTC session description factory, the session description factory would |
| 4492 | // call the transport controller. |
| 4493 | webrtc_session_desc_factory_.reset(); |
| 4494 | transport_controller_.reset(); |
| 4495 | |
deadbeef | 42a4263 | 2017-03-10 23:18:00 | [diff] [blame] | 4496 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 4497 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 4498 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 4499 | |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 4500 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 4501 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 4502 | call_.reset(); |
| 4503 | // The event log must outlive call (and any other object that uses it). |
| 4504 | event_log_.reset(); |
| 4505 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4506 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4507 | // The .h file says that observer can be discarded after close() returns. |
| 4508 | // Make sure this is true. |
| 4509 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4510 | } |
| 4511 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4512 | void PeerConnection::OnMessage(rtc::Message* msg) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 4513 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4514 | switch (msg->message_id) { |
| 4515 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 4516 | SetSessionDescriptionMsg* param = |
| 4517 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4518 | param->observer->OnSuccess(); |
| 4519 | delete param; |
| 4520 | break; |
| 4521 | } |
| 4522 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 4523 | SetSessionDescriptionMsg* param = |
| 4524 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4525 | param->observer->OnFailure(std::move(param->error)); |
| 4526 | delete param; |
| 4527 | break; |
| 4528 | } |
| 4529 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 4530 | CreateSessionDescriptionMsg* param = |
| 4531 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 4532 | param->observer->OnFailure(std::move(param->error)); |
| 4533 | delete param; |
| 4534 | break; |
| 4535 | } |
| 4536 | case MSG_GETSTATS: { |
| 4537 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 4538 | StatsReports reports; |
| 4539 | stats_->GetStats(param->track, &reports); |
| 4540 | param->observer->OnComplete(reports); |
| 4541 | delete param; |
| 4542 | break; |
| 4543 | } |
| 4544 | case MSG_FREE_DATACHANNELS: { |
| 4545 | sctp_data_channels_to_free_.clear(); |
| 4546 | break; |
| 4547 | } |
| 4548 | case MSG_REPORT_USAGE_PATTERN: { |
| 4549 | ReportUsagePattern(); |
| 4550 | break; |
| 4551 | } |
| 4552 | default: |
| 4553 | RTC_NOTREACHED() << "Not implemented"; |
| 4554 | break; |
| 4555 | } |
| 4556 | } |
| 4557 | |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 4558 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 4559 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4560 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 4561 | GetAudioTransceiver()->internal()->channel()); |
| 4562 | if (voice_channel) { |
| 4563 | return voice_channel->media_channel(); |
| 4564 | } else { |
| 4565 | return nullptr; |
| 4566 | } |
| 4567 | } |
| 4568 | |
| 4569 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 4570 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4571 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 4572 | GetVideoTransceiver()->internal()->channel()); |
| 4573 | if (video_channel) { |
| 4574 | return video_channel->media_channel(); |
| 4575 | } else { |
| 4576 | return nullptr; |
| 4577 | } |
| 4578 | } |
| 4579 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4580 | void PeerConnection::CreateAudioReceiver( |
| 4581 | MediaStreamInterface* stream, |
| 4582 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 12:41:51 | [diff] [blame] | 4583 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4584 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 4585 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4586 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4587 | auto* audio_receiver = new AudioRtpReceiver( |
| 4588 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4589 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 23:23:05 | [diff] [blame] | 4590 | if (remote_sender_info.sender_id == kDefaultAudioSenderId) { |
| 4591 | audio_receiver->SetupUnsignaledMediaChannel(); |
| 4592 | } else { |
| 4593 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4594 | } |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4595 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4596 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4597 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 20:38:12 | [diff] [blame] | 4598 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4599 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4600 | } |
| 4601 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4602 | void PeerConnection::CreateVideoReceiver( |
| 4603 | MediaStreamInterface* stream, |
| 4604 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 12:41:51 | [diff] [blame] | 4605 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4606 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 4607 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4608 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4609 | auto* video_receiver = new VideoRtpReceiver( |
| 4610 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4611 | video_receiver->SetMediaChannel(video_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 23:23:05 | [diff] [blame] | 4612 | if (remote_sender_info.sender_id == kDefaultVideoSenderId) { |
| 4613 | video_receiver->SetupUnsignaledMediaChannel(); |
| 4614 | } else { |
| 4615 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4616 | } |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4617 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4618 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4619 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 20:38:12 | [diff] [blame] | 4620 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4621 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4622 | } |
| 4623 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 4624 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 4625 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4626 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4627 | const RtpSenderInfo& remote_sender_info) { |
| 4628 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 4629 | if (!receiver) { |
| 4630 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 4631 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4632 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 4633 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4634 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4635 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 4636 | } else { |
| 4637 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 4638 | } |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4639 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4640 | } |
| 4641 | |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4642 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 4643 | MediaStreamInterface* stream) { |
| 4644 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4645 | RTC_DCHECK(track); |
| 4646 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4647 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4648 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4649 | // We already have a sender for this track, so just change the stream_id |
| 4650 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 4651 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4652 | return; |
| 4653 | } |
| 4654 | |
| 4655 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4656 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 4657 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4658 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4659 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4660 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 4661 | // which will connect the sender to the underlying transport. This can |
| 4662 | // occur if a local session description that contains the ID of the sender |
| 4663 | // is set before AddStream is called. It can also occur if the local |
| 4664 | // session description is not changed and RemoveStream is called, and |
| 4665 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4666 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4667 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4668 | if (sender_info) { |
| 4669 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 4674 | // indefinitely, when we have unified plan SDP. |
| 4675 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 4676 | MediaStreamInterface* stream) { |
| 4677 | RTC_DCHECK(!IsClosed()); |
| 4678 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4679 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4680 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4681 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4682 | return; |
| 4683 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4684 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 4688 | MediaStreamInterface* stream) { |
| 4689 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4690 | RTC_DCHECK(track); |
| 4691 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4692 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4693 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4694 | // We already have a sender for this track, so just change the stream_id |
| 4695 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 4696 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4697 | return; |
| 4698 | } |
| 4699 | |
| 4700 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4701 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 4702 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4703 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4704 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 4705 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4706 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4707 | if (sender_info) { |
| 4708 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 4713 | MediaStreamInterface* stream) { |
| 4714 | RTC_DCHECK(!IsClosed()); |
| 4715 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4716 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4717 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4718 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4719 | return; |
| 4720 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4721 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4722 | } |
| 4723 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4724 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4725 | if (ice_connection_state_ == new_state) { |
| 4726 | return; |
| 4727 | } |
| 4728 | |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 4729 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4730 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4731 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 4732 | return; |
| 4733 | } |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4734 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4735 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 4736 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4737 | RTC_DCHECK(ice_connection_state_ != |
| 4738 | PeerConnectionInterface::kIceConnectionClosed); |
| 4739 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4740 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4741 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4742 | } |
| 4743 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4744 | void PeerConnection::SetStandardizedIceConnectionState( |
| 4745 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 4746 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4747 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4751 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 4755 | << standardized_ice_connection_state_ << " => " << new_state; |
| 4756 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4757 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 10:25:14 | [diff] [blame] | 4758 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4759 | } |
| 4760 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4761 | void PeerConnection::SetConnectionState( |
| 4762 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4763 | if (connection_state_ == new_state) |
| 4764 | return; |
| 4765 | if (IsClosed()) |
| 4766 | return; |
| 4767 | connection_state_ = new_state; |
| 4768 | Observer()->OnConnectionChange(new_state); |
| 4769 | } |
| 4770 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4771 | void PeerConnection::OnIceGatheringChange( |
| 4772 | PeerConnectionInterface::IceGatheringState new_state) { |
| 4773 | if (IsClosed()) { |
| 4774 | return; |
| 4775 | } |
| 4776 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4777 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4778 | } |
| 4779 | |
jbauch | 81bf7b0 | 2017-03-25 15:31:12 | [diff] [blame] | 4780 | void PeerConnection::OnIceCandidate( |
| 4781 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4782 | if (IsClosed()) { |
| 4783 | return; |
| 4784 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 4785 | ReportIceCandidateCollected(candidate->candidate()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4786 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4787 | } |
| 4788 | |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 4789 | void PeerConnection::OnIceCandidateError(const std::string& host_candidate, |
| 4790 | const std::string& url, |
| 4791 | int error_code, |
| 4792 | const std::string& error_text) { |
| 4793 | if (IsClosed()) { |
| 4794 | return; |
| 4795 | } |
| 4796 | Observer()->OnIceCandidateError(host_candidate, url, error_code, error_text); |
| 4797 | } |
| 4798 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4799 | void PeerConnection::OnIceCandidatesRemoved( |
| 4800 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4801 | if (IsClosed()) { |
| 4802 | return; |
| 4803 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4804 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4805 | } |
| 4806 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 4807 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 4808 | const cricket::CandidatePairChangeEvent& event) { |
| 4809 | if (IsClosed()) { |
| 4810 | return; |
| 4811 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 4812 | |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 4813 | if (event.selected_candidate_pair.local_candidate().type() == |
| 4814 | LOCAL_PORT_TYPE && |
| 4815 | event.selected_candidate_pair.remote_candidate().type() == |
| 4816 | LOCAL_PORT_TYPE) { |
| 4817 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 4818 | } |
| 4819 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 4820 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 4821 | } |
| 4822 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4823 | void PeerConnection::ChangeSignalingState( |
| 4824 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4825 | if (signaling_state_ == signaling_state) { |
| 4826 | return; |
| 4827 | } |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4828 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 4829 | << GetSignalingStateString(signaling_state_) |
| 4830 | << " New state: " |
| 4831 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4832 | signaling_state_ = signaling_state; |
| 4833 | if (signaling_state == kClosed) { |
| 4834 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4835 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4836 | standardized_ice_connection_state_ = |
| 4837 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4838 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 4839 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4840 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 4841 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4842 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4843 | } |
| 4844 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4845 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4846 | } |
| 4847 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4848 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4849 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4850 | if (IsClosed()) { |
| 4851 | return; |
| 4852 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4853 | AddAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4854 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4855 | } |
| 4856 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4857 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4858 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4859 | if (IsClosed()) { |
| 4860 | return; |
| 4861 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4862 | RemoveAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4863 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4867 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4868 | if (IsClosed()) { |
| 4869 | return; |
| 4870 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4871 | AddVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4872 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4876 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4877 | if (IsClosed()) { |
| 4878 | return; |
| 4879 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4880 | RemoveVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4881 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4882 | } |
| 4883 | |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 4884 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4885 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4886 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4887 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4888 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 4889 | } |
| 4890 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4891 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4892 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4893 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 4894 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4895 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4896 | msg->error = std::move(error); |
| 4897 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4898 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4902 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 4903 | RTCError error) { |
| 4904 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4905 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4906 | msg->error = std::move(error); |
| 4907 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4908 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4909 | } |
| 4910 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4911 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4912 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4913 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4914 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4915 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4916 | if (IsUnifiedPlan()) { |
| 4917 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4918 | } else { |
| 4919 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 4920 | } |
| 4921 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4922 | // Intentionally unset the data channel type for RTP data channel with the |
| 4923 | // second condition. Otherwise the RTP data channels would be successfully |
| 4924 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4925 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4926 | // people won't try to use them. |
| 4927 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4928 | session_options->data_channel_type = data_channel_type(); |
| 4929 | } |
| 4930 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4931 | // Apply ICE restart flag and renomination flag. |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 4932 | bool ice_restart = offer_answer_options.ice_restart || |
| 4933 | local_ice_credentials_to_replace_->HasIceCredentials(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4934 | for (auto& options : session_options->media_description_options) { |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 4935 | options.transport_options.ice_restart = ice_restart; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4936 | options.transport_options.enable_ice_renomination = |
| 4937 | configuration_.enable_ice_renomination; |
| 4938 | } |
| 4939 | |
| 4940 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 4941 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 4942 | session_options->pooled_ice_credentials = |
| 4943 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4944 | RTC_FROM_HERE, |
| 4945 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4946 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 4947 | session_options->offer_extmap_allow_mixed = |
| 4948 | configuration_.offer_extmap_allow_mixed; |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 4949 | |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 4950 | if (configuration_.use_media_transport || |
| 4951 | configuration_.use_media_transport_for_data_channels) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 4952 | session_options->media_transport_settings = |
| 4953 | transport_controller_->GenerateOrGetLastMediaTransportOffer(); |
| 4954 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 4955 | |
| 4956 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 4957 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 4958 | for (auto& options : session_options->media_description_options) { |
Bjorn A Mellem | 8e1343a | 2019-09-30 22:12:47 | [diff] [blame] | 4959 | absl::optional<cricket::OpaqueTransportParameters> params = |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 4960 | transport_controller_->GetTransportParameters(options.mid); |
Bjorn A Mellem | 8e1343a | 2019-09-30 22:12:47 | [diff] [blame] | 4961 | if (!params) { |
| 4962 | continue; |
| 4963 | } |
| 4964 | options.transport_options.opaque_parameters = params; |
| 4965 | if ((use_datagram_transport_ && |
| 4966 | (options.type == cricket::MEDIA_TYPE_AUDIO || |
| 4967 | options.type == cricket::MEDIA_TYPE_VIDEO)) || |
| 4968 | (use_datagram_transport_for_data_channels_ && |
| 4969 | options.type == cricket::MEDIA_TYPE_DATA)) { |
| 4970 | options.alt_protocol = params->protocol; |
| 4971 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 4972 | } |
| 4973 | } |
| 4974 | |
Harald Alvestrand | 4aa1192 | 2019-05-14 20:00:01 | [diff] [blame] | 4975 | // Allow fallback for using obsolete SCTP syntax. |
| 4976 | // Note that the default in |session_options| is true, while |
| 4977 | // the default in |options| is false. |
| 4978 | session_options->use_obsolete_sctp_sdp = |
| 4979 | offer_answer_options.use_obsolete_sctp_sdp; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4983 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4984 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4985 | // Figure out transceiver directional preferences. |
| 4986 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4987 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4988 | |
| 4989 | // By default, generate sendrecv/recvonly m= sections. |
| 4990 | bool recv_audio = true; |
| 4991 | bool recv_video = true; |
| 4992 | |
| 4993 | // By default, only offer a new m= section if we have media to send with it. |
| 4994 | bool offer_new_audio_description = send_audio; |
| 4995 | bool offer_new_video_description = send_video; |
| 4996 | bool offer_new_data_description = HasDataChannels(); |
| 4997 | |
| 4998 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4999 | if (offer_answer_options.offer_to_receive_audio != |
| 5000 | RTCOfferAnswerOptions::kUndefined) { |
| 5001 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5002 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5003 | offer_new_audio_description || |
| 5004 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5005 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5006 | if (offer_answer_options.offer_to_receive_video != |
| 5007 | RTCOfferAnswerOptions::kUndefined) { |
| 5008 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5009 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5010 | offer_new_video_description || |
| 5011 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5012 | } |
| 5013 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5014 | absl::optional<size_t> audio_index; |
| 5015 | absl::optional<size_t> video_index; |
| 5016 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5017 | // If a current description exists, generate m= sections in the same order, |
| 5018 | // using the first audio/video/data section that appears and rejecting |
| 5019 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5020 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5021 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5022 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 5023 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 5024 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 5025 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5026 | } |
| 5027 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5028 | // Add audio/video/data m= sections to the end if needed. |
| 5029 | if (!audio_index && offer_new_audio_description) { |
| 5030 | session_options->media_description_options.push_back( |
| 5031 | cricket::MediaDescriptionOptions( |
| 5032 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 5033 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 5034 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5035 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 20:14:45 | [diff] [blame] | 5036 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5037 | if (!video_index && offer_new_video_description) { |
| 5038 | session_options->media_description_options.push_back( |
| 5039 | cricket::MediaDescriptionOptions( |
| 5040 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 5041 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 5042 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5043 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 20:14:45 | [diff] [blame] | 5044 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5045 | if (!data_index && offer_new_data_description) { |
| 5046 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5047 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5048 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 5052 | !audio_index ? nullptr |
| 5053 | : &session_options->media_description_options[*audio_index]; |
| 5054 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 5055 | !video_index ? nullptr |
| 5056 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5057 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 5058 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 5059 | audio_media_description_options, |
| 5060 | video_media_description_options, |
| 5061 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5062 | } |
| 5063 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5064 | static cricket::MediaDescriptionOptions |
| 5065 | GetMediaDescriptionOptionsForTransceiver( |
| 5066 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5067 | transceiver, |
| 5068 | const std::string& mid) { |
| 5069 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5070 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5071 | transceiver->stopped()); |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 5072 | media_description_options.codec_preferences = |
| 5073 | transceiver->codec_preferences(); |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 5074 | // This behavior is specified in JSEP. The gist is that: |
| 5075 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 5076 | // sendrecv. |
| 5077 | // 2. If the MSID is included, then it must be included in any subsequent |
| 5078 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5079 | if (transceiver->stopped() || |
| 5080 | (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 5081 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 5082 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 5083 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5084 | |
| 5085 | cricket::SenderOptions sender_options; |
| 5086 | sender_options.track_id = transceiver->sender()->id(); |
| 5087 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 5088 | |
| 5089 | // The following sets up RIDs and Simulcast. |
| 5090 | // RIDs are included if Simulcast is requested or if any RID was specified. |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 5091 | RtpParameters send_parameters = |
| 5092 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5093 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 5094 | send_parameters.encodings.end(), |
| 5095 | [](const RtpEncodingParameters& encoding) { |
| 5096 | return !encoding.rid.empty(); |
| 5097 | }); |
| 5098 | |
Amit Hilbuch | b7446ed | 2019-01-28 20:25:25 | [diff] [blame] | 5099 | std::vector<RidDescription> send_rids; |
| 5100 | SimulcastLayerList send_layers; |
| 5101 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 5102 | if (encoding.rid.empty()) { |
| 5103 | continue; |
| 5104 | } |
| 5105 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 5106 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 5107 | } |
| 5108 | |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5109 | if (has_rids) { |
| 5110 | sender_options.rids = send_rids; |
| 5111 | } |
| 5112 | |
| 5113 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5114 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 5115 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 5116 | // simulcast, or simulcast is acheived by munging the SDP. |
| 5117 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 5118 | media_description_options.sender_options.push_back(sender_options); |
| 5119 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5120 | return media_description_options; |
| 5121 | } |
| 5122 | |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5123 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 5124 | static const ContentInfo* GetContentByIndex( |
| 5125 | const SessionDescriptionInterface* sdesc, |
| 5126 | size_t i) { |
| 5127 | if (!sdesc) { |
| 5128 | return nullptr; |
| 5129 | } |
| 5130 | const ContentInfos& contents = sdesc->description()->contents(); |
| 5131 | return (i < contents.size() ? &contents[i] : nullptr); |
| 5132 | } |
| 5133 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5134 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 5135 | const RTCOfferAnswerOptions& offer_answer_options, |
| 5136 | cricket::MediaSessionOptions* session_options) { |
| 5137 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 5138 | // Offers) and 5.2.2 (Subsequent Offers). |
| 5139 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 5140 | const ContentInfos no_infos; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5141 | const ContentInfos& local_contents = |
| 5142 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 5143 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5144 | const ContentInfos& remote_contents = |
| 5145 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 5146 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5147 | // The mline indices that can be recycled. New transceivers should reuse these |
| 5148 | // slots first. |
| 5149 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5150 | // First, go through each media section that exists in either the local or |
| 5151 | // remote description and generate a media section in this offer for the |
| 5152 | // associated transceiver. If a media section can be recycled, generate a |
| 5153 | // default, rejected media section here that can be later overwritten. |
| 5154 | for (size_t i = 0; |
| 5155 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 5156 | // Either |local_content| or |remote_content| is non-null. |
| 5157 | const ContentInfo* local_content = |
| 5158 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5159 | const ContentInfo* current_local_content = |
| 5160 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5161 | const ContentInfo* remote_content = |
| 5162 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5163 | const ContentInfo* current_remote_content = |
| 5164 | GetContentByIndex(current_remote_description(), i); |
| 5165 | bool had_been_rejected = |
| 5166 | (current_local_content && current_local_content->rejected) || |
| 5167 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5168 | const std::string& mid = |
| 5169 | (local_content ? local_content->name : remote_content->name); |
| 5170 | cricket::MediaType media_type = |
| 5171 | (local_content ? local_content->media_description()->type() |
| 5172 | : remote_content->media_description()->type()); |
| 5173 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5174 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5175 | auto transceiver = GetAssociatedTransceiver(mid); |
| 5176 | RTC_CHECK(transceiver); |
| 5177 | // A media section is considered eligible for recycling if it is marked as |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5178 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 5179 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5180 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5181 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 5182 | RtpTransceiverDirection::kInactive, |
| 5183 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5184 | recycleable_mline_indices.push(i); |
| 5185 | } else { |
| 5186 | session_options->media_description_options.push_back( |
| 5187 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 5188 | // CreateOffer shouldn't really cause any state changes in |
| 5189 | // PeerConnection, but we need a way to match new transceivers to new |
| 5190 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 5191 | // by recording the index of the media section generated for the |
| 5192 | // transceiver in the offer. |
| 5193 | transceiver->internal()->set_mline_index(i); |
| 5194 | } |
| 5195 | } else { |
| 5196 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5197 | RTC_CHECK(GetDataMid()); |
| 5198 | if (had_been_rejected || mid != *GetDataMid()) { |
| 5199 | session_options->media_description_options.push_back( |
| 5200 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 5201 | } else { |
| 5202 | session_options->media_description_options.push_back( |
| 5203 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 5204 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5205 | } |
| 5206 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 5207 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5208 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 5209 | // and not associated). Reuse media sections marked as recyclable first, |
| 5210 | // otherwise append to the end of the offer. New media sections should be |
| 5211 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5212 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5213 | if (transceiver->mid() || transceiver->stopped()) { |
| 5214 | continue; |
| 5215 | } |
| 5216 | size_t mline_index; |
| 5217 | if (!recycleable_mline_indices.empty()) { |
| 5218 | mline_index = recycleable_mline_indices.front(); |
| 5219 | recycleable_mline_indices.pop(); |
| 5220 | session_options->media_description_options[mline_index] = |
| 5221 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 5222 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5223 | } else { |
| 5224 | mline_index = session_options->media_description_options.size(); |
| 5225 | session_options->media_description_options.push_back( |
| 5226 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 5227 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5228 | } |
| 5229 | // See comment above for why CreateOffer changes the transceiver's state. |
| 5230 | transceiver->internal()->set_mline_index(mline_index); |
| 5231 | } |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5232 | // Lastly, add a m-section if we have local data channels and an m section |
| 5233 | // does not already exist. |
| 5234 | if (!GetDataMid() && HasDataChannels()) { |
| 5235 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 5236 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5237 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5238 | } |
| 5239 | |
| 5240 | void PeerConnection::GetOptionsForAnswer( |
| 5241 | const RTCOfferAnswerOptions& offer_answer_options, |
| 5242 | cricket::MediaSessionOptions* session_options) { |
| 5243 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 5244 | |
| 5245 | if (IsUnifiedPlan()) { |
| 5246 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 5247 | } else { |
| 5248 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 5249 | } |
| 5250 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5251 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 5252 | // the RTP data channels would be successfully negotiated by default and the |
| 5253 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 5254 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 5255 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 5256 | session_options->data_channel_type = data_channel_type(); |
| 5257 | } |
| 5258 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5259 | // Apply ICE renomination flag. |
| 5260 | for (auto& options : session_options->media_description_options) { |
| 5261 | options.transport_options.enable_ice_renomination = |
| 5262 | configuration_.enable_ice_renomination; |
| 5263 | } |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 5264 | |
| 5265 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 5266 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 5267 | session_options->pooled_ice_credentials = |
| 5268 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 5269 | RTC_FROM_HERE, |
| 5270 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 5271 | port_allocator_.get())); |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 5272 | |
| 5273 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 5274 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 5275 | for (auto& options : session_options->media_description_options) { |
Bjorn A Mellem | 8e1343a | 2019-09-30 22:12:47 | [diff] [blame] | 5276 | absl::optional<cricket::OpaqueTransportParameters> params = |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 5277 | transport_controller_->GetTransportParameters(options.mid); |
Bjorn A Mellem | 8e1343a | 2019-09-30 22:12:47 | [diff] [blame] | 5278 | if (!params) { |
| 5279 | continue; |
| 5280 | } |
| 5281 | options.transport_options.opaque_parameters = params; |
| 5282 | if ((use_datagram_transport_ && |
| 5283 | (options.type == cricket::MEDIA_TYPE_AUDIO || |
| 5284 | options.type == cricket::MEDIA_TYPE_VIDEO)) || |
| 5285 | (use_datagram_transport_for_data_channels_ && |
| 5286 | options.type == cricket::MEDIA_TYPE_DATA)) { |
| 5287 | options.alt_protocol = params->protocol; |
| 5288 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 17:28:06 | [diff] [blame] | 5289 | } |
| 5290 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5291 | } |
| 5292 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5293 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 5294 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 5295 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5296 | // Figure out transceiver directional preferences. |
| 5297 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 5298 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 5299 | |
| 5300 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 5301 | // restricted by the direction in the offer. |
| 5302 | bool recv_audio = true; |
| 5303 | bool recv_video = true; |
| 5304 | |
| 5305 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5306 | if (offer_answer_options.offer_to_receive_audio != |
| 5307 | RTCOfferAnswerOptions::kUndefined) { |
| 5308 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 5309 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5310 | if (offer_answer_options.offer_to_receive_video != |
| 5311 | RTCOfferAnswerOptions::kUndefined) { |
| 5312 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5313 | } |
| 5314 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5315 | absl::optional<size_t> audio_index; |
| 5316 | absl::optional<size_t> video_index; |
| 5317 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 5318 | |
| 5319 | // Generate m= sections that match those in the offer. |
| 5320 | // Note that mediasession.cc will handle intersection our preferred |
| 5321 | // direction with the offered direction. |
| 5322 | GenerateMediaDescriptionOptions( |
| 5323 | remote_description(), |
| 5324 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 5325 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 5326 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5327 | |
| 5328 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 5329 | !audio_index ? nullptr |
| 5330 | : &session_options->media_description_options[*audio_index]; |
| 5331 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 5332 | !video_index ? nullptr |
| 5333 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5334 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 5335 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 5336 | audio_media_description_options, |
| 5337 | video_media_description_options, |
| 5338 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5339 | } |
zhihuang | af38847 | 2016-11-02 23:49:48 | [diff] [blame] | 5340 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5341 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 5342 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 5343 | cricket::MediaSessionOptions* session_options) { |
| 5344 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 5345 | // Answers) and 5.3.2 (Subsequent Answers). |
| 5346 | RTC_DCHECK(remote_description()); |
| 5347 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 5348 | for (const ContentInfo& content : |
| 5349 | remote_description()->description()->contents()) { |
| 5350 | cricket::MediaType media_type = content.media_description()->type(); |
| 5351 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5352 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5353 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 5354 | RTC_CHECK(transceiver); |
| 5355 | session_options->media_description_options.push_back( |
| 5356 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 5357 | } else { |
| 5358 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 23:23:40 | [diff] [blame] | 5359 | // Reject all data sections if data channels are disabled. |
| 5360 | // Reject a data section if it has already been rejected. |
| 5361 | // Reject all data sections except for the first one. |
| 5362 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 5363 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5364 | session_options->media_description_options.push_back( |
| 5365 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 5366 | } else { |
| 5367 | session_options->media_description_options.push_back( |
| 5368 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 5369 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5370 | } |
| 5371 | } |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 5372 | } |
| 5373 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5374 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 5375 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 5376 | RtpTransceiverDirection audio_direction, |
| 5377 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5378 | absl::optional<size_t>* audio_index, |
| 5379 | absl::optional<size_t>* video_index, |
| 5380 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 5381 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5382 | for (const cricket::ContentInfo& content : |
| 5383 | session_desc->description()->contents()) { |
| 5384 | if (IsAudioContent(&content)) { |
| 5385 | // If we already have an audio m= section, reject this extra one. |
| 5386 | if (*audio_index) { |
| 5387 | session_options->media_description_options.push_back( |
| 5388 | cricket::MediaDescriptionOptions( |
| 5389 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5390 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5391 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5392 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5393 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5394 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 5395 | content.name, audio_direction, |
| 5396 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5397 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5398 | } |
| 5399 | } else if (IsVideoContent(&content)) { |
| 5400 | // If we already have an video m= section, reject this extra one. |
| 5401 | if (*video_index) { |
| 5402 | session_options->media_description_options.push_back( |
| 5403 | cricket::MediaDescriptionOptions( |
| 5404 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5405 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5406 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5407 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5408 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5409 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 5410 | content.name, video_direction, |
| 5411 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5412 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5413 | } |
| 5414 | } else { |
| 5415 | RTC_DCHECK(IsDataContent(&content)); |
| 5416 | // If we already have an data m= section, reject this extra one. |
| 5417 | if (*data_index) { |
| 5418 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5419 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5420 | } else { |
| 5421 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5422 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5423 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5424 | } |
| 5425 | } |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 5426 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5427 | } |
| 5428 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5429 | cricket::MediaDescriptionOptions |
| 5430 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 5431 | const std::string& mid) const { |
| 5432 | // Direction for data sections is meaningless, but legacy endpoints might |
| 5433 | // expect sendrecv. |
| 5434 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 5435 | RtpTransceiverDirection::kSendRecv, |
| 5436 | /*stopped=*/false); |
| 5437 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 5438 | return options; |
| 5439 | } |
| 5440 | |
| 5441 | cricket::MediaDescriptionOptions |
| 5442 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 5443 | const std::string& mid) const { |
| 5444 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 5445 | RtpTransceiverDirection::kInactive, |
| 5446 | /*stopped=*/true); |
| 5447 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 5448 | return options; |
| 5449 | } |
| 5450 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5451 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5452 | switch (data_channel_type_) { |
| 5453 | case cricket::DCT_RTP: |
| 5454 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5455 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5456 | } |
| 5457 | return rtp_data_channel_->content_name(); |
| 5458 | case cricket::DCT_SCTP: |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5459 | case cricket::DCT_MEDIA_TRANSPORT: |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 5460 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
| 5461 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
| 5462 | return sctp_mid_; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5463 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5464 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5465 | } |
| 5466 | } |
| 5467 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5468 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 5469 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 5470 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5471 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 5472 | } |
| 5473 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5474 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5475 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5476 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5477 | cricket::MediaType media_type, |
| 5478 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5479 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5480 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5481 | std::vector<RtpSenderInfo>* current_senders = |
| 5482 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5483 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5484 | // Find removed senders. I.e., senders where the sender id or ssrc don't match |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5485 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5486 | for (auto sender_it = current_senders->begin(); |
| 5487 | sender_it != current_senders->end(); |
| 5488 | /* incremented manually */) { |
| 5489 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5490 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5491 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5492 | std::string params_stream_id; |
| 5493 | if (params) { |
| 5494 | params_stream_id = |
| 5495 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 5496 | : kDefaultStreamId); |
| 5497 | } |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5498 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5499 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5500 | // If this is a default track, and we still need it, don't remove it. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5501 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5502 | sender_exists) { |
| 5503 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5504 | } else { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5505 | OnRemoteSenderRemoved(info, media_type); |
| 5506 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5507 | } |
| 5508 | } |
| 5509 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5510 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5511 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 5512 | if (!params.has_ssrcs()) { |
| 5513 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 5514 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 5515 | // create a default. |
| 5516 | default_sender_needed = true; |
| 5517 | break; |
| 5518 | } |
| 5519 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5520 | // |params.id| is the sender id and the stream id uses the first of |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5521 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 5522 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 5523 | // not supported in Plan B, we just take the first here and create the |
| 5524 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5525 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5526 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 5527 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5528 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5529 | uint32_t ssrc = params.first_ssrc(); |
| 5530 | |
| 5531 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5532 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5533 | if (!stream) { |
| 5534 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5535 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5536 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5537 | remote_streams_->AddStream(stream); |
| 5538 | new_streams->AddStream(stream); |
| 5539 | } |
| 5540 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5541 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5542 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5543 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5544 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5545 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5546 | } |
| 5547 | } |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5548 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5549 | // Add default sender if necessary. |
| 5550 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5551 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5552 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5553 | if (!default_stream) { |
| 5554 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5555 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5556 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5557 | remote_streams_->AddStream(default_stream); |
| 5558 | new_streams->AddStream(default_stream); |
| 5559 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5560 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5561 | ? kDefaultAudioSenderId |
| 5562 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5563 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5564 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5565 | if (!default_sender_info) { |
| 5566 | current_senders->push_back( |
Ruslan Burakov | 7ea4605 | 2019-02-16 01:07:05 | [diff] [blame] | 5567 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5568 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5569 | } |
| 5570 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5571 | } |
| 5572 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5573 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 5574 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 5575 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 5576 | << " receiver for track_id=" << sender_info.sender_id |
| 5577 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5578 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 5579 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5580 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5581 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5582 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5583 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5584 | } else { |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 5585 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5586 | } |
| 5587 | } |
| 5588 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5589 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 5590 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5591 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 5592 | << " receiver for track_id=" << sender_info.sender_id |
| 5593 | << " and stream_id=" << sender_info.stream_id; |
| 5594 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5595 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5596 | |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 5597 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5598 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5599 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 5600 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5601 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5602 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5603 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5604 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5605 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5606 | } |
| 5607 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5608 | // Stopping or destroying a VideoRtpReceiver will end the |
| 5609 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5610 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5611 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5612 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5613 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5614 | // There's no guarantee the track is still available, e.g. the track may |
| 5615 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5616 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5617 | } |
| 5618 | } else { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 5619 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5620 | } |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 5621 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5622 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 5623 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 5627 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 5628 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 5629 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 5630 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 5631 | streams_to_remove.push_back(stream); |
| 5632 | } |
| 5633 | } |
| 5634 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 20:02:21 | [diff] [blame] | 5635 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5636 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5637 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5638 | } |
| 5639 | } |
| 5640 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5641 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5642 | const std::vector<cricket::StreamParams>& streams, |
| 5643 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5644 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5645 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5646 | // Find removed tracks. I.e., tracks where the track id, stream id or ssrc |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5647 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5648 | for (auto sender_it = current_senders->begin(); |
| 5649 | sender_it != current_senders->end(); |
| 5650 | /* incremented manually */) { |
| 5651 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5652 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5653 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 5654 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5655 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5656 | OnLocalSenderRemoved(info, media_type); |
| 5657 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5658 | } else { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5659 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5660 | } |
| 5661 | } |
| 5662 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5663 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5664 | for (const cricket::StreamParams& params : streams) { |
| 5665 | // The sync_label is the MediaStream label and the |stream.id| is the |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5666 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5667 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5668 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5669 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5670 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5671 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5672 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5673 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5674 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5675 | } |
| 5676 | } |
| 5677 | } |
| 5678 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5679 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 5680 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5681 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5682 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5683 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5684 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 5685 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5686 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5687 | return; |
| 5688 | } |
| 5689 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5690 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5691 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5692 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5693 | return; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5694 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5695 | |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 5696 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5697 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5698 | } |
| 5699 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5700 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 5701 | cricket::MediaType media_type) { |
| 5702 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5703 | if (!sender) { |
| 5704 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5705 | // SessionDescriptions has been renegotiated. |
| 5706 | return; |
| 5707 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5708 | |
| 5709 | // A sender has been removed from the SessionDescription but it's still |
| 5710 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 5711 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 5712 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5713 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5714 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5715 | return; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5716 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5717 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5718 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5719 | } |
| 5720 | |
| 5721 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 5722 | const cricket::StreamParamsVec& streams) { |
| 5723 | std::vector<std::string> existing_channels; |
| 5724 | |
| 5725 | // Find new and active data channels. |
| 5726 | for (const cricket::StreamParams& params : streams) { |
| 5727 | // |it->sync_label| is actually the data channel label. The reason is that |
| 5728 | // we use the same naming of data channels as we do for |
| 5729 | // MediaStreams and Tracks. |
| 5730 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 5731 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5732 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5733 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 5734 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5735 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5736 | continue; |
| 5737 | } |
| 5738 | // Set the SSRC the data channel should use for sending. |
| 5739 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 5740 | existing_channels.push_back(data_channel_it->first); |
| 5741 | } |
| 5742 | |
| 5743 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 5744 | } |
| 5745 | |
| 5746 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 5747 | const cricket::StreamParamsVec& streams) { |
| 5748 | std::vector<std::string> existing_channels; |
| 5749 | |
| 5750 | // Find new and active data channels. |
| 5751 | for (const cricket::StreamParams& params : streams) { |
| 5752 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 5753 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5754 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5755 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5756 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5757 | auto data_channel_it = rtp_data_channels_.find(label); |
| 5758 | if (data_channel_it == rtp_data_channels_.end()) { |
| 5759 | // This is a new data channel. |
| 5760 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 5761 | } else { |
| 5762 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 5763 | } |
| 5764 | existing_channels.push_back(label); |
| 5765 | } |
| 5766 | |
| 5767 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 5768 | } |
| 5769 | |
| 5770 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 5771 | const std::vector<std::string>& active_channels, |
| 5772 | bool is_local_update) { |
| 5773 | auto it = rtp_data_channels_.begin(); |
| 5774 | while (it != rtp_data_channels_.end()) { |
| 5775 | DataChannel* data_channel = it->second; |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 5776 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5777 | ++it; |
| 5778 | continue; |
| 5779 | } |
| 5780 | |
| 5781 | if (is_local_update) { |
| 5782 | data_channel->SetSendSsrc(0); |
| 5783 | } else { |
| 5784 | data_channel->RemotePeerRequestClose(); |
| 5785 | } |
| 5786 | |
| 5787 | if (data_channel->state() == DataChannel::kClosed) { |
| 5788 | rtp_data_channels_.erase(it); |
| 5789 | it = rtp_data_channels_.begin(); |
| 5790 | } else { |
| 5791 | ++it; |
| 5792 | } |
| 5793 | } |
| 5794 | } |
| 5795 | |
| 5796 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 5797 | uint32_t remote_ssrc) { |
| 5798 | rtc::scoped_refptr<DataChannel> channel( |
| 5799 | InternalCreateDataChannel(label, nullptr)); |
| 5800 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5801 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5802 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5803 | return; |
| 5804 | } |
| 5805 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 5806 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5807 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5808 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5809 | } |
| 5810 | |
| 5811 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 5812 | const std::string& label, |
| 5813 | const InternalDataChannelInit* config) { |
| 5814 | if (IsClosed()) { |
| 5815 | return nullptr; |
| 5816 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5817 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5818 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5819 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 5820 | return nullptr; |
| 5821 | } |
| 5822 | InternalDataChannelInit new_config = |
| 5823 | config ? (*config) : InternalDataChannelInit(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5824 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5825 | if (new_config.id < 0) { |
| 5826 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5827 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5828 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5829 | RTC_LOG(LS_ERROR) |
| 5830 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5831 | return nullptr; |
| 5832 | } |
| 5833 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5834 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5835 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5836 | return nullptr; |
| 5837 | } |
| 5838 | } |
| 5839 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5840 | rtc::scoped_refptr<DataChannel> channel( |
| 5841 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5842 | if (!channel) { |
| 5843 | sid_allocator_.ReleaseSid(new_config.id); |
| 5844 | return nullptr; |
| 5845 | } |
| 5846 | |
| 5847 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 5848 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5849 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 5850 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5851 | return nullptr; |
| 5852 | } |
| 5853 | rtp_data_channels_[channel->label()] = channel; |
| 5854 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5855 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5856 | sctp_data_channels_.push_back(channel); |
| 5857 | channel->SignalClosed.connect(this, |
| 5858 | &PeerConnection::OnSctpDataChannelClosed); |
| 5859 | } |
| 5860 | |
Steve Anton | 2d8609c | 2018-01-24 00:38:46 | [diff] [blame] | 5861 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5862 | return channel; |
| 5863 | } |
| 5864 | |
| 5865 | bool PeerConnection::HasDataChannels() const { |
| 5866 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 5867 | } |
| 5868 | |
| 5869 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 10:29:38 | [diff] [blame] | 5870 | std::vector<rtc::scoped_refptr<DataChannel>> channels_to_close; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5871 | for (const auto& channel : sctp_data_channels_) { |
| 5872 | if (channel->id() < 0) { |
| 5873 | int sid; |
| 5874 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 10:29:38 | [diff] [blame] | 5875 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid, closing channel."; |
| 5876 | channels_to_close.push_back(channel); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5877 | continue; |
| 5878 | } |
| 5879 | channel->SetSctpSid(sid); |
| 5880 | } |
| 5881 | } |
Harald Alvestrand | 1f928d3 | 2019-03-28 10:29:38 | [diff] [blame] | 5882 | // Since closing modifies the list of channels, we have to do the actual |
| 5883 | // closing outside the loop. |
| 5884 | for (const auto& channel : channels_to_close) { |
| 5885 | channel->CloseAbruptly(); |
| 5886 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5887 | } |
| 5888 | |
| 5889 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-15 02:15:29 | [diff] [blame] | 5890 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5891 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 5892 | ++it) { |
| 5893 | if (it->get() == channel) { |
| 5894 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 5895 | // After the closing procedure is done, it's safe to use this ID for |
| 5896 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5897 | sid_allocator_.ReleaseSid(channel->id()); |
| 5898 | } |
deadbeef | bd29246 | 2015-12-15 02:15:29 | [diff] [blame] | 5899 | // Since this method is triggered by a signal from the DataChannel, |
| 5900 | // we can't free it directly here; we need to free it asynchronously. |
| 5901 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5902 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 5903 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 5904 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5905 | return; |
| 5906 | } |
| 5907 | } |
| 5908 | } |
| 5909 | |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 5910 | void PeerConnection::OnTransportChannelClosed() { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5911 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 5912 | // DataChannel may callback to us and try to modify the list. |
| 5913 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 5914 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 5915 | for (const auto& kv : temp_rtp_dcs) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 5916 | kv.second->OnTransportChannelClosed(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 5920 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 5921 | for (const auto& channel : temp_sctp_dcs) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 5922 | channel->OnTransportChannelClosed(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | void PeerConnection::OnDataChannelOpenMessage( |
| 5927 | const std::string& label, |
| 5928 | const InternalDataChannelInit& config) { |
| 5929 | rtc::scoped_refptr<DataChannel> channel( |
| 5930 | InternalCreateDataChannel(label, &config)); |
| 5931 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5932 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5933 | return; |
| 5934 | } |
| 5935 | |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 5936 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5937 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5938 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 5939 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5940 | } |
| 5941 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5942 | bool PeerConnection::HandleOpenMessage_s( |
| 5943 | const cricket::ReceiveDataParams& params, |
| 5944 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5945 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 5946 | // Received OPEN message; parse and signal that a new data channel should |
| 5947 | // be created. |
| 5948 | std::string label; |
| 5949 | InternalDataChannelInit config; |
| 5950 | config.id = params.ssrc; |
| 5951 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 5952 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 5953 | << params.ssrc; |
| 5954 | return true; |
| 5955 | } |
| 5956 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5957 | OnDataChannelOpenMessage(label, config); |
| 5958 | return true; |
| 5959 | } |
| 5960 | return false; |
| 5961 | } |
| 5962 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5963 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5964 | PeerConnection::GetAudioTransceiver() const { |
| 5965 | // This method only works with Plan B SDP, where there is a single |
| 5966 | // audio/video transceiver. |
| 5967 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5968 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5969 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5970 | return transceiver; |
| 5971 | } |
| 5972 | } |
| 5973 | RTC_NOTREACHED(); |
| 5974 | return nullptr; |
| 5975 | } |
| 5976 | |
| 5977 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5978 | PeerConnection::GetVideoTransceiver() const { |
| 5979 | // This method only works with Plan B SDP, where there is a single |
| 5980 | // audio/video transceiver. |
| 5981 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5982 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5983 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5984 | return transceiver; |
| 5985 | } |
| 5986 | } |
| 5987 | RTC_NOTREACHED(); |
| 5988 | return nullptr; |
| 5989 | } |
| 5990 | |
| 5991 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 5992 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5993 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5994 | switch (type) { |
| 5995 | case cricket::MEDIA_TYPE_AUDIO: |
| 5996 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 5997 | case cricket::MEDIA_TYPE_VIDEO: |
| 5998 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 5999 | case cricket::MEDIA_TYPE_DATA: |
| 6000 | return false; |
| 6001 | } |
| 6002 | RTC_NOTREACHED(); |
| 6003 | return false; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 6004 | } |
| 6005 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6006 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 6007 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6008 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6009 | for (auto sender : transceiver->internal()->senders()) { |
| 6010 | if (sender->track() == track) { |
| 6011 | return sender; |
| 6012 | } |
| 6013 | } |
| 6014 | } |
| 6015 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 6016 | } |
| 6017 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6018 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 6019 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6020 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6021 | for (auto sender : transceiver->internal()->senders()) { |
| 6022 | if (sender->id() == sender_id) { |
| 6023 | return sender; |
| 6024 | } |
| 6025 | } |
| 6026 | } |
| 6027 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 6028 | } |
| 6029 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6030 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 6031 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6032 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6033 | for (auto receiver : transceiver->internal()->receivers()) { |
| 6034 | if (receiver->id() == receiver_id) { |
| 6035 | return receiver; |
| 6036 | } |
| 6037 | } |
| 6038 | } |
| 6039 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 6040 | } |
| 6041 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6042 | std::vector<PeerConnection::RtpSenderInfo>* |
| 6043 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 6044 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 6045 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 6046 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 6047 | ? &remote_audio_sender_infos_ |
| 6048 | : &remote_video_sender_infos_; |
| 6049 | } |
| 6050 | |
| 6051 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 6052 | cricket::MediaType media_type) { |
| 6053 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 6054 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6055 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 6056 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 6057 | } |
| 6058 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6059 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 6060 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 6061 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6062 | const std::string sender_id) const { |
| 6063 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 6064 | if (sender_info.stream_id == stream_id && |
| 6065 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6066 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 6067 | } |
| 6068 | } |
| 6069 | return nullptr; |
| 6070 | } |
| 6071 | |
| 6072 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 6073 | for (const auto& channel : sctp_data_channels_) { |
| 6074 | if (channel->id() == sid) { |
| 6075 | return channel; |
| 6076 | } |
| 6077 | } |
| 6078 | return nullptr; |
| 6079 | } |
| 6080 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6081 | PeerConnection::InitializePortAllocatorResult |
| 6082 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 6083 | const cricket::ServerAddresses& stun_servers, |
| 6084 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6085 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6086 | RTC_DCHECK_RUN_ON(network_thread()); |
| 6087 | |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 6088 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6089 | // To handle both internal and externally created port allocator, we will |
| 6090 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6091 | int port_allocator_flags = port_allocator_->flags(); |
| 6092 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 6093 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 6094 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6095 | // If the disable-IPv6 flag was specified, we'll not override it |
| 6096 | // by experiment. |
| 6097 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6098 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 16:50:47 | [diff] [blame] | 6099 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 6100 | .find("Disabled") == 0) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6101 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6102 | } |
| 6103 | |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 6104 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6105 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6106 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 6107 | } |
| 6108 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6109 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6110 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6111 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6112 | } |
| 6113 | |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 6114 | if (configuration.candidate_network_policy == |
| 6115 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6116 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6117 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 6118 | } |
| 6119 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 6120 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6121 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 6122 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 6123 | } |
| 6124 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6125 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6126 | // No step delay is used while allocating ports. |
| 6127 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 6128 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6129 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 6130 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6131 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 6132 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 6133 | for (auto& turn_server : turn_servers_copy) { |
| 6134 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 6135 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6136 | // Call this last since it may create pooled allocator sessions using the |
| 6137 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 6138 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 6139 | stun_servers, std::move(turn_servers_copy), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 6140 | configuration.ice_candidate_pool_size, |
| 6141 | configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 6142 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 6143 | |
| 6144 | InitializePortAllocatorResult res; |
| 6145 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 6146 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6147 | } |
| 6148 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 6149 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 6150 | const cricket::ServerAddresses& stun_servers, |
| 6151 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 6152 | IceTransportsType type, |
| 6153 | int candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 6154 | PortPrunePolicy turn_port_prune_policy, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 6155 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 6156 | absl::optional<int> stun_candidate_keepalive_interval, |
| 6157 | bool have_local_description) { |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 6158 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 6159 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 6160 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 6161 | // size is not allowed, and changing the set of ICE servers will not result |
| 6162 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 6163 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 6164 | port_allocator_->FreezeCandidatePool(); |
| 6165 | } |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 6166 | // Add the custom tls turn servers if they exist. |
| 6167 | auto turn_servers_copy = turn_servers; |
| 6168 | for (auto& turn_server : turn_servers_copy) { |
| 6169 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 6170 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6171 | // Call this last since it may create pooled allocator sessions using the |
| 6172 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 6173 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 6174 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 6175 | turn_port_prune_policy, turn_customizer, |
| 6176 | stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 6177 | } |
| 6178 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 6179 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 6180 | return factory_->channel_manager(); |
| 6181 | } |
| 6182 | |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 6183 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 6184 | std::unique_ptr<RtcEventLogOutput> output, |
| 6185 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 6186 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 6187 | if (!event_log_) { |
| 6188 | return false; |
| 6189 | } |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 6190 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 6191 | } |
| 6192 | |
| 6193 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 6194 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 6195 | if (event_log_) { |
| 6196 | event_log_->StopLogging(); |
| 6197 | } |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 6198 | } |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 6199 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6200 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6201 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6202 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6203 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6204 | if (channel && channel->content_name() == content_name) { |
| 6205 | return channel; |
| 6206 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6207 | } |
| 6208 | if (rtp_data_channel() && |
| 6209 | rtp_data_channel()->content_name() == content_name) { |
| 6210 | return rtp_data_channel(); |
| 6211 | } |
| 6212 | return nullptr; |
| 6213 | } |
| 6214 | |
| 6215 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6216 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6217 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6218 | RTC_LOG(LS_INFO) |
| 6219 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6220 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6221 | return false; |
| 6222 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6223 | if (!data_channel_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6224 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6225 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6226 | return false; |
| 6227 | } |
| 6228 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6229 | absl::optional<rtc::SSLRole> dtls_role; |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6230 | if (sctp_mid_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6231 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6232 | if (!dtls_role && is_caller_.has_value()) { |
| 6233 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 6234 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6235 | *role = *dtls_role; |
| 6236 | return true; |
| 6237 | } |
| 6238 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6239 | } |
| 6240 | |
| 6241 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 6242 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6243 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6244 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6245 | RTC_LOG(LS_INFO) |
| 6246 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6247 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6248 | return false; |
| 6249 | } |
| 6250 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6251 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 6252 | if (dtls_role) { |
| 6253 | *role = *dtls_role; |
| 6254 | return true; |
| 6255 | } |
| 6256 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6257 | } |
| 6258 | |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6259 | void PeerConnection::SetSessionError(SessionError error, |
| 6260 | const std::string& error_desc) { |
| 6261 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6262 | if (error != session_error_) { |
| 6263 | session_error_ = error; |
| 6264 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6265 | } |
| 6266 | } |
| 6267 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6268 | RTCError PeerConnection::UpdateSessionState( |
| 6269 | SdpType type, |
| 6270 | cricket::ContentSource source, |
| 6271 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6272 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6273 | |
| 6274 | // If there's already a pending error then no state transition should happen. |
| 6275 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6276 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6277 | |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6278 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6279 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6280 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | // Update the signaling state according to the specified state machine (see |
| 6284 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6285 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6286 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 6287 | ? PeerConnectionInterface::kHaveLocalOffer |
| 6288 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6289 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6290 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 6291 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 6292 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6293 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6294 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6295 | ChangeSignalingState(PeerConnectionInterface::kStable); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 6296 | transceiver_stable_states_by_transceivers_.clear(); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6297 | } |
| 6298 | |
| 6299 | // Update internal objects according to the session description's media |
| 6300 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6301 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6302 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 6303 | return error; |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 6304 | } |
| 6305 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6306 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6307 | } |
| 6308 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6309 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6310 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6311 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6312 | const SessionDescriptionInterface* sdesc = |
| 6313 | (source == cricket::CS_LOCAL ? local_description() |
| 6314 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6315 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6316 | |
| 6317 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6318 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6319 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6320 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6321 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6322 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6323 | continue; |
| 6324 | } |
| 6325 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 6326 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6327 | if (!content_desc) { |
| 6328 | continue; |
| 6329 | } |
| 6330 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 6331 | bool success = (source == cricket::CS_LOCAL) |
| 6332 | ? channel->SetLocalContent(content_desc, type, &error) |
| 6333 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6334 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 20:48:57 | [diff] [blame] | 6335 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6336 | } |
| 6337 | } |
| 6338 | |
| 6339 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 6340 | if (rtp_data_channel_) { |
| 6341 | const ContentInfo* data_content = |
| 6342 | cricket::GetFirstDataContent(sdesc->description()); |
| 6343 | if (data_content && !data_content->rejected) { |
| 6344 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 6345 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6346 | if (data_desc) { |
| 6347 | std::string error; |
| 6348 | bool success = |
| 6349 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6350 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 6351 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6352 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 20:48:57 | [diff] [blame] | 6353 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6354 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6355 | } |
| 6356 | } |
| 6357 | } |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6358 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6359 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 6360 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6361 | if (sctp_mid_ && local_description() && remote_description()) { |
| 6362 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
| 6363 | transport_controller_->GetSctpTransport(*sctp_mid_); |
Harald Alvestrand | fbb45bd | 2019-05-15 06:07:47 | [diff] [blame] | 6364 | auto local_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 6365 | local_description()->description()); |
| 6366 | auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 6367 | remote_description()->description()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6368 | if (sctp_transport && local_sctp_description && remote_sctp_description) { |
Harald Alvestrand | 8d3d6cf | 2019-05-16 09:49:17 | [diff] [blame] | 6369 | int max_message_size; |
| 6370 | // A remote max message size of zero means "any size supported". |
| 6371 | // We configure the connection with our own max message size. |
| 6372 | if (remote_sctp_description->max_message_size() == 0) { |
| 6373 | max_message_size = local_sctp_description->max_message_size(); |
| 6374 | } else { |
| 6375 | max_message_size = |
| 6376 | std::min(local_sctp_description->max_message_size(), |
| 6377 | remote_sctp_description->max_message_size()); |
Harald Alvestrand | fbb45bd | 2019-05-15 06:07:47 | [diff] [blame] | 6378 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6379 | sctp_transport->Start(local_sctp_description->port(), |
| 6380 | remote_sctp_description->port(), max_message_size); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6381 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6382 | } |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6383 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6384 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6385 | } |
| 6386 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6387 | RTCError PeerConnection::PushdownTransportDescription( |
| 6388 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6389 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6390 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6391 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6392 | if (source == cricket::CS_LOCAL) { |
| 6393 | const SessionDescriptionInterface* sdesc = local_description(); |
| 6394 | RTC_DCHECK(sdesc); |
| 6395 | return transport_controller_->SetLocalDescription(type, |
| 6396 | sdesc->description()); |
| 6397 | } else { |
| 6398 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 6399 | RTC_DCHECK(sdesc); |
| 6400 | return transport_controller_->SetRemoteDescription(type, |
| 6401 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6402 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6403 | } |
| 6404 | |
| 6405 | bool PeerConnection::GetTransportDescription( |
| 6406 | const SessionDescription* description, |
| 6407 | const std::string& content_name, |
| 6408 | cricket::TransportDescription* tdesc) { |
| 6409 | if (!description || !tdesc) { |
| 6410 | return false; |
| 6411 | } |
| 6412 | const TransportInfo* transport_info = |
| 6413 | description->GetTransportInfoByName(content_name); |
| 6414 | if (!transport_info) { |
| 6415 | return false; |
| 6416 | } |
| 6417 | *tdesc = transport_info->description; |
| 6418 | return true; |
| 6419 | } |
| 6420 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6421 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 6422 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 6423 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6424 | switch (config.continual_gathering_policy) { |
| 6425 | case PeerConnectionInterface::GATHER_ONCE: |
| 6426 | gathering_policy = cricket::GATHER_ONCE; |
| 6427 | break; |
| 6428 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 6429 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 6430 | break; |
| 6431 | default: |
| 6432 | RTC_NOTREACHED(); |
| 6433 | gathering_policy = cricket::GATHER_ONCE; |
| 6434 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 6435 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6436 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 6437 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 6438 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6439 | ice_config.prioritize_most_likely_candidate_pairs = |
| 6440 | config.prioritize_most_likely_ice_candidate_pairs; |
| 6441 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 6442 | RTCConfigurationToIceConfigOptionalInt( |
| 6443 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6444 | ice_config.continual_gathering_policy = gathering_policy; |
| 6445 | ice_config.presume_writable_when_fully_relayed = |
| 6446 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 6447 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 6448 | config.surface_ice_candidates_on_ice_transport_type_changed; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 6449 | ice_config.ice_check_interval_strong_connectivity = |
| 6450 | config.ice_check_interval_strong_connectivity; |
| 6451 | ice_config.ice_check_interval_weak_connectivity = |
| 6452 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6453 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-30 07:08:51 | [diff] [blame] | 6454 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 6455 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 6456 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 6457 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6458 | ice_config.regather_all_networks_interval_range = |
| 6459 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 6460 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6461 | return ice_config; |
| 6462 | } |
| 6463 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6464 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 6465 | const rtc::CopyOnWriteBuffer& payload, |
| 6466 | cricket::SendDataResult* result) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6467 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6468 | if (data_channel_transport_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6469 | SendDataParams send_params; |
| 6470 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 6471 | send_params.ordered = params.ordered; |
| 6472 | if (params.max_rtx_count >= 0) { |
| 6473 | send_params.max_rtx_count = params.max_rtx_count; |
| 6474 | } else if (params.max_rtx_ms >= 0) { |
| 6475 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 6476 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6477 | |
| 6478 | RTCError error = network_thread()->Invoke<RTCError>( |
| 6479 | RTC_FROM_HERE, [this, params, send_params, payload] { |
| 6480 | return data_channel_transport_->SendData(params.sid, send_params, |
| 6481 | payload); |
| 6482 | }); |
| 6483 | |
| 6484 | if (error.ok()) { |
| 6485 | *result = cricket::SendDataResult::SDR_SUCCESS; |
| 6486 | return true; |
| 6487 | } else if (error.type() == RTCErrorType::RESOURCE_EXHAUSTED) { |
| 6488 | // SCTP transport uses RESOURCE_EXHAUSTED when it's blocked. |
| 6489 | // TODO(mellem): Stop using RTCError here and get rid of the mapping. |
| 6490 | *result = cricket::SendDataResult::SDR_BLOCK; |
| 6491 | return false; |
| 6492 | } |
| 6493 | *result = cricket::SendDataResult::SDR_ERROR; |
| 6494 | return false; |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6495 | } else if (rtp_data_channel_) { |
| 6496 | return rtp_data_channel_->SendData(params, payload, result); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6497 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6498 | RTC_LOG(LS_ERROR) << "SendData called before transport is ready"; |
| 6499 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6503 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6504 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6505 | // Don't log an error here, because DataChannels are expected to call |
| 6506 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 6507 | // whether or not the underlying transport is ready. |
| 6508 | return false; |
| 6509 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6510 | if (data_channel_transport_) { |
| 6511 | SignalDataChannelTransportWritable_s.connect(webrtc_data_channel, |
| 6512 | &DataChannel::OnChannelReady); |
| 6513 | SignalDataChannelTransportReceivedData_s.connect( |
| 6514 | webrtc_data_channel, &DataChannel::OnDataReceived); |
| 6515 | SignalDataChannelTransportChannelClosing_s.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6516 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6517 | SignalDataChannelTransportChannelClosed_s.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6518 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6519 | } |
| 6520 | if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6521 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 6522 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 6523 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 6524 | &DataChannel::OnDataReceived); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6525 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6526 | return true; |
| 6527 | } |
| 6528 | |
| 6529 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6530 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6531 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6532 | RTC_LOG(LS_ERROR) |
| 6533 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 6534 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6535 | return; |
| 6536 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6537 | if (data_channel_transport_) { |
| 6538 | SignalDataChannelTransportWritable_s.disconnect(webrtc_data_channel); |
| 6539 | SignalDataChannelTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 6540 | SignalDataChannelTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 6541 | SignalDataChannelTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 6542 | } |
| 6543 | if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6544 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 6545 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6546 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6547 | } |
| 6548 | |
| 6549 | void PeerConnection::AddSctpDataStream(int sid) { |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6550 | if (data_channel_transport_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6551 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 6552 | if (data_channel_transport_) { |
| 6553 | data_channel_transport_->OpenChannel(sid); |
| 6554 | } |
| 6555 | }); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6556 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6557 | } |
| 6558 | |
| 6559 | void PeerConnection::RemoveSctpDataStream(int sid) { |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6560 | if (data_channel_transport_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6561 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 6562 | if (data_channel_transport_) { |
| 6563 | data_channel_transport_->CloseChannel(sid); |
| 6564 | } |
| 6565 | }); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6566 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6567 | } |
| 6568 | |
| 6569 | bool PeerConnection::ReadyToSendData() const { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6570 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6571 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6572 | (data_channel_transport_ && data_channel_transport_ready_to_send_); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6573 | } |
| 6574 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6575 | void PeerConnection::OnDataReceived(int channel_id, |
| 6576 | DataMessageType type, |
| 6577 | const rtc::CopyOnWriteBuffer& buffer) { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 6578 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6579 | cricket::ReceiveDataParams params; |
| 6580 | params.sid = channel_id; |
| 6581 | params.type = ToCricketDataMessageType(type); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6582 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6583 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 6584 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6585 | if (!HandleOpenMessage_s(params, buffer)) { |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6586 | SignalDataChannelTransportReceivedData_s(params, buffer); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6587 | } |
| 6588 | }); |
| 6589 | } |
| 6590 | |
| 6591 | void PeerConnection::OnChannelClosing(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 6592 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6593 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6594 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 6595 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6596 | SignalDataChannelTransportChannelClosing_s(channel_id); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6597 | }); |
| 6598 | } |
| 6599 | |
| 6600 | void PeerConnection::OnChannelClosed(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 6601 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6602 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6603 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 6604 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6605 | SignalDataChannelTransportChannelClosed_s(channel_id); |
| 6606 | }); |
| 6607 | } |
| 6608 | |
| 6609 | void PeerConnection::OnReadyToSend() { |
| 6610 | RTC_DCHECK_RUN_ON(network_thread()); |
| 6611 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 6612 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 6613 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6614 | data_channel_transport_ready_to_send_ = true; |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6615 | SignalDataChannelTransportWritable_s( |
| 6616 | data_channel_transport_ready_to_send_); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6617 | }); |
| 6618 | } |
| 6619 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6620 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6621 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6622 | if (sctp_mid_ && transport_controller_) { |
| 6623 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 6624 | if (dtls_transport) { |
| 6625 | return dtls_transport->transport_name(); |
| 6626 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6627 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6628 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6629 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6630 | } |
| 6631 | |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 6632 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 6633 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 6634 | network_thread()->Invoke<void>( |
| 6635 | RTC_FROM_HERE, |
| 6636 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 6637 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 6638 | return candidate_states_list; |
| 6639 | } |
| 6640 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6641 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 6642 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 6643 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6644 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6645 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6646 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6647 | if (channel) { |
| 6648 | transport_names_by_mid[channel->content_name()] = |
| 6649 | channel->transport_name(); |
| 6650 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6651 | } |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6652 | if (rtp_data_channel_) { |
| 6653 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 6654 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6655 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6656 | if (data_channel_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6657 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6658 | RTC_DCHECK(transport_name); |
| 6659 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6660 | } |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6661 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6662 | } |
| 6663 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6664 | std::map<std::string, cricket::TransportStats> |
| 6665 | PeerConnection::GetTransportStatsByNames( |
| 6666 | const std::set<std::string>& transport_names) { |
| 6667 | if (!network_thread()->IsCurrent()) { |
| 6668 | return network_thread() |
| 6669 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 6670 | RTC_FROM_HERE, |
| 6671 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6672 | } |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6673 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6674 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 6675 | for (const std::string& transport_name : transport_names) { |
| 6676 | cricket::TransportStats transport_stats; |
| 6677 | bool success = |
| 6678 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 6679 | if (success) { |
| 6680 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 6681 | } else { |
| 6682 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 6683 | << transport_name; |
| 6684 | } |
| 6685 | } |
| 6686 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6687 | } |
| 6688 | |
| 6689 | bool PeerConnection::GetLocalCertificate( |
| 6690 | const std::string& transport_name, |
| 6691 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6692 | if (!certificate) { |
| 6693 | return false; |
| 6694 | } |
| 6695 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 6696 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6697 | } |
| 6698 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 6699 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6700 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 6701 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 6705 | return data_channel_type_; |
| 6706 | } |
| 6707 | |
| 6708 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 13:36:53 | [diff] [blame] | 6709 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6710 | return pending_ice_restarts_.find(content_name) != |
| 6711 | pending_ice_restarts_.end(); |
| 6712 | } |
| 6713 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6714 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 6715 | return transport_controller_->NeedsIceRestart(content_name); |
| 6716 | } |
| 6717 | |
| 6718 | void PeerConnection::OnCertificateReady( |
| 6719 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 6720 | transport_controller_->SetLocalCertificate(certificate); |
| 6721 | } |
| 6722 | |
| 6723 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6724 | SetSessionError(SessionError::kTransport, |
| 6725 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6726 | } |
| 6727 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 6728 | void PeerConnection::OnTransportControllerConnectionState( |
| 6729 | cricket::IceConnectionState state) { |
| 6730 | switch (state) { |
| 6731 | case cricket::kIceConnectionConnecting: |
| 6732 | // If the current state is Connected or Completed, then there were |
| 6733 | // writable channels but now there are not, so the next state must |
| 6734 | // be Disconnected. |
| 6735 | // kIceConnectionConnecting is currently used as the default, |
| 6736 | // un-connected state by the TransportController, so its only use is |
| 6737 | // detecting disconnections. |
| 6738 | if (ice_connection_state_ == |
| 6739 | PeerConnectionInterface::kIceConnectionConnected || |
| 6740 | ice_connection_state_ == |
| 6741 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 6742 | SetIceConnectionState( |
| 6743 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 6744 | } |
| 6745 | break; |
| 6746 | case cricket::kIceConnectionFailed: |
| 6747 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 6748 | break; |
| 6749 | case cricket::kIceConnectionConnected: |
| 6750 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 6751 | "all transports are writable."; |
| 6752 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6753 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6754 | break; |
| 6755 | case cricket::kIceConnectionCompleted: |
| 6756 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 6757 | "all transports are complete."; |
| 6758 | if (ice_connection_state_ != |
| 6759 | PeerConnectionInterface::kIceConnectionConnected) { |
| 6760 | // If jumping directly from "checking" to "connected", |
| 6761 | // signal "connected" first. |
| 6762 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6763 | } |
| 6764 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 6765 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6766 | ReportTransportStats(); |
| 6767 | break; |
| 6768 | default: |
| 6769 | RTC_NOTREACHED(); |
| 6770 | } |
| 6771 | } |
| 6772 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6773 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 6774 | const std::string& transport_name, |
| 6775 | const cricket::Candidates& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6776 | int sdp_mline_index; |
| 6777 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6778 | RTC_LOG(LS_ERROR) |
| 6779 | << "OnTransportControllerCandidatesGathered: content name " |
| 6780 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6781 | return; |
| 6782 | } |
| 6783 | |
| 6784 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 6785 | citer != candidates.end(); ++citer) { |
| 6786 | // Use transport_name as the candidate media id. |
| 6787 | std::unique_ptr<JsepIceCandidate> candidate( |
| 6788 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 6789 | if (local_description()) { |
| 6790 | mutable_local_description()->AddCandidate(candidate.get()); |
| 6791 | } |
| 6792 | OnIceCandidate(std::move(candidate)); |
| 6793 | } |
| 6794 | } |
| 6795 | |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 6796 | void PeerConnection::OnTransportControllerCandidateError( |
| 6797 | const cricket::IceCandidateErrorEvent& event) { |
| 6798 | OnIceCandidateError(event.host_candidate, event.url, event.error_code, |
| 6799 | event.error_text); |
| 6800 | } |
| 6801 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6802 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 6803 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6804 | // Sanity check. |
| 6805 | for (const cricket::Candidate& candidate : candidates) { |
| 6806 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6807 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6808 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6809 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6810 | return; |
| 6811 | } |
| 6812 | } |
| 6813 | |
| 6814 | if (local_description()) { |
| 6815 | mutable_local_description()->RemoveCandidates(candidates); |
| 6816 | } |
| 6817 | OnIceCandidatesRemoved(candidates); |
| 6818 | } |
| 6819 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 6820 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 6821 | const cricket::CandidatePairChangeEvent& event) { |
| 6822 | OnSelectedCandidatePairChanged(event); |
| 6823 | } |
| 6824 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6825 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 6826 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6827 | RTC_HISTOGRAM_ENUMERATION( |
| 6828 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 6829 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6830 | } |
| 6831 | |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6832 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6833 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6834 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6835 | if (channel && !channel->enabled()) { |
| 6836 | channel->Enable(true); |
| 6837 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6838 | } |
| 6839 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6840 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6841 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6842 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | // Returns the media index for a local ice candidate given the content name. |
| 6846 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 6847 | const std::string& content_name, |
| 6848 | int* sdp_mline_index) { |
| 6849 | if (!local_description() || !sdp_mline_index) { |
| 6850 | return false; |
| 6851 | } |
| 6852 | |
| 6853 | bool content_found = false; |
| 6854 | const ContentInfos& contents = local_description()->description()->contents(); |
| 6855 | for (size_t index = 0; index < contents.size(); ++index) { |
| 6856 | if (contents[index].name == content_name) { |
| 6857 | *sdp_mline_index = static_cast<int>(index); |
| 6858 | content_found = true; |
| 6859 | break; |
| 6860 | } |
| 6861 | } |
| 6862 | return content_found; |
| 6863 | } |
| 6864 | |
| 6865 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 6866 | const SessionDescriptionInterface* remote_desc) { |
| 6867 | if (!remote_desc) { |
| 6868 | return true; |
| 6869 | } |
| 6870 | bool ret = true; |
| 6871 | |
| 6872 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 6873 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 6874 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 6875 | const IceCandidateInterface* candidate = candidates->at(n); |
| 6876 | bool valid = false; |
| 6877 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 6878 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6879 | RTC_LOG(LS_INFO) |
| 6880 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6881 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6882 | } |
| 6883 | continue; |
| 6884 | } |
| 6885 | ret = UseCandidate(candidate); |
| 6886 | if (!ret) { |
| 6887 | break; |
| 6888 | } |
| 6889 | } |
| 6890 | } |
| 6891 | return ret; |
| 6892 | } |
| 6893 | |
| 6894 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 6895 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 6896 | FindContentInfo(remote_description(), candidate); |
| 6897 | if (!result.ok()) { |
| 6898 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. " |
| 6899 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6900 | return false; |
| 6901 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6902 | std::vector<cricket::Candidate> candidates; |
| 6903 | candidates.push_back(candidate->candidate()); |
| 6904 | // Invoking BaseSession method to handle remote candidates. |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 6905 | RTCError error = transport_controller_->AddRemoteCandidates( |
| 6906 | result.value()->name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 | [diff] [blame] | 6907 | if (error.ok()) { |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 6908 | ReportRemoteIceCandidateAdded(candidate->candidate()); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 | [diff] [blame] | 6909 | // Candidates successfully submitted for checking. |
| 6910 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6911 | ice_connection_state_ == |
| 6912 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6913 | // If state is New, then the session has just gotten its first remote ICE |
| 6914 | // candidates, so go to Checking. |
| 6915 | // If state is Disconnected, the session is re-using old candidates or |
| 6916 | // receiving additional ones, so go to Checking. |
| 6917 | // If state is Connected, stay Connected. |
| 6918 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6919 | // newly added transport, then the state actually _should_ move to |
| 6920 | // checking. Add a way to distinguish that case. |
| 6921 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6922 | } |
| 6923 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 08:07:07 | [diff] [blame] | 6924 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6925 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6926 | } |
| 6927 | return true; |
| 6928 | } |
| 6929 | |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 6930 | RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo( |
| 6931 | const SessionDescriptionInterface* description, |
| 6932 | const IceCandidateInterface* candidate) { |
| 6933 | if (candidate->sdp_mline_index() >= 0) { |
| 6934 | size_t mediacontent_index = |
| 6935 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 6936 | size_t content_size = description->description()->contents().size(); |
| 6937 | if (mediacontent_index < content_size) { |
| 6938 | return &description->description()->contents()[mediacontent_index]; |
| 6939 | } else { |
| 6940 | return RTCError(RTCErrorType::INVALID_RANGE, |
| 6941 | "Media line index (" + |
| 6942 | rtc::ToString(candidate->sdp_mline_index()) + |
| 6943 | ") out of range (number of mlines: " + |
| 6944 | rtc::ToString(content_size) + ")."); |
| 6945 | } |
| 6946 | } else if (!candidate->sdp_mid().empty()) { |
| 6947 | auto& contents = description->description()->contents(); |
| 6948 | auto it = absl::c_find_if( |
| 6949 | contents, [candidate](const cricket::ContentInfo& content_info) { |
| 6950 | return content_info.mid() == candidate->sdp_mid(); |
| 6951 | }); |
| 6952 | if (it == contents.end()) { |
| 6953 | return RTCError( |
| 6954 | RTCErrorType::INVALID_PARAMETER, |
| 6955 | "Mid " + candidate->sdp_mid() + |
| 6956 | " specified but no media section with that mid found."); |
| 6957 | } else { |
| 6958 | return &*it; |
| 6959 | } |
| 6960 | } |
| 6961 | |
| 6962 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 6963 | "Neither sdp_mline_index nor sdp_mid specified."); |
| 6964 | } |
| 6965 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6966 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6967 | // Destroy video channel first since it may have a pointer to the |
| 6968 | // voice channel. |
| 6969 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 6970 | if (!video_info || video_info->rejected) { |
| 6971 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6972 | } |
| 6973 | |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 6974 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6975 | if (!audio_info || audio_info->rejected) { |
| 6976 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6977 | } |
| 6978 | |
| 6979 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6980 | if (!data_info || data_info->rejected) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 6981 | DestroyDataChannelTransport(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6982 | } |
| 6983 | } |
| 6984 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6985 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6986 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6987 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6988 | if (configuration_.bundle_policy == |
| 6989 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6990 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6991 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6992 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6993 | "max-bundle configured but session description " |
| 6994 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6995 | } |
| 6996 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 12:47:42 | [diff] [blame] | 6997 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6998 | } |
| 6999 | |
| 7000 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7001 | // Creating the media channels. Transports should already have been created |
| 7002 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 7003 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7004 | if (voice && !voice->rejected && |
| 7005 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7006 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7007 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7008 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 7009 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7010 | } |
| 7011 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 7012 | } |
| 7013 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 7014 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7015 | if (video && !video->rejected && |
| 7016 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7017 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7018 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7019 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 7020 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7021 | } |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7022 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7023 | } |
| 7024 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 7025 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7026 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7027 | !rtp_data_channel_ && !data_channel_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7028 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7029 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 7030 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7031 | } |
| 7032 | } |
| 7033 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7034 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7035 | } |
| 7036 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 7037 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7038 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7039 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 7040 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 7041 | MediaTransportConfig media_transport_config = |
| 7042 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 7043 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7044 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 18:12:57 | [diff] [blame] | 7045 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 7046 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 7047 | GetCryptoOptions(), &ssrc_generator_, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7048 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7049 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7050 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7051 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 7052 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7053 | voice_channel->SignalSentPacket.connect(this, |
| 7054 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7055 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 7056 | |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7057 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7058 | } |
| 7059 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 7060 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7061 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7062 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 7063 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 7064 | MediaTransportConfig media_transport_config = |
| 7065 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 7066 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7067 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 18:12:57 | [diff] [blame] | 7068 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 7069 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 7070 | GetCryptoOptions(), &ssrc_generator_, video_options_, |
Anton Sukhanov | 4f08faa | 2019-05-21 18:12:57 | [diff] [blame] | 7071 | video_bitrate_allocator_factory_.get()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7072 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7073 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7074 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7075 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 7076 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7077 | video_channel->SignalSentPacket.connect(this, |
| 7078 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7079 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 7080 | |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 7081 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7082 | } |
| 7083 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7084 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7085 | switch (data_channel_type_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7086 | case cricket::DCT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 | [diff] [blame] | 7087 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 | [diff] [blame] | 7088 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
| 7089 | case cricket::DCT_MEDIA_TRANSPORT: |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7090 | if (!network_thread()->Invoke<bool>( |
| 7091 | RTC_FROM_HERE, |
| 7092 | rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this, |
| 7093 | mid))) { |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 | [diff] [blame] | 7094 | return false; |
| 7095 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7096 | |
| 7097 | // All non-RTP data channels must initialize |sctp_data_channels_|. |
| 7098 | for (const auto& channel : sctp_data_channels_) { |
| 7099 | channel->OnTransportChannelCreated(); |
| 7100 | } |
| 7101 | return true; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7102 | case cricket::DCT_RTP: |
| 7103 | default: |
| 7104 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 7105 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 7106 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 7107 | SrtpRequired(), GetCryptoOptions(), &ssrc_generator_); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7108 | if (!rtp_data_channel_) { |
| 7109 | return false; |
| 7110 | } |
| 7111 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 7112 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 7113 | rtp_data_channel_->SignalSentPacket.connect( |
| 7114 | this, &PeerConnection::OnSentPacket_w); |
| 7115 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 7116 | return true; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7117 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7118 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7119 | } |
| 7120 | |
| 7121 | Call::Stats PeerConnection::GetCallStats() { |
| 7122 | if (!worker_thread()->IsCurrent()) { |
| 7123 | return worker_thread()->Invoke<Call::Stats>( |
| 7124 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 7125 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 7126 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7127 | if (call_) { |
| 7128 | return call_->GetStats(); |
| 7129 | } else { |
| 7130 | return Call::Stats(); |
| 7131 | } |
| 7132 | } |
| 7133 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7134 | bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7135 | DataChannelTransportInterface* transport = |
| 7136 | transport_controller_->GetDataChannelTransport(mid); |
| 7137 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 7138 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7139 | << "Data channel transport is not available for data channels, mid=" |
| 7140 | << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7141 | return false; |
| 7142 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7143 | RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7144 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7145 | data_channel_transport_ = transport; |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 7146 | data_channel_transport_invoker_ = std::make_unique<rtc::AsyncInvoker>(); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7147 | sctp_mid_ = mid; |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7148 | |
| 7149 | // Note: setting the data sink and checking initial state must be done last, |
| 7150 | // after setting up the data channel. Setting the data sink may trigger |
| 7151 | // callbacks to PeerConnection which require the transport to be completely |
| 7152 | // set up (eg. OnReadyToSend()). |
| 7153 | transport->SetDataSink(this); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7154 | return true; |
| 7155 | } |
| 7156 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7157 | void PeerConnection::TeardownDataChannelTransport_n() { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7158 | if (!sctp_mid_ && !data_channel_transport_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7159 | return; |
| 7160 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7161 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
| 7162 | << *sctp_mid_; |
| 7163 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7164 | // |sctp_mid_| may still be active through an SCTP transport. If not, unset |
| 7165 | // it. |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7166 | sctp_mid_.reset(); |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 | [diff] [blame] | 7167 | data_channel_transport_invoker_ = nullptr; |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7168 | if (data_channel_transport_) { |
| 7169 | data_channel_transport_->SetDataSink(nullptr); |
| 7170 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7171 | data_channel_transport_ = nullptr; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7172 | } |
| 7173 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7174 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 7175 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 7176 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 7177 | if (!bundle_enabled) |
| 7178 | return true; |
| 7179 | |
| 7180 | const cricket::ContentGroup* bundle_group = |
| 7181 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 7182 | RTC_DCHECK(bundle_group != NULL); |
| 7183 | |
| 7184 | const cricket::ContentInfos& contents = desc->contents(); |
| 7185 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 7186 | citer != contents.end(); ++citer) { |
| 7187 | const cricket::ContentInfo* content = (&*citer); |
| 7188 | RTC_DCHECK(content != NULL); |
| 7189 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 7190 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7191 | if (!HasRtcpMuxEnabled(content)) |
| 7192 | return false; |
| 7193 | } |
| 7194 | } |
| 7195 | // RTCP-MUX is enabled in all the contents. |
| 7196 | return true; |
| 7197 | } |
| 7198 | |
| 7199 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 7200 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7201 | } |
| 7202 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 7203 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 7204 | std::set<std::string> mids; |
| 7205 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 19:32:20 | [diff] [blame] | 7206 | if (content.name.empty()) { |
| 7207 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 7208 | "A media section is missing a MID attribute."); |
| 7209 | } |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 7210 | if (!mids.insert(content.name).second) { |
| 7211 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 7212 | "Duplicate a=mid value '" + content.name + "'."); |
| 7213 | } |
| 7214 | } |
| 7215 | return RTCError::OK(); |
| 7216 | } |
| 7217 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7218 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7219 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7220 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 7221 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7222 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7226 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7227 | } |
| 7228 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7229 | SdpType type = sdesc->GetType(); |
| 7230 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 7231 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7232 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 7233 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7234 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7235 | } |
| 7236 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 7237 | RTCError error = ValidateMids(*sdesc->description()); |
| 7238 | if (!error.ok()) { |
| 7239 | return error; |
| 7240 | } |
| 7241 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7242 | // Verify crypto settings. |
| 7243 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7244 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 7245 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7246 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7247 | if (!crypto_error.ok()) { |
| 7248 | return crypto_error; |
| 7249 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | // Verify ice-ufrag and ice-pwd. |
| 7253 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7254 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 7255 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7256 | } |
| 7257 | |
| 7258 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7259 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 7260 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 7264 | // m-lines that do not rtcp-mux enabled. |
| 7265 | |
| 7266 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7267 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 7268 | // With an answer we want to compare the new answer session description with |
| 7269 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7270 | const cricket::SessionDescription* offer_desc = |
| 7271 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 7272 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 7273 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 7274 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 7275 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7276 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 7277 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7278 | } |
| 7279 | } else { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7280 | // The re-offers should respect the order of m= sections in current |
| 7281 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 7282 | // With a re-offer, either the current local or current remote descriptions |
| 7283 | // could be the most up to date, so we would like to check against both of |
| 7284 | // them if they exist. It could be the case that one of them has a 0 port |
| 7285 | // for a media section, but the other does not. This is important to check |
| 7286 | // against in the case that we are recycling an m= section. |
| 7287 | const cricket::SessionDescription* current_desc = nullptr; |
| 7288 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 7289 | if (local_description()) { |
| 7290 | current_desc = local_description()->description(); |
| 7291 | if (remote_description()) { |
| 7292 | secondary_current_desc = remote_description()->description(); |
| 7293 | } |
| 7294 | } else if (remote_description()) { |
| 7295 | current_desc = remote_description()->description(); |
| 7296 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7297 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 7298 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 7299 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7300 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 7301 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7302 | } |
| 7303 | } |
| 7304 | |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 7305 | if (IsUnifiedPlan()) { |
| 7306 | // Ensure that each audio and video media section has at most one |
| 7307 | // "StreamParams". This will return an error if receiving a session |
| 7308 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 7309 | // same type. With Unified Plan, there can only be at most one track per |
| 7310 | // media section. |
| 7311 | for (const ContentInfo& content : sdesc->description()->contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 7312 | const MediaContentDescription& desc = *content.media_description(); |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 7313 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 7314 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 7315 | desc.streams().size() > 1u) { |
| 7316 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 7317 | "Media section has more than one track specified " |
| 7318 | "with a=ssrc lines which is not supported with " |
| 7319 | "Unified Plan."); |
| 7320 | } |
| 7321 | } |
| 7322 | } |
| 7323 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 7324 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7325 | } |
| 7326 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7327 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7328 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7329 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7330 | return (state == PeerConnectionInterface::kStable) || |
| 7331 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-05 00:24:52 | [diff] [blame] | 7332 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7333 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7334 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 7335 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 7336 | } |
| 7337 | } |
| 7338 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7339 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7340 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7341 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7342 | return (state == PeerConnectionInterface::kStable) || |
| 7343 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-05 00:24:52 | [diff] [blame] | 7344 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 7345 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7346 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 7347 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 7348 | } |
| 7349 | } |
| 7350 | |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 7351 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 7352 | switch (error) { |
| 7353 | case SessionError::kNone: |
| 7354 | return "ERROR_NONE"; |
| 7355 | case SessionError::kContent: |
| 7356 | return "ERROR_CONTENT"; |
| 7357 | case SessionError::kTransport: |
| 7358 | return "ERROR_TRANSPORT"; |
| 7359 | } |
| 7360 | RTC_NOTREACHED(); |
| 7361 | return ""; |
| 7362 | } |
| 7363 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7364 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 7365 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 7366 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 7367 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 7368 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7369 | } |
| 7370 | |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 7371 | void PeerConnection::ReportSdpFormatReceived( |
| 7372 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 7373 | int num_audio_mlines = 0; |
| 7374 | int num_video_mlines = 0; |
| 7375 | int num_audio_tracks = 0; |
| 7376 | int num_video_tracks = 0; |
| 7377 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 7378 | cricket::MediaType media_type = content.media_description()->type(); |
| 7379 | int num_tracks = std::max( |
| 7380 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 7381 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 7382 | num_audio_mlines += 1; |
| 7383 | num_audio_tracks += num_tracks; |
| 7384 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 7385 | num_video_mlines += 1; |
| 7386 | num_video_tracks += num_tracks; |
| 7387 | } |
| 7388 | } |
| 7389 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 7390 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 7391 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 7392 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 7393 | format = kSdpFormatReceivedComplexPlanB; |
| 7394 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 7395 | format = kSdpFormatReceivedSimple; |
| 7396 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7397 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 7398 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 7399 | } |
| 7400 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 7401 | void PeerConnection::ReportIceCandidateCollected( |
| 7402 | const cricket::Candidate& candidate) { |
| 7403 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 7404 | if (candidate.address().IsPrivateIP()) { |
| 7405 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 7406 | } |
| 7407 | if (candidate.address().IsUnresolvedIP()) { |
| 7408 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 7409 | } |
| 7410 | if (candidate.address().family() == AF_INET6) { |
| 7411 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 7412 | } |
| 7413 | } |
| 7414 | |
| 7415 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 7416 | const cricket::Candidate& candidate) { |
| 7417 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 7418 | if (candidate.address().IsPrivateIP()) { |
| 7419 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 7420 | } |
| 7421 | if (candidate.address().IsUnresolvedIP()) { |
| 7422 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 7423 | } |
| 7424 | if (candidate.address().family() == AF_INET6) { |
| 7425 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 7426 | } |
| 7427 | } |
| 7428 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 7429 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 7430 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7431 | usage_event_accumulator_ |= static_cast<int>(event); |
| 7432 | } |
| 7433 | |
| 7434 | void PeerConnection::ReportUsagePattern() const { |
| 7435 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7436 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 7437 | usage_event_accumulator_, |
| 7438 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 7439 | const int bad_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 7440 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 7441 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 7442 | const int good_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 7443 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 7444 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 7445 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 7446 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 7447 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7448 | // If called after close(), we can't report, because observer may have |
| 7449 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 7450 | if (observer_) { |
| 7451 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 7452 | } else { |
| 7453 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 7454 | << usage_event_accumulator_ |
| 7455 | << " observed after observer shutdown"; |
| 7456 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 7457 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 7458 | } |
| 7459 | |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7460 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 7461 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7462 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7463 | switch (answer.description()->msid_signaling()) { |
| 7464 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7465 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7466 | break; |
| 7467 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7468 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7469 | break; |
| 7470 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7471 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7472 | break; |
| 7473 | case cricket::kMsidSignalingMediaSection | |
| 7474 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7475 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7476 | break; |
| 7477 | default: |
| 7478 | RTC_NOTREACHED(); |
| 7479 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7480 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 7481 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7482 | } |
| 7483 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7484 | // We need to check the local/remote description for the Transport instead of |
| 7485 | // the session, because a new Transport added during renegotiation may have |
| 7486 | // them unset while the session has them set from the previous negotiation. |
| 7487 | // Not doing so may trigger the auto generation of transport description and |
| 7488 | // mess up DTLS identity information, ICE credential, etc. |
| 7489 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 7490 | const IceCandidateInterface* candidate, |
| 7491 | const SessionDescriptionInterface* remote_desc, |
| 7492 | bool* valid) { |
| 7493 | *valid = true; |
| 7494 | |
| 7495 | const SessionDescriptionInterface* current_remote_desc = |
| 7496 | remote_desc ? remote_desc : remote_description(); |
| 7497 | |
| 7498 | if (!current_remote_desc) { |
| 7499 | return false; |
| 7500 | } |
| 7501 | |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 7502 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 7503 | FindContentInfo(current_remote_desc, candidate); |
| 7504 | if (!result.ok()) { |
| 7505 | RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. " |
| 7506 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7507 | |
| 7508 | *valid = false; |
| 7509 | return false; |
| 7510 | } |
| 7511 | |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 7512 | std::string transport_name = GetTransportName(result.value()->name); |
| 7513 | return !transport_name.empty(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7514 | } |
| 7515 | |
| 7516 | bool PeerConnection::SrtpRequired() const { |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 7517 | return !use_datagram_transport_ && |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 7518 | (dtls_enabled_ || |
| 7519 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7520 | } |
| 7521 | |
| 7522 | void PeerConnection::OnTransportControllerGatheringState( |
| 7523 | cricket::IceGatheringState state) { |
| 7524 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 7525 | if (state == cricket::kIceGatheringGathering) { |
| 7526 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 7527 | } else if (state == cricket::kIceGatheringComplete) { |
| 7528 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 7529 | } |
| 7530 | } |
| 7531 | |
| 7532 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7533 | std::map<std::string, std::set<cricket::MediaType>> |
| 7534 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 7535 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7536 | if (transceiver->internal()->channel()) { |
| 7537 | const std::string& transport_name = |
| 7538 | transceiver->internal()->channel()->transport_name(); |
| 7539 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 7540 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7541 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7542 | } |
| 7543 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7544 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 7545 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7546 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7547 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 7548 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7549 | if (transport_name) { |
| 7550 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7551 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7552 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7553 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7554 | for (const auto& entry : media_types_by_transport_name) { |
| 7555 | const std::string& transport_name = entry.first; |
| 7556 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7557 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7558 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7559 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7560 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7561 | } |
| 7562 | } |
| 7563 | } |
| 7564 | // Walk through the ConnectionInfos to gather best connection usage |
| 7565 | // for IPv4 and IPv6. |
| 7566 | void PeerConnection::ReportBestConnectionState( |
| 7567 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7568 | for (const cricket::TransportChannelStats& channel_stats : |
| 7569 | stats.channel_stats) { |
| 7570 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 06:10:27 | [diff] [blame] | 7571 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7572 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7573 | continue; |
| 7574 | } |
| 7575 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7576 | const cricket::Candidate& local = connection_info.local_candidate; |
| 7577 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7578 | |
| 7579 | // Increment the counter for IceCandidatePairType. |
| 7580 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 7581 | (local.type() == RELAY_PORT_TYPE && |
| 7582 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7583 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 7584 | GetIceCandidatePairCounter(local, remote), |
| 7585 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7586 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7587 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 7588 | GetIceCandidatePairCounter(local, remote), |
| 7589 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7590 | } else { |
| 7591 | RTC_CHECK(0); |
| 7592 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7593 | |
| 7594 | // Increment the counter for IP type. |
| 7595 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7596 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7597 | kBestConnections_IPv4, |
| 7598 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7599 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7600 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7601 | kBestConnections_IPv6, |
| 7602 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7603 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 7604 | RTC_CHECK(!local.address().hostname().empty() && |
| 7605 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7606 | } |
| 7607 | |
| 7608 | return; |
| 7609 | } |
| 7610 | } |
| 7611 | } |
| 7612 | |
| 7613 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7614 | const cricket::TransportStats& stats, |
| 7615 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7616 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 7617 | return; |
| 7618 | } |
| 7619 | |
| 7620 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 7621 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 7622 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 7623 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7624 | return; |
| 7625 | } |
| 7626 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7627 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 7628 | for (cricket::MediaType media_type : media_types) { |
| 7629 | switch (media_type) { |
| 7630 | case cricket::MEDIA_TYPE_AUDIO: |
| 7631 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7632 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 7633 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7634 | break; |
| 7635 | case cricket::MEDIA_TYPE_VIDEO: |
| 7636 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7637 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 7638 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7639 | break; |
| 7640 | case cricket::MEDIA_TYPE_DATA: |
| 7641 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7642 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 7643 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7644 | break; |
| 7645 | default: |
| 7646 | RTC_NOTREACHED(); |
| 7647 | continue; |
| 7648 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7649 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7650 | } |
| 7651 | |
| 7652 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7653 | for (cricket::MediaType media_type : media_types) { |
| 7654 | switch (media_type) { |
| 7655 | case cricket::MEDIA_TYPE_AUDIO: |
| 7656 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7657 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 7658 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7659 | break; |
| 7660 | case cricket::MEDIA_TYPE_VIDEO: |
| 7661 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7662 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 7663 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7664 | break; |
| 7665 | case cricket::MEDIA_TYPE_DATA: |
| 7666 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7667 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 7668 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7669 | break; |
| 7670 | default: |
| 7671 | RTC_NOTREACHED(); |
| 7672 | continue; |
| 7673 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7674 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7675 | } |
| 7676 | } |
| 7677 | |
| 7678 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 7679 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7680 | RTC_DCHECK(call_); |
| 7681 | call_->OnSentPacket(sent_packet); |
| 7682 | } |
| 7683 | |
| 7684 | const std::string PeerConnection::GetTransportName( |
| 7685 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7686 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7687 | if (channel) { |
| 7688 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7689 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7690 | if (data_channel_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7691 | RTC_DCHECK(sctp_mid_); |
| 7692 | if (content_name == *sctp_mid_) { |
| 7693 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7694 | } |
| 7695 | } |
| 7696 | // Return an empty string if failed to retrieve the transport name. |
| 7697 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7698 | } |
| 7699 | |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7700 | void PeerConnection::DestroyTransceiverChannel( |
| 7701 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 7702 | transceiver) { |
| 7703 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7704 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7705 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7706 | if (channel) { |
| 7707 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7708 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7709 | } |
| 7710 | } |
| 7711 | |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 7712 | void PeerConnection::DestroyDataChannelTransport() { |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7713 | if (rtp_data_channel_) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 7714 | OnTransportChannelClosed(); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7715 | DestroyChannelInterface(rtp_data_channel_); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7716 | rtp_data_channel_ = nullptr; |
| 7717 | } |
| 7718 | |
| 7719 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 7720 | // grab a reference to this PeerConnection. If this is called from the |
| 7721 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 7722 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 7723 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 7724 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7725 | if (sctp_mid_) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame^] | 7726 | OnTransportChannelClosed(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7727 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 7728 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7729 | TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7730 | }); |
| 7731 | } |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7732 | } |
| 7733 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7734 | void PeerConnection::DestroyChannelInterface( |
| 7735 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7736 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7737 | switch (channel->media_type()) { |
| 7738 | case cricket::MEDIA_TYPE_AUDIO: |
| 7739 | channel_manager()->DestroyVoiceChannel( |
| 7740 | static_cast<cricket::VoiceChannel*>(channel)); |
| 7741 | break; |
| 7742 | case cricket::MEDIA_TYPE_VIDEO: |
| 7743 | channel_manager()->DestroyVideoChannel( |
| 7744 | static_cast<cricket::VideoChannel*>(channel)); |
| 7745 | break; |
| 7746 | case cricket::MEDIA_TYPE_DATA: |
| 7747 | channel_manager()->DestroyRtpDataChannel( |
| 7748 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 7749 | break; |
| 7750 | default: |
| 7751 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 7752 | break; |
| 7753 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7754 | } |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7755 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7756 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7757 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7758 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 7759 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7760 | MediaTransportInterface* media_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7761 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 12:08:37 | [diff] [blame] | 7762 | RTC_DCHECK_RUN_ON(network_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 7763 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7764 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7765 | auto base_channel = GetChannel(mid); |
| 7766 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7767 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7768 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 16:26:19 | [diff] [blame] | 7769 | |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 7770 | if (use_media_transport_) { |
Tommi | 78a7138 | 2019-08-08 10:27:53 | [diff] [blame] | 7771 | RTC_LOG(LS_ERROR) << "Media transport isn't supported."; |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 21:31:15 | [diff] [blame] | 7772 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 16:26:19 | [diff] [blame] | 7773 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7774 | if (data_channel_transport_ && mid == sctp_mid_ && |
| 7775 | data_channel_transport_ != data_channel_transport) { |
| 7776 | // Changed which data channel transport is used for |sctp_mid_| (eg. now |
| 7777 | // it's bundled). |
| 7778 | data_channel_transport_->SetDataSink(nullptr); |
| 7779 | data_channel_transport_ = data_channel_transport; |
| 7780 | if (data_channel_transport) { |
| 7781 | data_channel_transport->SetDataSink(this); |
| 7782 | |
| 7783 | // There's a new data channel transport. This needs to be signaled to the |
| 7784 | // |sctp_data_channels_| so that they can reopen and reconnect. This is |
| 7785 | // necessary when bundling is applied. |
| 7786 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 7787 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 7788 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7789 | for (auto channel : sctp_data_channels_) { |
| 7790 | channel->OnTransportChannelCreated(); |
| 7791 | } |
| 7792 | }); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7793 | } |
| 7794 | } |
| 7795 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7796 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7797 | } |
| 7798 | |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 7799 | void PeerConnection::OnSetStreams() { |
| 7800 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7801 | if (IsUnifiedPlan()) |
| 7802 | UpdateNegotiationNeeded(); |
| 7803 | } |
| 7804 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7805 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 7806 | // In earlier production code, the pointer was not cleared on close, |
| 7807 | // which might have led to undefined behavior if the observer was not |
| 7808 | // deallocated, or strange crashes if it was. |
| 7809 | // We use CHECK in order to catch such behavior if it exists. |
| 7810 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 7811 | RTC_CHECK(observer_); |
| 7812 | return observer_; |
| 7813 | } |
| 7814 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 7815 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 7816 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 7817 | // after it has been removed. |
| 7818 | return configuration_.crypto_options.has_value() |
| 7819 | ? *configuration_.crypto_options |
| 7820 | : factory_->options().crypto_options; |
| 7821 | } |
| 7822 | |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 7823 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 7824 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 7825 | if (stats_collector_) { |
| 7826 | stats_collector_->ClearCachedStatsReport(); |
| 7827 | } |
| 7828 | } |
| 7829 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7830 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 7831 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 7832 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7833 | } |
| 7834 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7835 | void PeerConnection::UpdateNegotiationNeeded() { |
| 7836 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7837 | if (!IsUnifiedPlan()) { |
| 7838 | Observer()->OnRenegotiationNeeded(); |
| 7839 | return; |
| 7840 | } |
| 7841 | |
| 7842 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7843 | if (IsClosed()) |
| 7844 | return; |
| 7845 | |
| 7846 | // If connection's signaling state is not "stable", abort these steps. |
| 7847 | if (signaling_state() != kStable) |
| 7848 | return; |
| 7849 | |
| 7850 | // NOTE |
| 7851 | // The negotiation-needed flag will be updated once the state transitions to |
| 7852 | // "stable", as part of the steps for setting an RTCSessionDescription. |
| 7853 | |
| 7854 | // If the result of checking if negotiation is needed is false, clear the |
| 7855 | // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot |
| 7856 | // to false, and abort these steps. |
| 7857 | bool is_negotiation_needed = CheckIfNegotiationIsNeeded(); |
| 7858 | if (!is_negotiation_needed) { |
| 7859 | is_negotiation_needed_ = false; |
| 7860 | return; |
| 7861 | } |
| 7862 | |
| 7863 | // If connection's [[NegotiationNeeded]] slot is already true, abort these |
| 7864 | // steps. |
| 7865 | if (is_negotiation_needed_) |
| 7866 | return; |
| 7867 | |
| 7868 | // Set connection's [[NegotiationNeeded]] slot to true. |
| 7869 | is_negotiation_needed_ = true; |
| 7870 | |
| 7871 | // Queue a task that runs the following steps: |
| 7872 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7873 | // If connection's [[NegotiationNeeded]] slot is false, abort these steps. |
| 7874 | // Fire an event named negotiationneeded at connection. |
| 7875 | Observer()->OnRenegotiationNeeded(); |
| 7876 | } |
| 7877 | |
| 7878 | bool PeerConnection::CheckIfNegotiationIsNeeded() { |
| 7879 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7880 | // 1. If any implementation-specific negotiation is required, as described at |
| 7881 | // the start of this section, return true. |
| 7882 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7883 | // 2. If connection's [[RestartIce]] internal slot is true, return true. |
| 7884 | if (local_ice_credentials_to_replace_->HasIceCredentials()) { |
| 7885 | return true; |
| 7886 | } |
| 7887 | |
| 7888 | // 3. Let description be connection.[[CurrentLocalDescription]]. |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7889 | const SessionDescriptionInterface* description = current_local_description(); |
| 7890 | if (!description) |
| 7891 | return true; |
| 7892 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7893 | // 4. If connection has created any RTCDataChannels, and no m= section in |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7894 | // description has been negotiated yet for data, return true. |
| 7895 | if (!sctp_data_channels_.empty()) { |
| 7896 | if (!cricket::GetFirstDataContent(description->description()->contents())) |
| 7897 | return true; |
| 7898 | } |
| 7899 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7900 | // 5. For each transceiver in connection's set of transceivers, perform the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7901 | // following checks: |
| 7902 | for (const auto& transceiver : transceivers_) { |
| 7903 | const ContentInfo* current_local_msection = |
| 7904 | FindTransceiverMSection(transceiver.get(), description); |
| 7905 | |
| 7906 | const ContentInfo* current_remote_msection = FindTransceiverMSection( |
| 7907 | transceiver.get(), current_remote_description()); |
| 7908 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7909 | // 5.3 If transceiver is stopped and is associated with an m= section, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7910 | // but the associated m= section is not yet rejected in |
| 7911 | // connection.[[CurrentLocalDescription]] or |
| 7912 | // connection.[[CurrentRemoteDescription]], return true. |
| 7913 | if (transceiver->stopped()) { |
| 7914 | if (current_local_msection && !current_local_msection->rejected && |
| 7915 | ((current_remote_msection && !current_remote_msection->rejected) || |
| 7916 | !current_remote_msection)) { |
| 7917 | return true; |
| 7918 | } |
| 7919 | continue; |
| 7920 | } |
| 7921 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7922 | // 5.1 If transceiver isn't stopped and isn't yet associated with an m= |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7923 | // section in description, return true. |
| 7924 | if (!current_local_msection) |
| 7925 | return true; |
| 7926 | |
| 7927 | const MediaContentDescription* current_local_media_description = |
| 7928 | current_local_msection->media_description(); |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7929 | // 5.2 If transceiver isn't stopped and is associated with an m= section |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7930 | // in description then perform the following checks: |
| 7931 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7932 | // 5.2.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7933 | // associated m= section in description either doesn't contain a single |
| 7934 | // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this |
| 7935 | // m= section, or the MSID values themselves, differ from what is in |
| 7936 | // transceiver.sender.[[AssociatedMediaStreamIds]], return true. |
| 7937 | if (RtpTransceiverDirectionHasSend(transceiver->direction())) { |
| 7938 | if (current_local_media_description->streams().size() == 0) |
| 7939 | return true; |
| 7940 | |
| 7941 | std::vector<std::string> msection_msids; |
| 7942 | for (const auto& stream : current_local_media_description->streams()) { |
| 7943 | for (const std::string& msid : stream.stream_ids()) |
| 7944 | msection_msids.push_back(msid); |
| 7945 | } |
| 7946 | |
| 7947 | std::vector<std::string> transceiver_msids = |
| 7948 | transceiver->sender()->stream_ids(); |
| 7949 | if (msection_msids.size() != transceiver_msids.size()) |
| 7950 | return true; |
| 7951 | |
| 7952 | absl::c_sort(transceiver_msids); |
| 7953 | absl::c_sort(msection_msids); |
| 7954 | if (transceiver_msids != msection_msids) |
| 7955 | return true; |
| 7956 | } |
| 7957 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7958 | // 5.2.2 If description is of type "offer", and the direction of the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7959 | // associated m= section in neither connection.[[CurrentLocalDescription]] |
| 7960 | // nor connection.[[CurrentRemoteDescription]] matches |
| 7961 | // transceiver.[[Direction]], return true. |
| 7962 | if (description->GetType() == SdpType::kOffer) { |
| 7963 | if (!current_remote_description()) |
| 7964 | return true; |
| 7965 | |
| 7966 | if (!current_remote_msection) |
| 7967 | return true; |
| 7968 | |
| 7969 | RtpTransceiverDirection current_local_direction = |
| 7970 | current_local_media_description->direction(); |
| 7971 | RtpTransceiverDirection current_remote_direction = |
| 7972 | current_remote_msection->media_description()->direction(); |
| 7973 | if (transceiver->direction() != current_local_direction && |
| 7974 | transceiver->direction() != |
| 7975 | RtpTransceiverDirectionReversed(current_remote_direction)) { |
| 7976 | return true; |
| 7977 | } |
| 7978 | } |
| 7979 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7980 | // 5.2.3 If description is of type "answer", and the direction of the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7981 | // associated m= section in the description does not match |
| 7982 | // transceiver.[[Direction]] intersected with the offered direction (as |
| 7983 | // described in [JSEP] (section 5.3.1.)), return true. |
| 7984 | if (description->GetType() == SdpType::kAnswer) { |
| 7985 | if (!remote_description()) |
| 7986 | return true; |
| 7987 | |
| 7988 | const ContentInfo* offered_remote_msection = |
| 7989 | FindTransceiverMSection(transceiver.get(), remote_description()); |
| 7990 | |
| 7991 | RtpTransceiverDirection offered_direction = |
| 7992 | offered_remote_msection |
| 7993 | ? offered_remote_msection->media_description()->direction() |
| 7994 | : RtpTransceiverDirection::kInactive; |
| 7995 | |
| 7996 | if (current_local_media_description->direction() != |
| 7997 | (RtpTransceiverDirectionIntersection( |
| 7998 | transceiver->direction(), |
| 7999 | RtpTransceiverDirectionReversed(offered_direction)))) { |
| 8000 | return true; |
| 8001 | } |
| 8002 | } |
| 8003 | } |
| 8004 | |
| 8005 | // If all the preceding checks were performed and true was not returned, |
| 8006 | // nothing remains to be negotiated; return false. |
| 8007 | return false; |
| 8008 | } |
| 8009 | |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 8010 | RTCError PeerConnection::Rollback(SdpType sdp_type) { |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 8011 | auto state = signaling_state(); |
| 8012 | if (state != PeerConnectionInterface::kHaveLocalOffer && |
| 8013 | state != PeerConnectionInterface::kHaveRemoteOffer) { |
| 8014 | return RTCError(RTCErrorType::INVALID_STATE, |
| 8015 | "Called in wrong signalingState: " + |
| 8016 | GetSignalingStateString(signaling_state())); |
| 8017 | } |
| 8018 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 8019 | RTC_DCHECK(IsUnifiedPlan()); |
| 8020 | |
| 8021 | for (auto&& transceivers_stable_state_pair : |
| 8022 | transceiver_stable_states_by_transceivers_) { |
| 8023 | auto transceiver = transceivers_stable_state_pair.first; |
| 8024 | auto state = transceivers_stable_state_pair.second; |
| 8025 | RTC_DCHECK(transceiver->internal()->mid().has_value()); |
| 8026 | std::string mid = transceiver->internal()->mid().value(); |
| 8027 | transport_controller_->RollbackTransportForMid(mid); |
| 8028 | DestroyTransceiverChannel(transceiver); |
| 8029 | |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 8030 | if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer && |
| 8031 | transceiver->receiver()) { |
| 8032 | Observer()->OnRemoveTrack(transceiver->receiver()); |
| 8033 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 8034 | if (state.newly_created()) { |
| 8035 | // Remove added transceivers with no added track. |
| 8036 | if (transceiver->internal()->sender()->track()) { |
| 8037 | transceiver->internal()->set_created_by_addtrack(true); |
| 8038 | } else { |
| 8039 | int remaining_transceiver_count = 0; |
| 8040 | for (auto&& t : transceivers_) { |
| 8041 | if (t != transceiver) { |
| 8042 | transceivers_[remaining_transceiver_count++] = t; |
| 8043 | } |
| 8044 | } |
| 8045 | transceivers_.resize(remaining_transceiver_count); |
| 8046 | } |
| 8047 | } |
| 8048 | transceiver->internal()->sender_internal()->set_transport(nullptr); |
| 8049 | transceiver->internal()->receiver_internal()->set_transport(nullptr); |
| 8050 | transceiver->internal()->set_direction(state.direction()); |
| 8051 | transceiver->internal()->set_mid(state.mid()); |
| 8052 | transceiver->internal()->set_mline_index(state.mline_index()); |
| 8053 | } |
| 8054 | transceiver_stable_states_by_transceivers_.clear(); |
| 8055 | pending_local_description_.reset(); |
| 8056 | pending_remote_description_.reset(); |
| 8057 | ChangeSignalingState(PeerConnectionInterface::kStable); |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 8058 | |
| 8059 | // The assumption is that in case of implicit rollback UpdateNegotiationNeeded |
| 8060 | // gets called in SetRemoteDescription. |
| 8061 | if (sdp_type == SdpType::kRollback) { |
| 8062 | UpdateNegotiationNeeded(); |
| 8063 | if (is_negotiation_needed_) { |
| 8064 | Observer()->OnRenegotiationNeeded(); |
| 8065 | } |
| 8066 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 8067 | return RTCError::OK(); |
| 8068 | } |
| 8069 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 8070 | } // namespace webrtc |