henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/peer_connection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 14 | #include <limits> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 15 | #include <memory> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 16 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 17 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 18 | #include <utility> |
| 19 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 21 | #include "absl/algorithm/container.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 21:02:22 +0100 | [diff] [blame] | 22 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "api/jsep_ice_candidate.h" |
| 24 | #include "api/jsep_session_description.h" |
| 25 | #include "api/media_stream_proxy.h" |
| 26 | #include "api/media_stream_track_proxy.h" |
Amit Hilbuch | f477040 | 2019-04-08 14:11:57 -0700 | [diff] [blame] | 27 | #include "api/rtc_error.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 28 | #include "api/rtc_event_log/rtc_event_log.h" |
Niels Möller | d8b9ed7 | 2019-05-08 13:53:51 +0200 | [diff] [blame] | 29 | #include "api/rtc_event_log_output_file.h" |
Amit Hilbuch | f477040 | 2019-04-08 14:11:57 -0700 | [diff] [blame] | 30 | #include "api/rtp_parameters.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "api/uma_metrics.h" |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 32 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "call/call.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "logging/rtc_event_log/ice_logger.h" |
Amit Hilbuch | f477040 | 2019-04-08 14:11:57 -0700 | [diff] [blame] | 35 | #include "media/base/rid_description.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "media/sctp/sctp_transport.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 37 | #include "pc/audio_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 38 | #include "pc/audio_track.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 39 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [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 07:51:00 +0100 | [diff] [blame] | 48 | #include "pc/sctp_transport.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [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 10:29:19 +0100 | [diff] [blame] | 52 | #include "pc/video_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 53 | #include "pc/video_track.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [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 09:11:00 -0800 | [diff] [blame] | 57 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 58 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 59 | #include "rtc_base/trace_event.h" |
| 60 | #include "system_wrappers/include/clock.h" |
| 61 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 62 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 64 | using cricket::ContentInfo; |
| 65 | using cricket::ContentInfos; |
| 66 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 67 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 68 | using cricket::RidDescription; |
| 69 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 70 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 71 | using cricket::SimulcastDescription; |
| 72 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 73 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 74 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 75 | using cricket::TransportInfo; |
| 76 | |
| 77 | using cricket::LOCAL_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 78 | using cricket::PRFLX_PORT_TYPE; |
Jeroen de Borst | af242c8 | 2019-04-24 13:13:48 -0700 | [diff] [blame] | 79 | using cricket::RELAY_PORT_TYPE; |
| 80 | using cricket::STUN_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 81 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 82 | namespace webrtc { |
| 83 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 84 | // Error messages |
| 85 | const char kBundleWithoutRtcpMux[] = |
| 86 | "rtcp-mux must be enabled when BUNDLE " |
| 87 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 88 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 89 | const char kInvalidSdp[] = "Invalid session description."; |
| 90 | const char kMlineMismatchInAnswer[] = |
| 91 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 92 | "answer."; |
| 93 | const char kMlineMismatchInSubsequentOffer[] = |
| 94 | "The order of m-lines in subsequent offer doesn't match order from " |
| 95 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 96 | const char kSdpWithoutDtlsFingerprint[] = |
| 97 | "Called with SDP without DTLS fingerprint."; |
| 98 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 99 | const char kSdpWithoutIceUfragPwd[] = |
| 100 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 101 | const char kSessionError[] = "Session error code: "; |
| 102 | const char kSessionErrorDesc[] = "Session error description: "; |
| 103 | const char kDtlsSrtpSetupFailureRtp[] = |
| 104 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 105 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 106 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 108 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 110 | // Field trials. |
| 111 | // Controls datagram transport support. |
| 112 | const char kDatagramTransportFieldTrial[] = "WebRTC-DatagramTransport"; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 113 | // Controls datagram transport data channel support. |
| 114 | const char kDatagramTransportDataChannelFieldTrial[] = |
| 115 | "WebRTC-DatagramTransportDataChannels"; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 116 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 117 | // UMA metric names. |
| 118 | const char kSimulcastVersionApplyLocalDescription[] = |
| 119 | "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription"; |
| 120 | const char kSimulcastVersionApplyRemoteDescription[] = |
| 121 | "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription"; |
| 122 | const char kSimulcastNumberOfEncodings[] = |
| 123 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
| 124 | const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled"; |
| 125 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 126 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 127 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 128 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 129 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 130 | // The length of RTCP CNAMEs. |
| 131 | static const int kRtcpCnameLength = 16; |
| 132 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 133 | enum { |
| 134 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 135 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 136 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 137 | MSG_GETSTATS, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 138 | MSG_REPORT_USAGE_PATTERN, |
| 139 | }; |
| 140 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 141 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 142 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 143 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 144 | explicit SetSessionDescriptionMsg( |
| 145 | webrtc::SetSessionDescriptionObserver* observer) |
| 146 | : observer(observer) {} |
| 147 | |
| 148 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 149 | RTCError error; |
| 150 | }; |
| 151 | |
| 152 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 153 | explicit CreateSessionDescriptionMsg( |
| 154 | webrtc::CreateSessionDescriptionObserver* observer) |
| 155 | : observer(observer) {} |
| 156 | |
| 157 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 158 | RTCError error; |
| 159 | }; |
| 160 | |
| 161 | struct GetStatsMsg : public rtc::MessageData { |
| 162 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 163 | webrtc::MediaStreamTrackInterface* track) |
| 164 | : observer(observer), track(track) {} |
| 165 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 166 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 167 | }; |
| 168 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 169 | // Check if we can send |new_stream| on a PeerConnection. |
| 170 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 171 | webrtc::MediaStreamInterface* new_stream) { |
| 172 | if (!new_stream || !current_streams) { |
| 173 | return false; |
| 174 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 175 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 176 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 177 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 183 | // If the direction is "recvonly" or "inactive", treat the description |
| 184 | // as containing no streams. |
| 185 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 186 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 187 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 188 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 189 | ? desc->streams() |
| 190 | : std::vector<cricket::StreamParams>(); |
| 191 | } |
| 192 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 193 | bool IsValidOfferToReceiveMedia(int value) { |
| 194 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 195 | return (value >= Options::kUndefined) && |
| 196 | (value <= Options::kMaxOfferToReceiveMedia); |
| 197 | } |
| 198 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 199 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 200 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 201 | const std::vector<rtc::scoped_refptr< |
| 202 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 203 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 204 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 205 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 206 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 207 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 208 | if (audio_media_description_options) { |
| 209 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 210 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 211 | } |
| 212 | } else { |
| 213 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 214 | if (video_media_description_options) { |
| 215 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 216 | sender->id(), sender->internal()->stream_ids(), {}, |
| 217 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 220 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 221 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 222 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 223 | // Add options to |session_options| from |rtp_data_channels|. |
| 224 | void AddRtpDataChannelOptions( |
| 225 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 226 | rtp_data_channels, |
| 227 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 228 | if (!data_media_description_options) { |
| 229 | return; |
| 230 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 231 | // Check for data channels. |
| 232 | for (const auto& kv : rtp_data_channels) { |
| 233 | const DataChannel* channel = kv.second; |
| 234 | if (channel->state() == DataChannel::kConnecting || |
| 235 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 236 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 237 | // the data channel's label. |
| 238 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 239 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 244 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 245 | PeerConnectionInterface::IceTransportsType type) { |
| 246 | switch (type) { |
| 247 | case PeerConnectionInterface::kNone: |
| 248 | return cricket::CF_NONE; |
| 249 | case PeerConnectionInterface::kRelay: |
| 250 | return cricket::CF_RELAY; |
| 251 | case PeerConnectionInterface::kNoHost: |
| 252 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 253 | case PeerConnectionInterface::kAll: |
| 254 | return cricket::CF_ALL; |
| 255 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 256 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 257 | } |
| 258 | return cricket::CF_NONE; |
| 259 | } |
| 260 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 261 | std::string GetSignalingStateString( |
| 262 | PeerConnectionInterface::SignalingState state) { |
| 263 | switch (state) { |
| 264 | case PeerConnectionInterface::kStable: |
| 265 | return "kStable"; |
| 266 | case PeerConnectionInterface::kHaveLocalOffer: |
| 267 | return "kHaveLocalOffer"; |
| 268 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 269 | return "kHavePrAnswer"; |
| 270 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 271 | return "kHaveRemoteOffer"; |
| 272 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 273 | return "kHaveRemotePrAnswer"; |
| 274 | case PeerConnectionInterface::kClosed: |
| 275 | return "kClosed"; |
| 276 | } |
| 277 | RTC_NOTREACHED(); |
| 278 | return ""; |
| 279 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 280 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 281 | IceCandidatePairType GetIceCandidatePairCounter( |
| 282 | const cricket::Candidate& local, |
| 283 | const cricket::Candidate& remote) { |
| 284 | const auto& l = local.type(); |
| 285 | const auto& r = remote.type(); |
| 286 | const auto& host = LOCAL_PORT_TYPE; |
| 287 | const auto& srflx = STUN_PORT_TYPE; |
| 288 | const auto& relay = RELAY_PORT_TYPE; |
| 289 | const auto& prflx = PRFLX_PORT_TYPE; |
| 290 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 291 | bool local_hostname = |
| 292 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 293 | bool remote_hostname = !remote.address().hostname().empty() && |
| 294 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 295 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 296 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 297 | if (local_hostname) { |
| 298 | if (remote_hostname) { |
| 299 | return kIceCandidatePairHostNameHostName; |
| 300 | } else if (remote_private) { |
| 301 | return kIceCandidatePairHostNameHostPrivate; |
| 302 | } else { |
| 303 | return kIceCandidatePairHostNameHostPublic; |
| 304 | } |
| 305 | } else if (local_private) { |
| 306 | if (remote_hostname) { |
| 307 | return kIceCandidatePairHostPrivateHostName; |
| 308 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 309 | return kIceCandidatePairHostPrivateHostPrivate; |
| 310 | } else { |
| 311 | return kIceCandidatePairHostPrivateHostPublic; |
| 312 | } |
| 313 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 314 | if (remote_hostname) { |
| 315 | return kIceCandidatePairHostPublicHostName; |
| 316 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 317 | return kIceCandidatePairHostPublicHostPrivate; |
| 318 | } else { |
| 319 | return kIceCandidatePairHostPublicHostPublic; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | if (l == host && r == srflx) |
| 324 | return kIceCandidatePairHostSrflx; |
| 325 | if (l == host && r == relay) |
| 326 | return kIceCandidatePairHostRelay; |
| 327 | if (l == host && r == prflx) |
| 328 | return kIceCandidatePairHostPrflx; |
| 329 | if (l == srflx && r == host) |
| 330 | return kIceCandidatePairSrflxHost; |
| 331 | if (l == srflx && r == srflx) |
| 332 | return kIceCandidatePairSrflxSrflx; |
| 333 | if (l == srflx && r == relay) |
| 334 | return kIceCandidatePairSrflxRelay; |
| 335 | if (l == srflx && r == prflx) |
| 336 | return kIceCandidatePairSrflxPrflx; |
| 337 | if (l == relay && r == host) |
| 338 | return kIceCandidatePairRelayHost; |
| 339 | if (l == relay && r == srflx) |
| 340 | return kIceCandidatePairRelaySrflx; |
| 341 | if (l == relay && r == relay) |
| 342 | return kIceCandidatePairRelayRelay; |
| 343 | if (l == relay && r == prflx) |
| 344 | return kIceCandidatePairRelayPrflx; |
| 345 | if (l == prflx && r == host) |
| 346 | return kIceCandidatePairPrflxHost; |
| 347 | if (l == prflx && r == srflx) |
| 348 | return kIceCandidatePairPrflxSrflx; |
| 349 | if (l == prflx && r == relay) |
| 350 | return kIceCandidatePairPrflxRelay; |
| 351 | return kIceCandidatePairMax; |
| 352 | } |
| 353 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 354 | // Logic to decide if an m= section can be recycled. This means that the new |
| 355 | // m= section is not rejected, but the old local or remote m= section is |
| 356 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 357 | // of the old remote and old local descriptions in no particular order. |
| 358 | // We need to check both the old local and remote because either |
| 359 | // could be the most current from the latest negotation. |
| 360 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 361 | const ContentInfo& content, |
| 362 | const ContentInfo* old_content_one, |
| 363 | const ContentInfo* old_content_two) { |
| 364 | return type == SdpType::kOffer && !content.rejected && |
| 365 | ((old_content_one && old_content_one->rejected) || |
| 366 | (old_content_two && old_content_two->rejected)); |
| 367 | } |
| 368 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 369 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 370 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 371 | // less than |current_desc|. In the case of checking an answer's |
| 372 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 373 | // local or remote. In the case of checking an offer's |new_desc| we |
| 374 | // check against the local and remote descriptions stored from the last |
| 375 | // negotiation, because either of these could be the most up to date for |
| 376 | // possible rejected m sections. These are the |current_desc| and |
| 377 | // |secondary_current_desc|. |
| 378 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 379 | const SessionDescription* secondary_current_desc, |
| 380 | const SessionDescription& new_desc, |
| 381 | const SdpType type) { |
| 382 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 386 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 387 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 388 | if (secondary_current_desc && |
| 389 | i < secondary_current_desc->contents().size()) { |
| 390 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 391 | } |
| 392 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 393 | ¤t_desc.contents()[i], |
| 394 | secondary_content_info)) { |
| 395 | // For new offer descriptions, if the media section can be recycled, it's |
| 396 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 397 | continue; |
| 398 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 399 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 400 | return false; |
| 401 | } |
| 402 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 403 | new_desc.contents()[i].media_description(); |
| 404 | const MediaContentDescription* current_desc_mdesc = |
| 405 | current_desc.contents()[i].media_description(); |
| 406 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | return true; |
| 411 | } |
| 412 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 413 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 414 | const SessionDescription& desc2) { |
| 415 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 418 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 419 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 420 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 421 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 422 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 423 | static constexpr struct { |
| 424 | KeyExchangeProtocolType protocol_type; |
| 425 | cricket::MediaType media_type; |
| 426 | KeyExchangeProtocolMedia protocol_media; |
| 427 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 428 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 429 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 430 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 431 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 432 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 433 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 434 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 435 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 436 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 437 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 438 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 439 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 440 | }; |
| 441 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 442 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 443 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 444 | |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 445 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 446 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 447 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 448 | i.protocol_media, |
| 449 | kEnumCounterKeyProtocolMediaTypeMax); |
| 450 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 454 | void NoteAddIceCandidateResult(int result) { |
| 455 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 456 | kAddIceCandidateMax); |
| 457 | } |
| 458 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 459 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 460 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 461 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 462 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 463 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 464 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 465 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 466 | const cricket::ContentGroup* bundle = |
| 467 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 468 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 469 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 470 | continue; |
| 471 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 472 | // Note what media is used with each crypto protocol, for all sections. |
| 473 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 474 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 475 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 476 | const std::string& mid = content_info.name; |
| 477 | if (bundle && bundle->HasContentName(mid) && |
| 478 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 479 | // This isn't the first media section in the BUNDLE group, so it's not |
| 480 | // required to have crypto attributes, since only the crypto attributes |
| 481 | // from the first section actually get used. |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | // If the content isn't rejected or bundled into another m= section, crypto |
| 486 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 487 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 488 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 489 | if (!media || !tinfo) { |
| 490 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 491 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 492 | } |
| 493 | if (dtls_enabled) { |
| 494 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 495 | RTC_LOG(LS_WARNING) |
| 496 | << "Session description must have DTLS fingerprint if " |
| 497 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 498 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 499 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 500 | } |
| 501 | } else { |
| 502 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 503 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 504 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 505 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 509 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 513 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 514 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 515 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 516 | const cricket::ContentGroup* bundle = |
| 517 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 518 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 519 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 520 | continue; |
| 521 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 522 | const std::string& mid = content_info.name; |
| 523 | if (bundle && bundle->HasContentName(mid) && |
| 524 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 525 | // This isn't the first media section in the BUNDLE group, so it's not |
| 526 | // required to have ufrag/password, since only the ufrag/password from |
| 527 | // the first section actually get used. |
| 528 | continue; |
| 529 | } |
| 530 | |
| 531 | // If the content isn't rejected or bundled into another m= section, |
| 532 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 533 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 534 | if (!tinfo) { |
| 535 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 536 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 537 | return false; |
| 538 | } |
| 539 | if (tinfo->description.ice_ufrag.empty() || |
| 540 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 541 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | return true; |
| 546 | } |
| 547 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 548 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 549 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 550 | const SessionDescriptionInterface* new_desc, |
| 551 | const std::string& content_name) { |
| 552 | if (!old_desc) { |
| 553 | return false; |
| 554 | } |
| 555 | const SessionDescription* new_sd = new_desc->description(); |
| 556 | const SessionDescription* old_sd = old_desc->description(); |
| 557 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 558 | if (!cinfo || cinfo->rejected) { |
| 559 | return false; |
| 560 | } |
| 561 | // If the content isn't rejected, check if ufrag and password has changed. |
| 562 | const cricket::TransportDescription* new_transport_desc = |
| 563 | new_sd->GetTransportDescriptionByName(content_name); |
| 564 | const cricket::TransportDescription* old_transport_desc = |
| 565 | old_sd->GetTransportDescriptionByName(content_name); |
| 566 | if (!new_transport_desc || !old_transport_desc) { |
| 567 | // No transport description exists. This is not an ICE restart. |
| 568 | return false; |
| 569 | } |
| 570 | if (cricket::IceCredentialsChanged( |
| 571 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 572 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 573 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 574 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 575 | return true; |
| 576 | } |
| 577 | return false; |
| 578 | } |
| 579 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 580 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 581 | // from an RTCError. |
| 582 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 583 | SdpType type, |
| 584 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 585 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 586 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 587 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 588 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 591 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 592 | std::string output = "streams=["; |
| 593 | const char* separator = ""; |
| 594 | for (const auto& stream_id : stream_ids) { |
| 595 | output.append(separator).append(stream_id); |
| 596 | separator = ", "; |
| 597 | } |
| 598 | output.append("]"); |
| 599 | return output; |
| 600 | } |
| 601 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 602 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 603 | int rtc_configuration_parameter) { |
| 604 | if (rtc_configuration_parameter == |
| 605 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 606 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 607 | } |
| 608 | return rtc_configuration_parameter; |
| 609 | } |
| 610 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 611 | void ReportSimulcastApiVersion(const char* name, |
| 612 | const SessionDescription& session) { |
| 613 | bool has_legacy = false; |
| 614 | bool has_spec_compliant = false; |
| 615 | for (const ContentInfo& content : session.contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 616 | if (!content.media_description()) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 617 | continue; |
| 618 | } |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 619 | has_spec_compliant |= content.media_description()->HasSimulcast(); |
| 620 | for (const StreamParams& sp : content.media_description()->streams()) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 621 | has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (has_legacy) { |
| 626 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy, |
| 627 | kSimulcastApiVersionMax); |
| 628 | } |
| 629 | if (has_spec_compliant) { |
| 630 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant, |
| 631 | kSimulcastApiVersionMax); |
| 632 | } |
| 633 | if (!has_legacy && !has_spec_compliant) { |
| 634 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone, |
| 635 | kSimulcastApiVersionMax); |
| 636 | } |
| 637 | } |
| 638 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 639 | const ContentInfo* FindTransceiverMSection( |
| 640 | RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver, |
| 641 | const SessionDescriptionInterface* session_description) { |
| 642 | return transceiver->mid() |
| 643 | ? session_description->description()->GetContentByName( |
| 644 | *transceiver->mid()) |
| 645 | : nullptr; |
| 646 | } |
| 647 | |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 648 | // Wraps a CreateSessionDescriptionObserver and an OperationsChain operation |
| 649 | // complete callback. When the observer is invoked, the wrapped observer is |
| 650 | // invoked followed by invoking the completion callback. |
| 651 | class CreateSessionDescriptionObserverOperationWrapper |
| 652 | : public CreateSessionDescriptionObserver { |
| 653 | public: |
| 654 | CreateSessionDescriptionObserverOperationWrapper( |
| 655 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer, |
| 656 | std::function<void()> operation_complete_callback) |
| 657 | : observer_(std::move(observer)), |
| 658 | operation_complete_callback_(std::move(operation_complete_callback)) { |
| 659 | RTC_DCHECK(observer_); |
| 660 | } |
| 661 | ~CreateSessionDescriptionObserverOperationWrapper() override { |
| 662 | RTC_DCHECK(was_called_); |
| 663 | } |
| 664 | |
| 665 | void OnSuccess(SessionDescriptionInterface* desc) override { |
| 666 | RTC_DCHECK(!was_called_); |
| 667 | #ifdef RTC_DCHECK_IS_ON |
| 668 | was_called_ = true; |
| 669 | #endif // RTC_DCHECK_IS_ON |
| 670 | // Completing the operation before invoking the observer allows the observer |
| 671 | // to execute SetLocalDescription() without delay. |
| 672 | operation_complete_callback_(); |
| 673 | observer_->OnSuccess(desc); |
| 674 | } |
| 675 | |
| 676 | void OnFailure(RTCError error) override { |
| 677 | RTC_DCHECK(!was_called_); |
| 678 | #ifdef RTC_DCHECK_IS_ON |
| 679 | was_called_ = true; |
| 680 | #endif // RTC_DCHECK_IS_ON |
| 681 | operation_complete_callback_(); |
| 682 | observer_->OnFailure(std::move(error)); |
| 683 | } |
| 684 | |
| 685 | private: |
| 686 | #ifdef RTC_DCHECK_IS_ON |
| 687 | bool was_called_ = false; |
| 688 | #endif // RTC_DCHECK_IS_ON |
| 689 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_; |
| 690 | std::function<void()> operation_complete_callback_; |
| 691 | }; |
| 692 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 693 | } // namespace |
| 694 | |
Henrik Boström | 4e19670 | 2019-10-30 10:35:50 +0100 | [diff] [blame] | 695 | // Used by parameterless SetLocalDescription() to create an offer or answer. |
| 696 | // Upon completion of creating the session description, SetLocalDescription() is |
| 697 | // invoked with the result. |
| 698 | // For consistency with DoSetLocalDescription(), if the PeerConnection is |
| 699 | // destroyed midst operation, we DO NOT inform the |
| 700 | // |set_local_description_observer| that the operation failed. |
| 701 | // TODO(hbos): If/when we process SLD messages in ~PeerConnection, the |
| 702 | // consistent thing would be to inform the observer here. |
| 703 | class PeerConnection::ImplicitCreateSessionDescriptionObserver |
| 704 | : public CreateSessionDescriptionObserver { |
| 705 | public: |
| 706 | ImplicitCreateSessionDescriptionObserver( |
| 707 | rtc::WeakPtr<PeerConnection> pc, |
| 708 | rtc::scoped_refptr<SetSessionDescriptionObserver> |
| 709 | set_local_description_observer) |
| 710 | : pc_(std::move(pc)), |
| 711 | set_local_description_observer_( |
| 712 | std::move(set_local_description_observer)) {} |
| 713 | ~ImplicitCreateSessionDescriptionObserver() override { |
| 714 | RTC_DCHECK(was_called_); |
| 715 | } |
| 716 | |
| 717 | void SetOperationCompleteCallback( |
| 718 | std::function<void()> operation_complete_callback) { |
| 719 | operation_complete_callback_ = std::move(operation_complete_callback); |
| 720 | } |
| 721 | |
| 722 | bool was_called() const { return was_called_; } |
| 723 | |
| 724 | void OnSuccess(SessionDescriptionInterface* desc_ptr) override { |
| 725 | RTC_DCHECK(!was_called_); |
| 726 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 727 | was_called_ = true; |
| 728 | |
| 729 | // Abort early if |pc_| is no longer valid. |
| 730 | if (!pc_) { |
| 731 | operation_complete_callback_(); |
| 732 | return; |
| 733 | } |
| 734 | // DoSetLocalDescription() is currently implemented as a synchronous |
| 735 | // operation but where the |set_local_description_observer_|'s callbacks are |
| 736 | // invoked asynchronously in a post to PeerConnection::OnMessage(). |
| 737 | pc_->DoSetLocalDescription(std::move(desc), |
| 738 | std::move(set_local_description_observer_)); |
| 739 | // For backwards-compatability reasons, we declare the operation as |
| 740 | // completed here (rather than in PeerConnection::OnMessage()). This ensures |
| 741 | // that subsequent offer/answer operations can start immediately (without |
| 742 | // waiting for OnMessage()). |
| 743 | operation_complete_callback_(); |
| 744 | } |
| 745 | |
| 746 | void OnFailure(RTCError error) override { |
| 747 | RTC_DCHECK(!was_called_); |
| 748 | was_called_ = true; |
| 749 | |
| 750 | // Abort early if |pc_| is no longer valid. |
| 751 | if (!pc_) { |
| 752 | operation_complete_callback_(); |
| 753 | return; |
| 754 | } |
| 755 | // DoSetLocalDescription() reports its failures in a post. We do the |
| 756 | // same thing here for consistency. |
| 757 | pc_->PostSetSessionDescriptionFailure( |
| 758 | set_local_description_observer_, |
| 759 | RTCError(error.type(), |
| 760 | std::string("SetLocalDescription failed to create " |
| 761 | "session description - ") + |
| 762 | error.message())); |
| 763 | operation_complete_callback_(); |
| 764 | } |
| 765 | |
| 766 | private: |
| 767 | bool was_called_ = false; |
| 768 | rtc::WeakPtr<PeerConnection> pc_; |
| 769 | rtc::scoped_refptr<SetSessionDescriptionObserver> |
| 770 | set_local_description_observer_; |
| 771 | std::function<void()> operation_complete_callback_; |
| 772 | }; |
| 773 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 774 | class PeerConnection::LocalIceCredentialsToReplace { |
| 775 | public: |
| 776 | // Sets the ICE credentials that need restarting to the ICE credentials of |
| 777 | // the current and pending descriptions. |
| 778 | void SetIceCredentialsFromLocalDescriptions( |
| 779 | const SessionDescriptionInterface* current_local_description, |
| 780 | const SessionDescriptionInterface* pending_local_description) { |
| 781 | ice_credentials_.clear(); |
| 782 | if (current_local_description) { |
| 783 | AppendIceCredentialsFromSessionDescription(*current_local_description); |
| 784 | } |
| 785 | if (pending_local_description) { |
| 786 | AppendIceCredentialsFromSessionDescription(*pending_local_description); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | void ClearIceCredentials() { ice_credentials_.clear(); } |
| 791 | |
| 792 | // Returns true if we have ICE credentials that need restarting. |
| 793 | bool HasIceCredentials() const { return !ice_credentials_.empty(); } |
| 794 | |
| 795 | // Returns true if |local_description| shares no ICE credentials with the |
| 796 | // ICE credentials that need restarting. |
| 797 | bool SatisfiesIceRestart( |
| 798 | const SessionDescriptionInterface& local_description) const { |
| 799 | for (const auto& transport_info : |
| 800 | local_description.description()->transport_infos()) { |
| 801 | if (ice_credentials_.find(std::make_pair( |
| 802 | transport_info.description.ice_ufrag, |
| 803 | transport_info.description.ice_pwd)) != ice_credentials_.end()) { |
| 804 | return false; |
| 805 | } |
| 806 | } |
| 807 | return true; |
| 808 | } |
| 809 | |
| 810 | private: |
| 811 | void AppendIceCredentialsFromSessionDescription( |
| 812 | const SessionDescriptionInterface& desc) { |
| 813 | for (const auto& transport_info : desc.description()->transport_infos()) { |
| 814 | ice_credentials_.insert( |
| 815 | std::make_pair(transport_info.description.ice_ufrag, |
| 816 | transport_info.description.ice_pwd)); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | std::set<std::pair<std::string, std::string>> ice_credentials_; |
| 821 | }; |
| 822 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 823 | // Upon completion, posts a task to execute the callback of the |
| 824 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 825 | // point, the state of the peer connection might no longer reflect the effects |
| 826 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 827 | // modified during the post. |
| 828 | // TODO(hbos): Remove this class once we remove the version of |
| 829 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 830 | // SetSessionDescriptionObserver as an argument. |
| 831 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 832 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 833 | public: |
| 834 | SetRemoteDescriptionObserverAdapter( |
| 835 | rtc::scoped_refptr<PeerConnection> pc, |
| 836 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 837 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 838 | |
| 839 | // SetRemoteDescriptionObserverInterface implementation. |
| 840 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 841 | if (error.ok()) |
| 842 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 843 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 844 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | private: |
| 848 | rtc::scoped_refptr<PeerConnection> pc_; |
| 849 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 850 | }; |
| 851 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 852 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 853 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 854 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 855 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 856 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 857 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 858 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 859 | BundlePolicy bundle_policy; |
| 860 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 861 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 862 | int ice_candidate_pool_size; |
| 863 | bool disable_ipv6; |
| 864 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 865 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 866 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 867 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 868 | absl::optional<int> screencast_min_bitrate; |
| 869 | absl::optional<bool> combined_audio_video_bwe; |
| 870 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 871 | TcpCandidatePolicy tcp_candidate_policy; |
| 872 | CandidateNetworkPolicy candidate_network_policy; |
| 873 | int audio_jitter_buffer_max_packets; |
| 874 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 875 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 876 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 877 | int ice_connection_receiving_timeout; |
| 878 | int ice_backup_candidate_pair_ping_interval; |
| 879 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 880 | bool prioritize_most_likely_ice_candidate_pairs; |
| 881 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 882 | bool prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 883 | PortPrunePolicy turn_port_prune_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 884 | bool presume_writable_when_fully_relayed; |
| 885 | bool enable_ice_renomination; |
| 886 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 887 | bool surface_ice_candidates_on_ice_transport_type_changed; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 888 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 889 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 890 | absl::optional<int> ice_check_min_interval; |
| 891 | absl::optional<int> ice_unwritable_timeout; |
| 892 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 893 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 894 | absl::optional<int> stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 895 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 896 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 897 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 898 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 899 | bool use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 900 | bool use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 901 | absl::optional<bool> use_datagram_transport; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 902 | absl::optional<bool> use_datagram_transport_for_data_channels; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 903 | absl::optional<bool> use_datagram_transport_for_data_channels_receive_only; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 904 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 905 | bool offer_extmap_allow_mixed; |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 906 | std::string turn_logging_id; |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 907 | bool enable_implicit_rollback; |
philipel | 16cec3b | 2019-10-25 12:23:02 +0200 | [diff] [blame] | 908 | absl::optional<bool> allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 909 | }; |
| 910 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 911 | "Did you add something to RTCConfiguration and forget to " |
| 912 | "update operator==?"); |
| 913 | return type == o.type && servers == o.servers && |
| 914 | bundle_policy == o.bundle_policy && |
| 915 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 916 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 917 | candidate_network_policy == o.candidate_network_policy && |
| 918 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 919 | audio_jitter_buffer_fast_accelerate == |
| 920 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 921 | audio_jitter_buffer_min_delay_ms == |
| 922 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 923 | audio_jitter_buffer_enable_rtx_handling == |
| 924 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 925 | ice_connection_receiving_timeout == |
| 926 | o.ice_connection_receiving_timeout && |
| 927 | ice_backup_candidate_pair_ping_interval == |
| 928 | o.ice_backup_candidate_pair_ping_interval && |
| 929 | continual_gathering_policy == o.continual_gathering_policy && |
| 930 | certificates == o.certificates && |
| 931 | prioritize_most_likely_ice_candidate_pairs == |
| 932 | o.prioritize_most_likely_ice_candidate_pairs && |
| 933 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 934 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 935 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 936 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 937 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 938 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 939 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 940 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 941 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 942 | prune_turn_ports == o.prune_turn_ports && |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 943 | turn_port_prune_policy == o.turn_port_prune_policy && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 944 | presume_writable_when_fully_relayed == |
| 945 | o.presume_writable_when_fully_relayed && |
| 946 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 947 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 948 | surface_ice_candidates_on_ice_transport_type_changed == |
| 949 | o.surface_ice_candidates_on_ice_transport_type_changed && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 950 | ice_check_interval_strong_connectivity == |
| 951 | o.ice_check_interval_strong_connectivity && |
| 952 | ice_check_interval_weak_connectivity == |
| 953 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 954 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 955 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 956 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 957 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 958 | stun_candidate_keepalive_interval == |
| 959 | o.stun_candidate_keepalive_interval && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 960 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 961 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 962 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 963 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 964 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 965 | use_media_transport_for_data_channels == |
| 966 | o.use_media_transport_for_data_channels && |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 967 | use_datagram_transport == o.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 968 | use_datagram_transport_for_data_channels == |
| 969 | o.use_datagram_transport_for_data_channels && |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 970 | use_datagram_transport_for_data_channels_receive_only == |
| 971 | o.use_datagram_transport_for_data_channels_receive_only && |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 972 | crypto_options == o.crypto_options && |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 973 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed && |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 974 | turn_logging_id == o.turn_logging_id && |
philipel | 16cec3b | 2019-10-25 12:23:02 +0200 | [diff] [blame] | 975 | enable_implicit_rollback == o.enable_implicit_rollback && |
Henrik Boström | a0ff50c | 2020-05-05 15:54:46 +0200 | [diff] [blame] | 976 | allow_codec_switching == o.allow_codec_switching && |
| 977 | enable_simulcast_stats == o.enable_simulcast_stats; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 981 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 982 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 983 | } |
| 984 | |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 985 | void PeerConnection::TransceiverStableState::set_newly_created() { |
| 986 | RTC_DCHECK(!has_m_section_); |
| 987 | newly_created_ = true; |
| 988 | } |
| 989 | |
| 990 | void PeerConnection::TransceiverStableState::SetMSectionIfUnset( |
| 991 | absl::optional<std::string> mid, |
| 992 | absl::optional<size_t> mline_index) { |
| 993 | if (!has_m_section_) { |
| 994 | mid_ = mid; |
| 995 | mline_index_ = mline_index; |
| 996 | has_m_section_ = true; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | void PeerConnection::TransceiverStableState::SetRemoteStreamIdsIfUnset( |
| 1001 | const std::vector<std::string>& ids) { |
| 1002 | if (!remote_stream_ids_.has_value()) { |
| 1003 | remote_stream_ids_ = ids; |
| 1004 | } |
| 1005 | } |
| 1006 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 1007 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 1008 | std::string GenerateRtcpCname() { |
| 1009 | std::string cname; |
| 1010 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1011 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 1012 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 1013 | } |
| 1014 | return cname; |
| 1015 | } |
| 1016 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1017 | bool ValidateOfferAnswerOptions( |
| 1018 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 1019 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 1020 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1023 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 1024 | // m= sections (in other words, nothing that involves a map/array). |
| 1025 | void ExtractSharedMediaSessionOptions( |
| 1026 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 1027 | cricket::MediaSessionOptions* session_options) { |
| 1028 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 1029 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
Mirta Dvornicic | 479a3c0 | 2019-06-04 15:38:50 +0200 | [diff] [blame] | 1030 | session_options->raw_packetization_for_video = |
| 1031 | rtc_options.raw_packetization_for_video; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1032 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 1033 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 1034 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 1035 | std::unique_ptr<RtcEventLog> event_log, |
| 1036 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1037 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 1038 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 1039 | event_log_ptr_(event_log_.get()), |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 1040 | operations_chain_(rtc::OperationsChain::Create()), |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1041 | datagram_transport_config_( |
| 1042 | field_trial::FindFullName(kDatagramTransportFieldTrial)), |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1043 | datagram_transport_data_channel_config_( |
| 1044 | field_trial::FindFullName(kDatagramTransportDataChannelFieldTrial)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 1045 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1046 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 1047 | remote_streams_(StreamCollection::Create()), |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1048 | call_(std::move(call)), |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 1049 | call_ptr_(call_.get()), |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 1050 | local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()), |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 1051 | data_channel_controller_(this), |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 1052 | weak_ptr_factory_(this) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1053 | |
| 1054 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1055 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1056 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1057 | |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 1058 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 1059 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 1060 | // Need to stop transceivers before destroying the stats collector because |
| 1061 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 1062 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1063 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 1064 | transceiver->Stop(); |
| 1065 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1066 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1067 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 1068 | if (stats_collector_) { |
| 1069 | stats_collector_->WaitForPendingRequest(); |
| 1070 | stats_collector_ = nullptr; |
| 1071 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1072 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 1073 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 1074 | // the last stats request can still read from the channels. |
| 1075 | DestroyAllChannels(); |
| 1076 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1077 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1078 | |
| 1079 | webrtc_session_desc_factory_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1080 | sctp_factory_.reset(); |
| 1081 | transport_controller_.reset(); |
| 1082 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 1083 | // port_allocator_ lives on the network thread and should be destroyed there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1084 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 1085 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1086 | port_allocator_.reset(); |
| 1087 | }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 1088 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 1089 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 1090 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 1091 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1092 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 1093 | event_log_.reset(); |
| 1094 | }); |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 1095 | |
| 1096 | // Process all pending notifications in the message queue. If we don't do |
| 1097 | // this, requests will linger and not know they succeeded or failed. |
| 1098 | rtc::MessageList list; |
| 1099 | signaling_thread()->Clear(this, rtc::MQID_ANY, &list); |
| 1100 | for (auto& msg : list) { |
| 1101 | if (msg.message_id == MSG_CREATE_SESSIONDESCRIPTION_FAILED) { |
| 1102 | // Processing CreateOffer() and CreateAnswer() messages ensures their |
| 1103 | // observers are invoked even if the PeerConnection is destroyed early. |
| 1104 | OnMessage(&msg); |
| 1105 | } else { |
| 1106 | // TODO(hbos): Consider processing all pending messages. This would mean |
| 1107 | // that SetLocalDescription() and SetRemoteDescription() observers are |
| 1108 | // informed of successes and failures; this is currently NOT the case. |
| 1109 | delete msg.pdata; |
| 1110 | } |
| 1111 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 1114 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 1115 | // Destroy video channels first since they may have a pointer to a voice |
| 1116 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1117 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1118 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 1119 | DestroyTransceiverChannel(transceiver); |
| 1120 | } |
| 1121 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1122 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1123 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 1124 | DestroyTransceiverChannel(transceiver); |
| 1125 | } |
| 1126 | } |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 1127 | DestroyDataChannelTransport(); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1130 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1131 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1132 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1133 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1134 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1135 | |
| 1136 | RTCError config_error = ValidateConfiguration(configuration); |
| 1137 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1138 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1139 | return false; |
| 1140 | } |
| 1141 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1142 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1143 | RTC_LOG(LS_ERROR) |
| 1144 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 1145 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1146 | return false; |
| 1147 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 1148 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1149 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1150 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1151 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 1152 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 1153 | return false; |
| 1154 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1155 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1156 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 1157 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1158 | port_allocator_ = std::move(dependencies.allocator); |
Jorge E. Moreira | 00b46f7 | 2020-03-27 18:01:19 -0700 | [diff] [blame] | 1159 | packet_socket_factory_ = std::move(dependencies.packet_socket_factory); |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 1160 | ice_transport_factory_ = std::move(dependencies.ice_transport_factory); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1161 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 1162 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 1163 | cricket::ServerAddresses stun_servers; |
| 1164 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1165 | |
| 1166 | RTCErrorType parse_error = |
| 1167 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 1168 | if (parse_error != RTCErrorType::NONE) { |
| 1169 | return false; |
| 1170 | } |
| 1171 | |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 1172 | // Add the turn logging id to all turn servers |
| 1173 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 1174 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 1175 | } |
| 1176 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 1177 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1178 | // there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1179 | const auto pa_result = |
| 1180 | network_thread()->Invoke<InitializePortAllocatorResult>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 1181 | RTC_FROM_HERE, |
| 1182 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1183 | stun_servers, turn_servers, configuration)); |
| 1184 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 1185 | // If initialization was successful, note if STUN or TURN servers |
| 1186 | // were supplied. |
| 1187 | if (!stun_servers.empty()) { |
| 1188 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1189 | } |
| 1190 | if (!turn_servers.empty()) { |
| 1191 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1192 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1193 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1194 | // Send information about IPv4/IPv6 status. |
| 1195 | PeerConnectionAddressFamilyCounter address_family; |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1196 | if (pa_result.enable_ipv6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1197 | address_family = kPeerConnection_IPv6; |
| 1198 | } else { |
| 1199 | address_family = kPeerConnection_IPv4; |
| 1200 | } |
| 1201 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 1202 | kPeerConnectionAddressFamilyCounter_Max); |
| 1203 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1204 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 1205 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1206 | // RFC 3264: The numeric value of the session id and version in the |
| 1207 | // o line MUST be representable with a "64 bit signed integer". |
| 1208 | // Due to this constraint session id |session_id_| is max limited to |
| 1209 | // LLONG_MAX. |
| 1210 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1211 | JsepTransportController::Config config; |
| 1212 | config.redetermine_role_on_ice_restart = |
| 1213 | configuration.redetermine_role_on_ice_restart; |
| 1214 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 1215 | config.disable_encryption = options.disable_encryption; |
| 1216 | config.bundle_policy = configuration.bundle_policy; |
| 1217 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1218 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 1219 | // stub. |
| 1220 | config.crypto_options = configuration.crypto_options.has_value() |
| 1221 | ? *configuration.crypto_options |
| 1222 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1223 | config.transport_observer = this; |
Sebastian Jansson | 1b83a9e | 2019-09-18 18:22:12 +0200 | [diff] [blame] | 1224 | // It's safe to pass |this| and using |rtcp_invoker_| and the |call_| pointer |
| 1225 | // since the JsepTransportController instance is owned by this PeerConnection |
| 1226 | // instance and is destroyed before both |rtcp_invoker_| and the |call_| |
| 1227 | // pointer. |
| 1228 | config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet, |
| 1229 | int64_t packet_time_us) { |
| 1230 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1231 | rtcp_invoker_.AsyncInvoke<void>( |
| 1232 | RTC_FROM_HERE, worker_thread(), [this, packet, packet_time_us] { |
| 1233 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1234 | // |call_| is reset on the worker thread in the PeerConnection |
| 1235 | // destructor, so we check that it's still valid before propagating |
| 1236 | // the packet. |
| 1237 | if (call_) { |
| 1238 | call_->Receiver()->DeliverPacket(MediaType::ANY, packet, |
| 1239 | packet_time_us); |
| 1240 | } |
| 1241 | }); |
| 1242 | }; |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 1243 | config.event_log = event_log_ptr_; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1244 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 1245 | config.enable_external_auth = true; |
| 1246 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 1247 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1248 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1249 | use_datagram_transport_ = datagram_transport_config_.enabled && |
Bjorn A Mellem | 238aab9 | 2019-07-02 11:06:00 -0700 | [diff] [blame] | 1250 | configuration.use_datagram_transport.value_or( |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1251 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1252 | use_datagram_transport_for_data_channels_ = |
| 1253 | datagram_transport_data_channel_config_.enabled && |
| 1254 | configuration.use_datagram_transport_for_data_channels.value_or( |
| 1255 | datagram_transport_data_channel_config_.default_value); |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 1256 | use_datagram_transport_for_data_channels_receive_only_ = |
| 1257 | configuration.use_datagram_transport_for_data_channels_receive_only |
| 1258 | .value_or(datagram_transport_data_channel_config_.receive_only); |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 1259 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1260 | if (!factory_->media_transport_factory()) { |
| 1261 | RTC_DCHECK(false) |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 1262 | << "PeerConnecton is initialized with use_datagram_transport = true " |
| 1263 | "or use_datagram_transport_for_data_channels = true " |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 1264 | "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1265 | return false; |
| 1266 | } |
| 1267 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1268 | config.use_datagram_transport = use_datagram_transport_; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1269 | config.use_datagram_transport_for_data_channels = |
| 1270 | use_datagram_transport_for_data_channels_; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 1271 | config.use_datagram_transport_for_data_channels_receive_only = |
| 1272 | use_datagram_transport_for_data_channels_receive_only_; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1273 | config.media_transport_factory = factory_->media_transport_factory(); |
| 1274 | } |
| 1275 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1276 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1277 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1278 | if (!configuration.certificates.empty()) { |
| 1279 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1280 | // just picking the first one. The decision should be made based on the DTLS |
| 1281 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1282 | certificate = configuration.certificates[0]; |
| 1283 | } |
| 1284 | |
| 1285 | if (options.disable_encryption) { |
| 1286 | dtls_enabled_ = false; |
| 1287 | } else { |
| 1288 | // Enable DTLS by default if we have an identity store or a certificate. |
| 1289 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
| 1290 | // |configuration| can override the default |dtls_enabled_| value. |
| 1291 | if (configuration.enable_dtls_srtp) { |
| 1292 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
| 1297 | |
| 1298 | if (use_datagram_transport_for_data_channels_) { |
| 1299 | if (configuration.enable_rtp_data_channel) { |
| 1300 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1301 | "use_datagram_transport_for_data_channels are " |
| 1302 | "incompatible and cannot both be set to true"; |
| 1303 | return false; |
| 1304 | } |
| 1305 | if (configuration.enable_dtls_srtp && !*configuration.enable_dtls_srtp) { |
| 1306 | RTC_LOG(LS_INFO) << "Using data channel transport with no fallback"; |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 1307 | data_channel_controller_.set_data_channel_type( |
| 1308 | cricket::DCT_DATA_CHANNEL_TRANSPORT); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1309 | } else { |
| 1310 | RTC_LOG(LS_INFO) << "Using data channel transport with fallback to SCTP"; |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 1311 | data_channel_controller_.set_data_channel_type( |
| 1312 | cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1313 | config.sctp_factory = sctp_factory_.get(); |
| 1314 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1315 | } else if (configuration.enable_rtp_data_channel) { |
| 1316 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1317 | // set. It takes precendence over the disable_sctp_data_channels |
| 1318 | // PeerConnectionFactoryInterface::Options. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 1319 | data_channel_controller_.set_data_channel_type(cricket::DCT_RTP); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1320 | } else { |
| 1321 | // DTLS has to be enabled to use SCTP. |
| 1322 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 1323 | data_channel_controller_.set_data_channel_type(cricket::DCT_SCTP); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1324 | config.sctp_factory = sctp_factory_.get(); |
| 1325 | } |
| 1326 | } |
| 1327 | |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 1328 | config.ice_transport_factory = ice_transport_factory_.get(); |
| 1329 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1330 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 1331 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1332 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1333 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1334 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1335 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1336 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1337 | transport_controller_->SignalConnectionState.connect( |
| 1338 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1339 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1340 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1341 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1342 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 1343 | transport_controller_->SignalIceCandidateError.connect( |
| 1344 | this, &PeerConnection::OnTransportControllerCandidateError); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1345 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1346 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1347 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1348 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 1349 | transport_controller_->SignalIceCandidatePairChanged.connect( |
| 1350 | this, &PeerConnection::OnTransportControllerCandidateChanged); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1351 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1352 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1353 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1354 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1355 | configuration_ = configuration; |
| 1356 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1357 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1358 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1359 | video_options_.screencast_min_bitrate_kbps = |
| 1360 | configuration.screencast_min_bitrate; |
| 1361 | audio_options_.combined_audio_video_bwe = |
| 1362 | configuration.combined_audio_video_bwe; |
| 1363 | |
| 1364 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1365 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1366 | |
| 1367 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1368 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1369 | |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 1370 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1371 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1372 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 1373 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1374 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1375 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1376 | // Whether the certificate generator/certificate is null or not determines |
| 1377 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1378 | // the right instructions by clearing the variables if needed. |
| 1379 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1380 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1381 | certificate = nullptr; |
| 1382 | } else if (certificate) { |
| 1383 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1384 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1388 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1389 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1390 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1391 | this, &PeerConnection::OnCertificateReady); |
| 1392 | |
| 1393 | if (options.disable_encryption) { |
| 1394 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1395 | } |
| 1396 | |
| 1397 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1398 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1399 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1400 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1401 | // Add default audio/video transceivers for Plan B SDP. |
| 1402 | if (!IsUnifiedPlan()) { |
| 1403 | transceivers_.push_back( |
| 1404 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1405 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1406 | transceivers_.push_back( |
| 1407 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1408 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1409 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1410 | int delay_ms = |
| 1411 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1412 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1413 | MSG_REPORT_USAGE_PATTERN, nullptr); |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 1414 | |
| 1415 | if (dependencies.video_bitrate_allocator_factory) { |
| 1416 | video_bitrate_allocator_factory_ = |
| 1417 | std::move(dependencies.video_bitrate_allocator_factory); |
| 1418 | } else { |
| 1419 | video_bitrate_allocator_factory_ = |
| 1420 | CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1421 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1422 | return true; |
| 1423 | } |
| 1424 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1425 | RTCError PeerConnection::ValidateConfiguration( |
| 1426 | const RTCConfiguration& config) const { |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame] | 1427 | return cricket::P2PTransportChannel::ValidateIceConfig( |
| 1428 | ParseIceConfig(config)); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1431 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1432 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1433 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1434 | "Plan SdpSemantics. Please use GetSenders " |
| 1435 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1436 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1439 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1440 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1441 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1442 | "Plan SdpSemantics. Please use GetReceivers " |
| 1443 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1444 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1447 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1448 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1449 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1450 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1451 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1452 | if (IsClosed()) { |
| 1453 | return false; |
| 1454 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1455 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1458 | |
| 1459 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1460 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1461 | observer->SignalAudioTrackAdded.connect(this, |
| 1462 | &PeerConnection::OnAudioTrackAdded); |
| 1463 | observer->SignalAudioTrackRemoved.connect( |
| 1464 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1465 | observer->SignalVideoTrackAdded.connect(this, |
| 1466 | &PeerConnection::OnVideoTrackAdded); |
| 1467 | observer->SignalVideoTrackRemoved.connect( |
| 1468 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1469 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1470 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1471 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1472 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1473 | } |
| 1474 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1475 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1478 | stats_->AddStream(local_stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1479 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1480 | return true; |
| 1481 | } |
| 1482 | |
| 1483 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1484 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1485 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1486 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1487 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1488 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1489 | if (!IsClosed()) { |
| 1490 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1491 | RemoveAudioTrack(track.get(), local_stream); |
| 1492 | } |
| 1493 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1494 | RemoveVideoTrack(track.get(), local_stream); |
| 1495 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1496 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1497 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1498 | stream_observers_.erase( |
| 1499 | std::remove_if( |
| 1500 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1501 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1502 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1503 | }), |
| 1504 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1505 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1506 | if (IsClosed()) { |
| 1507 | return; |
| 1508 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1509 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1512 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1513 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1514 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1515 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1516 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1517 | if (!track) { |
| 1518 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1519 | } |
| 1520 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1521 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1522 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1523 | "Track has invalid kind: " + track->kind()); |
| 1524 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1525 | if (IsClosed()) { |
| 1526 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1527 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1528 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1529 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1530 | LOG_AND_RETURN_ERROR( |
| 1531 | RTCErrorType::INVALID_PARAMETER, |
| 1532 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1533 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1534 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1535 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1536 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1537 | if (sender_or_error.ok()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1538 | UpdateNegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1539 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1540 | } |
| 1541 | return sender_or_error; |
| 1542 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1543 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1544 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1545 | PeerConnection::AddTrackPlanB( |
| 1546 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1547 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1548 | if (stream_ids.size() > 1u) { |
| 1549 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1550 | "AddTrack with more than one stream is not " |
| 1551 | "supported with Plan B semantics."); |
| 1552 | } |
| 1553 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1554 | if (adjusted_stream_ids.empty()) { |
| 1555 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1556 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1557 | cricket::MediaType media_type = |
| 1558 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1559 | ? cricket::MEDIA_TYPE_AUDIO |
| 1560 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1561 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1562 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1563 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1564 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1565 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1566 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1567 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1568 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1569 | if (sender_info) { |
| 1570 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1571 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1572 | } else { |
| 1573 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1574 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1575 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1576 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1577 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1578 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1579 | if (sender_info) { |
| 1580 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1581 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1582 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1583 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1584 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1585 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1586 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1587 | PeerConnection::AddTrackUnifiedPlan( |
| 1588 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1589 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1590 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1591 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1592 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1593 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1594 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1595 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1596 | transceiver->internal()->set_direction( |
| 1597 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1598 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1599 | transceiver->internal()->set_direction( |
| 1600 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1601 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1602 | transceiver->sender()->SetTrack(track); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1603 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 1604 | transceiver->internal()->set_reused_for_addtrack(true); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1605 | } else { |
| 1606 | cricket::MediaType media_type = |
| 1607 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1608 | ? cricket::MEDIA_TYPE_AUDIO |
| 1609 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1610 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1611 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1612 | std::string sender_id = track->id(); |
| 1613 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1614 | // This can happen if this is the second time AddTrack has created a sender |
| 1615 | // for this track. |
| 1616 | if (FindSenderById(sender_id)) { |
| 1617 | sender_id = rtc::CreateRandomUuid(); |
| 1618 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1619 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1620 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1621 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1622 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1623 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1624 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1625 | return transceiver->sender(); |
| 1626 | } |
| 1627 | |
| 1628 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1629 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1630 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1631 | RTC_DCHECK(track); |
| 1632 | for (auto transceiver : transceivers_) { |
| 1633 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1634 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1635 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1636 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1637 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1638 | return transceiver; |
| 1639 | } |
| 1640 | } |
| 1641 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1645 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1646 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1649 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1650 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1651 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1652 | if (!sender) { |
| 1653 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1654 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1655 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1656 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1657 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1658 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1659 | if (IsUnifiedPlan()) { |
| 1660 | auto transceiver = FindTransceiverBySender(sender); |
| 1661 | if (!transceiver || !sender->track()) { |
| 1662 | return RTCError::OK(); |
| 1663 | } |
| 1664 | sender->SetTrack(nullptr); |
| 1665 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1666 | transceiver->internal()->set_direction( |
| 1667 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1668 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1669 | transceiver->internal()->set_direction( |
| 1670 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1671 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1672 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1673 | bool removed; |
| 1674 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1675 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1676 | } else { |
| 1677 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1678 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1679 | } |
| 1680 | if (!removed) { |
| 1681 | LOG_AND_RETURN_ERROR( |
| 1682 | RTCErrorType::INVALID_PARAMETER, |
| 1683 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1684 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1685 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1686 | UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1687 | return RTCError::OK(); |
| 1688 | } |
| 1689 | |
| 1690 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1691 | PeerConnection::FindTransceiverBySender( |
| 1692 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1693 | for (auto transceiver : transceivers_) { |
| 1694 | if (transceiver->sender() == sender) { |
| 1695 | return transceiver; |
| 1696 | } |
| 1697 | } |
| 1698 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1701 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1702 | PeerConnection::AddTransceiver( |
| 1703 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1704 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1705 | } |
| 1706 | |
| 1707 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1708 | PeerConnection::AddTransceiver( |
| 1709 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1710 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1711 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1712 | RTC_CHECK(IsUnifiedPlan()) |
| 1713 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1714 | if (!track) { |
| 1715 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1716 | } |
| 1717 | cricket::MediaType media_type; |
| 1718 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1719 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1720 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1721 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1722 | } else { |
| 1723 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1724 | "Track kind is not audio or video"); |
| 1725 | } |
| 1726 | return AddTransceiver(media_type, track, init); |
| 1727 | } |
| 1728 | |
| 1729 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1730 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1731 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1732 | } |
| 1733 | |
| 1734 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1735 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1736 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1737 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1738 | RTC_CHECK(IsUnifiedPlan()) |
| 1739 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1740 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1741 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1742 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1743 | "media type is not audio or video"); |
| 1744 | } |
| 1745 | return AddTransceiver(media_type, nullptr, init); |
| 1746 | } |
| 1747 | |
| 1748 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1749 | PeerConnection::AddTransceiver( |
| 1750 | cricket::MediaType media_type, |
| 1751 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1752 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1753 | bool update_negotiation_needed) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1754 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1755 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1756 | if (track) { |
| 1757 | RTC_DCHECK_EQ(media_type, |
| 1758 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1759 | ? cricket::MEDIA_TYPE_AUDIO |
| 1760 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1761 | } |
| 1762 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 1763 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1764 | init.send_encodings.size(), 0, 7, 8); |
| 1765 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1766 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1767 | [](const RtpEncodingParameters& encoding) { |
| 1768 | return !encoding.rid.empty(); |
| 1769 | }); |
| 1770 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1771 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1772 | RTCErrorType::INVALID_PARAMETER, |
| 1773 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Amit Hilbuch | f477040 | 2019-04-08 14:11:57 -0700 | [diff] [blame] | 1776 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1777 | [](const RtpEncodingParameters& encoding) { |
| 1778 | return !IsLegalRsidName(encoding.rid); |
| 1779 | })) { |
| 1780 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1781 | "Invalid RID value provided."); |
| 1782 | } |
| 1783 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1784 | if (absl::c_any_of(init.send_encodings, |
| 1785 | [](const RtpEncodingParameters& encoding) { |
| 1786 | return encoding.ssrc.has_value(); |
| 1787 | })) { |
| 1788 | LOG_AND_RETURN_ERROR( |
| 1789 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1790 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | RtpParameters parameters; |
| 1794 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1795 | |
| 1796 | // Encodings are dropped from the tail if too many are provided. |
| 1797 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1798 | parameters.encodings.erase( |
| 1799 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1800 | parameters.encodings.end()); |
| 1801 | } |
| 1802 | |
| 1803 | // Single RID should be removed. |
| 1804 | if (parameters.encodings.size() == 1 && |
| 1805 | !parameters.encodings[0].rid.empty()) { |
| 1806 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1807 | parameters.encodings[0].rid.clear(); |
| 1808 | } |
| 1809 | |
| 1810 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1811 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1812 | rtc::UniqueStringGenerator rid_generator; |
| 1813 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1814 | encoding.rid = rid_generator(); |
| 1815 | } |
| 1816 | } |
| 1817 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1818 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1819 | LOG_AND_RETURN_ERROR( |
| 1820 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1821 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1822 | } |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1823 | |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 1824 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1825 | if (!result.ok()) { |
| 1826 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1827 | } |
| 1828 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1829 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1830 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1831 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1832 | // that sender ID is already in use. |
| 1833 | std::string sender_id = |
| 1834 | (track && !FindSenderById(track->id()) ? track->id() |
| 1835 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1836 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1837 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1838 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1839 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1840 | transceiver->internal()->set_direction(init.direction); |
| 1841 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1842 | if (update_negotiation_needed) { |
| 1843 | UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1844 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1845 | |
| 1846 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1847 | } |
| 1848 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1849 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1850 | PeerConnection::CreateSender( |
| 1851 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1852 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1853 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1854 | const std::vector<std::string>& stream_ids, |
| 1855 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1856 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1857 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1858 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1859 | RTC_DCHECK(!track || |
| 1860 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1861 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1862 | signaling_thread(), |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1863 | AudioRtpSender::Create(worker_thread(), id, stats_.get(), this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1864 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1865 | } else { |
| 1866 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1867 | RTC_DCHECK(!track || |
| 1868 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1869 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1870 | signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1871 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1872 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1873 | bool set_track_succeeded = sender->SetTrack(track); |
| 1874 | RTC_DCHECK(set_track_succeeded); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1875 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1876 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1877 | return sender; |
| 1878 | } |
| 1879 | |
| 1880 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1881 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1882 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1883 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1884 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1885 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1886 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1887 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1888 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1889 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1890 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1891 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1892 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1893 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1894 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1895 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1896 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1897 | return receiver; |
| 1898 | } |
| 1899 | |
| 1900 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1901 | PeerConnection::CreateAndAddTransceiver( |
| 1902 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1903 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1904 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1905 | // Ensure that the new sender does not have an ID that is already in use by |
| 1906 | // another sender. |
| 1907 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1908 | // could be invalid, but should not cause a crash). |
| 1909 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1910 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 1911 | signaling_thread(), |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 1912 | new RtpTransceiver( |
| 1913 | sender, receiver, channel_manager(), |
| 1914 | sender->media_type() == cricket::MEDIA_TYPE_AUDIO |
| 1915 | ? channel_manager()->GetSupportedAudioRtpHeaderExtensions() |
| 1916 | : channel_manager()->GetSupportedVideoRtpHeaderExtensions())); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1917 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1918 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1919 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1920 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1921 | } |
| 1922 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1923 | void PeerConnection::OnNegotiationNeeded() { |
| 1924 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1925 | RTC_DCHECK(!IsClosed()); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1926 | UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1927 | } |
| 1928 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1929 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1930 | const std::string& kind, |
| 1931 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1932 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1933 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1934 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1935 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1936 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1937 | if (IsClosed()) { |
| 1938 | return nullptr; |
| 1939 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1940 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1941 | // Internally we need to have one stream with Plan B semantics, so we |
| 1942 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1943 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1944 | if (stream_id.empty()) { |
| 1945 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1946 | RTC_LOG(LS_INFO) |
| 1947 | << "No stream_id specified for sender. Generated stream ID: " |
| 1948 | << stream_ids[0]; |
| 1949 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1950 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1951 | } |
| 1952 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1953 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1954 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1955 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1956 | auto audio_sender = AudioRtpSender::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1957 | worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1958 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1959 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1960 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1961 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1962 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1963 | auto video_sender = |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1964 | VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1965 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1966 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1967 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1968 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1969 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1970 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1971 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1972 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1973 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1974 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1975 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1978 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1979 | const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 1980 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1981 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1982 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1983 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1984 | } |
| 1985 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1988 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1989 | PeerConnection::GetSendersInternal() const { |
| 1990 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1991 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1992 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1993 | auto senders = transceiver->internal()->senders(); |
| 1994 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1995 | } |
| 1996 | return all_senders; |
| 1997 | } |
| 1998 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1999 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 2000 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 2001 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2002 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2003 | for (const auto& receiver : GetReceiversInternal()) { |
| 2004 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2005 | } |
| 2006 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2007 | } |
| 2008 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2009 | std::vector< |
| 2010 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 2011 | PeerConnection::GetReceiversInternal() const { |
| 2012 | std::vector< |
| 2013 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 2014 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2015 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 2016 | auto receivers = transceiver->internal()->receivers(); |
| 2017 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 2018 | receivers.end()); |
| 2019 | } |
| 2020 | return all_receivers; |
| 2021 | } |
| 2022 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 2023 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2024 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2025 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 2026 | RTC_CHECK(IsUnifiedPlan()) |
| 2027 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 2028 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2029 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 2030 | all_transceivers.push_back(transceiver); |
| 2031 | } |
| 2032 | return all_transceivers; |
| 2033 | } |
| 2034 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2035 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 2036 | MediaStreamTrackInterface* track, |
| 2037 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2038 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 2039 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2040 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2041 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2042 | return false; |
| 2043 | } |
| 2044 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2045 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 2046 | // The StatsCollector is used to tell if a track is valid because it may |
| 2047 | // remember tracks that the PeerConnection previously removed. |
| 2048 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2049 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 2050 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 2051 | return false; |
| 2052 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 2053 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 2054 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2055 | return true; |
| 2056 | } |
| 2057 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 2058 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 2059 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 2060 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 2061 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 2062 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 2063 | stats_collector_->GetStatsReport(callback); |
| 2064 | } |
| 2065 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 2066 | void PeerConnection::GetStats( |
| 2067 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 2068 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 2069 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 2070 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 2071 | RTC_DCHECK(callback); |
| 2072 | RTC_DCHECK(stats_collector_); |
| 2073 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 2074 | if (selector) { |
| 2075 | for (const auto& proxy_transceiver : transceivers_) { |
| 2076 | for (const auto& proxy_sender : |
| 2077 | proxy_transceiver->internal()->senders()) { |
| 2078 | if (proxy_sender == selector) { |
| 2079 | internal_sender = proxy_sender->internal(); |
| 2080 | break; |
| 2081 | } |
| 2082 | } |
| 2083 | if (internal_sender) |
| 2084 | break; |
| 2085 | } |
| 2086 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 2087 | // If there is no |internal_sender| then |selector| is either null or does not |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 2088 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 2089 | // PeerConnection). This means that "all the stats objects representing the |
| 2090 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 2091 | // produces an empty stats report. |
| 2092 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 2093 | } |
| 2094 | |
| 2095 | void PeerConnection::GetStats( |
| 2096 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 2097 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 2098 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 2099 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 2100 | RTC_DCHECK(callback); |
| 2101 | RTC_DCHECK(stats_collector_); |
| 2102 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 2103 | if (selector) { |
| 2104 | for (const auto& proxy_transceiver : transceivers_) { |
| 2105 | for (const auto& proxy_receiver : |
| 2106 | proxy_transceiver->internal()->receivers()) { |
| 2107 | if (proxy_receiver == selector) { |
| 2108 | internal_receiver = proxy_receiver->internal(); |
| 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | if (internal_receiver) |
| 2113 | break; |
| 2114 | } |
| 2115 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 2116 | // If there is no |internal_receiver| then |selector| is either null or does |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 2117 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 2118 | // the PeerConnection). This means that "all the stats objects representing |
| 2119 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 2120 | // selector produces an empty stats report. |
| 2121 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 2122 | } |
| 2123 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2124 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2125 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2126 | return signaling_state_; |
| 2127 | } |
| 2128 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2129 | PeerConnectionInterface::IceConnectionState |
| 2130 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2131 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2132 | return ice_connection_state_; |
| 2133 | } |
| 2134 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 2135 | PeerConnectionInterface::IceConnectionState |
| 2136 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2137 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 2138 | return standardized_ice_connection_state_; |
| 2139 | } |
| 2140 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 2141 | PeerConnectionInterface::PeerConnectionState |
| 2142 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2143 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 2144 | return connection_state_; |
| 2145 | } |
| 2146 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2147 | PeerConnectionInterface::IceGatheringState |
| 2148 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2149 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2150 | return ice_gathering_state_; |
| 2151 | } |
| 2152 | |
Harald Alvestrand | 61f74d9 | 2020-03-02 11:20:00 +0100 | [diff] [blame] | 2153 | absl::optional<bool> PeerConnection::can_trickle_ice_candidates() { |
| 2154 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2155 | SessionDescriptionInterface* description = current_remote_description_.get(); |
| 2156 | if (!description) { |
| 2157 | description = pending_remote_description_.get(); |
| 2158 | } |
| 2159 | if (!description) { |
| 2160 | return absl::nullopt; |
| 2161 | } |
| 2162 | // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option. |
| 2163 | if (description->description()->transport_infos().size() < 1) { |
| 2164 | return absl::nullopt; |
| 2165 | } |
| 2166 | return description->description()->transport_infos()[0].description.HasOption( |
| 2167 | "trickle"); |
| 2168 | } |
| 2169 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2170 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2171 | const std::string& label, |
| 2172 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 10:17:47 +0100 | [diff] [blame] | 2173 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2174 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2175 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 2176 | bool first_datachannel = !data_channel_controller_.HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2177 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 2178 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 2179 | if (config) { |
| 2180 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 2181 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2182 | rtc::scoped_refptr<DataChannelInterface> channel( |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 2183 | data_channel_controller_.InternalCreateDataChannel( |
| 2184 | label, internal_config.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2185 | if (!channel.get()) { |
| 2186 | return nullptr; |
| 2187 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2188 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2189 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 2190 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2191 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2192 | UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2193 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2194 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2195 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 2196 | } |
| 2197 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 2198 | void PeerConnection::RestartIce() { |
| 2199 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2200 | local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions( |
| 2201 | current_local_description_.get(), pending_local_description_.get()); |
| 2202 | UpdateNegotiationNeeded(); |
| 2203 | } |
| 2204 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2205 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 2206 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2207 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2208 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2209 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2210 | // lambda will execute immediately. |
| 2211 | operations_chain_->ChainOperation( |
| 2212 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2213 | observer_refptr = |
| 2214 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 2215 | options](std::function<void()> operations_chain_callback) { |
| 2216 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2217 | if (!this_weak_ptr) { |
| 2218 | observer_refptr->OnFailure( |
| 2219 | RTCError(RTCErrorType::INTERNAL_ERROR, |
| 2220 | "CreateOffer failed because the session was shut down")); |
| 2221 | operations_chain_callback(); |
| 2222 | return; |
| 2223 | } |
| 2224 | // The operation completes asynchronously when the wrapper is invoked. |
| 2225 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 2226 | observer_wrapper(new rtc::RefCountedObject< |
| 2227 | CreateSessionDescriptionObserverOperationWrapper>( |
| 2228 | std::move(observer_refptr), |
| 2229 | std::move(operations_chain_callback))); |
| 2230 | this_weak_ptr->DoCreateOffer(options, observer_wrapper); |
| 2231 | }); |
| 2232 | } |
| 2233 | |
| 2234 | void PeerConnection::DoCreateOffer( |
| 2235 | const RTCOfferAnswerOptions& options, |
| 2236 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 2237 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2238 | TRACE_EVENT0("webrtc", "PeerConnection::DoCreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2239 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2240 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2241 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 2242 | return; |
| 2243 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2244 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2245 | if (IsClosed()) { |
| 2246 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2247 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2248 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2249 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2250 | return; |
| 2251 | } |
| 2252 | |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 2253 | // If a session error has occurred the PeerConnection is in a possibly |
| 2254 | // inconsistent state so fail right away. |
| 2255 | if (session_error() != SessionError::kNone) { |
| 2256 | std::string error_message = GetSessionErrorMsg(); |
| 2257 | RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message; |
| 2258 | PostCreateSessionDescriptionFailure( |
| 2259 | observer, |
| 2260 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2261 | return; |
| 2262 | } |
| 2263 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2264 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2265 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2266 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2267 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2268 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2269 | return; |
| 2270 | } |
| 2271 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2272 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 2273 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 2274 | if (IsUnifiedPlan()) { |
| 2275 | RTCError error = HandleLegacyOfferOptions(options); |
| 2276 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2277 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2278 | return; |
| 2279 | } |
| 2280 | } |
| 2281 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2282 | cricket::MediaSessionOptions session_options; |
| 2283 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2284 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2287 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 2288 | const RTCOfferAnswerOptions& options) { |
| 2289 | RTC_DCHECK(IsUnifiedPlan()); |
| 2290 | |
| 2291 | if (options.offer_to_receive_audio == 0) { |
| 2292 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2293 | cricket::MEDIA_TYPE_AUDIO); |
| 2294 | } else if (options.offer_to_receive_audio == 1) { |
| 2295 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 2296 | } else if (options.offer_to_receive_audio > 1) { |
| 2297 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2298 | "offer_to_receive_audio > 1 is not supported."); |
| 2299 | } |
| 2300 | |
| 2301 | if (options.offer_to_receive_video == 0) { |
| 2302 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2303 | cricket::MEDIA_TYPE_VIDEO); |
| 2304 | } else if (options.offer_to_receive_video == 1) { |
| 2305 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 2306 | } else if (options.offer_to_receive_video > 1) { |
| 2307 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2308 | "offer_to_receive_video > 1 is not supported."); |
| 2309 | } |
| 2310 | |
| 2311 | return RTCError::OK(); |
| 2312 | } |
| 2313 | |
| 2314 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2315 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2316 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2317 | RtpTransceiverDirection new_direction = |
| 2318 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 2319 | if (new_direction != transceiver->direction()) { |
| 2320 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 2321 | << " transceiver (MID=" |
| 2322 | << transceiver->mid().value_or("<not set>") << ") from " |
| 2323 | << RtpTransceiverDirectionToString( |
| 2324 | transceiver->direction()) |
| 2325 | << " to " |
| 2326 | << RtpTransceiverDirectionToString(new_direction) |
| 2327 | << " since CreateOffer specified offer_to_receive=0"; |
| 2328 | transceiver->internal()->set_direction(new_direction); |
| 2329 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 2334 | cricket::MediaType media_type) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 2335 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2336 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2337 | RTC_LOG(LS_INFO) |
| 2338 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 2339 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2340 | RtpTransceiverInit init; |
| 2341 | init.direction = RtpTransceiverDirection::kRecvOnly; |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2342 | AddTransceiver(media_type, nullptr, init, |
| 2343 | /*update_negotiation_needed=*/false); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2348 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 2349 | std::vector< |
| 2350 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2351 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2352 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2353 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2354 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 2355 | receiving_transceivers.push_back(transceiver); |
| 2356 | } |
| 2357 | } |
| 2358 | return receiving_transceivers; |
| 2359 | } |
| 2360 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2361 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 2362 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2363 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2364 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2365 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2366 | // lambda will execute immediately. |
| 2367 | operations_chain_->ChainOperation( |
| 2368 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2369 | observer_refptr = |
| 2370 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 2371 | options](std::function<void()> operations_chain_callback) { |
| 2372 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2373 | if (!this_weak_ptr) { |
| 2374 | observer_refptr->OnFailure(RTCError( |
| 2375 | RTCErrorType::INTERNAL_ERROR, |
| 2376 | "CreateAnswer failed because the session was shut down")); |
| 2377 | operations_chain_callback(); |
| 2378 | return; |
| 2379 | } |
| 2380 | // The operation completes asynchronously when the wrapper is invoked. |
| 2381 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 2382 | observer_wrapper(new rtc::RefCountedObject< |
| 2383 | CreateSessionDescriptionObserverOperationWrapper>( |
| 2384 | std::move(observer_refptr), |
| 2385 | std::move(operations_chain_callback))); |
| 2386 | this_weak_ptr->DoCreateAnswer(options, observer_wrapper); |
| 2387 | }); |
| 2388 | } |
| 2389 | |
| 2390 | void PeerConnection::DoCreateAnswer( |
| 2391 | const RTCOfferAnswerOptions& options, |
| 2392 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 2393 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2394 | TRACE_EVENT0("webrtc", "PeerConnection::DoCreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2395 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2396 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2397 | return; |
| 2398 | } |
| 2399 | |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 2400 | // If a session error has occurred the PeerConnection is in a possibly |
| 2401 | // inconsistent state so fail right away. |
| 2402 | if (session_error() != SessionError::kNone) { |
| 2403 | std::string error_message = GetSessionErrorMsg(); |
| 2404 | RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message; |
| 2405 | PostCreateSessionDescriptionFailure( |
| 2406 | observer, |
| 2407 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2408 | return; |
| 2409 | } |
| 2410 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2411 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 2412 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 2413 | std::string error = |
| 2414 | "PeerConnection cannot create an answer in a state other than " |
| 2415 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2416 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2417 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2418 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2419 | return; |
| 2420 | } |
| 2421 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2422 | // The remote description should be set if we're in the right state. |
| 2423 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2424 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2425 | if (IsUnifiedPlan()) { |
| 2426 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2427 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2428 | "supported with Unified Plan semantics. Use the " |
| 2429 | "RtpTransceiver API instead."; |
| 2430 | } |
| 2431 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2432 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2433 | "supported with Unified Plan semantics. Use the " |
| 2434 | "RtpTransceiver API instead."; |
| 2435 | } |
| 2436 | } |
| 2437 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2438 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2439 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2440 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2441 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | void PeerConnection::SetLocalDescription( |
| 2445 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2446 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2447 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2448 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2449 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2450 | // lambda will execute immediately. |
| 2451 | operations_chain_->ChainOperation( |
| 2452 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2453 | observer_refptr = |
| 2454 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 2455 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 2456 | std::function<void()> operations_chain_callback) mutable { |
| 2457 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2458 | if (!this_weak_ptr) { |
| 2459 | // For consistency with DoSetLocalDescription(), we DO NOT inform the |
| 2460 | // |observer_refptr| that the operation failed in this case. |
| 2461 | // TODO(hbos): If/when we process SLD messages in ~PeerConnection, |
| 2462 | // the consistent thing would be to inform the observer here. |
| 2463 | operations_chain_callback(); |
| 2464 | return; |
| 2465 | } |
| 2466 | this_weak_ptr->DoSetLocalDescription(std::move(desc), |
| 2467 | std::move(observer_refptr)); |
| 2468 | // DoSetLocalDescription() is currently implemented as a synchronous |
| 2469 | // operation but where the |observer|'s callbacks are invoked |
| 2470 | // asynchronously in a post to OnMessage(). |
| 2471 | // For backwards-compatability reasons, we declare the operation as |
Henrik Boström | 4e19670 | 2019-10-30 10:35:50 +0100 | [diff] [blame] | 2472 | // completed here (rather than in OnMessage()). This ensures that |
| 2473 | // subsequent offer/answer operations can start immediately (without |
| 2474 | // waiting for OnMessage()). |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2475 | operations_chain_callback(); |
| 2476 | }); |
| 2477 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2478 | |
Henrik Boström | 4e19670 | 2019-10-30 10:35:50 +0100 | [diff] [blame] | 2479 | void PeerConnection::SetLocalDescription( |
| 2480 | SetSessionDescriptionObserver* observer) { |
| 2481 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2482 | // The |create_sdp_observer| handles performing DoSetLocalDescription() with |
| 2483 | // the resulting description as well as completing the operation. |
| 2484 | rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver> |
| 2485 | create_sdp_observer( |
| 2486 | new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>( |
| 2487 | weak_ptr_factory_.GetWeakPtr(), |
| 2488 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer))); |
| 2489 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2490 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2491 | // lambda will execute immediately. |
| 2492 | operations_chain_->ChainOperation( |
| 2493 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2494 | create_sdp_observer](std::function<void()> operations_chain_callback) { |
| 2495 | // The |create_sdp_observer| is responsible for completing the |
| 2496 | // operation. |
| 2497 | create_sdp_observer->SetOperationCompleteCallback( |
| 2498 | std::move(operations_chain_callback)); |
| 2499 | // Abort early if |this_weak_ptr| is no longer valid. This triggers the |
| 2500 | // same code path as if DoCreateOffer() or DoCreateAnswer() failed. |
| 2501 | if (!this_weak_ptr) { |
| 2502 | create_sdp_observer->OnFailure(RTCError( |
| 2503 | RTCErrorType::INTERNAL_ERROR, |
| 2504 | "SetLocalDescription failed because the session was shut down")); |
| 2505 | return; |
| 2506 | } |
| 2507 | switch (this_weak_ptr->signaling_state()) { |
| 2508 | case PeerConnectionInterface::kStable: |
| 2509 | case PeerConnectionInterface::kHaveLocalOffer: |
| 2510 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 2511 | // TODO(hbos): If [LastCreatedOffer] exists and still represents the |
| 2512 | // current state of the system, use that instead of creating another |
| 2513 | // offer. |
| 2514 | this_weak_ptr->DoCreateOffer(RTCOfferAnswerOptions(), |
| 2515 | create_sdp_observer); |
| 2516 | break; |
| 2517 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 2518 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 2519 | // TODO(hbos): If [LastCreatedAnswer] exists and still represents |
| 2520 | // the current state of the system, use that instead of creating |
| 2521 | // another answer. |
| 2522 | this_weak_ptr->DoCreateAnswer(RTCOfferAnswerOptions(), |
| 2523 | create_sdp_observer); |
| 2524 | break; |
| 2525 | case PeerConnectionInterface::kClosed: |
| 2526 | create_sdp_observer->OnFailure(RTCError( |
| 2527 | RTCErrorType::INVALID_STATE, |
| 2528 | "SetLocalDescription called when PeerConnection is closed.")); |
| 2529 | break; |
| 2530 | } |
| 2531 | }); |
| 2532 | } |
| 2533 | |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2534 | void PeerConnection::DoSetLocalDescription( |
| 2535 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2536 | rtc::scoped_refptr<SetSessionDescriptionObserver> observer) { |
| 2537 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2538 | TRACE_EVENT0("webrtc", "PeerConnection::DoSetLocalDescription"); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2539 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2540 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2541 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2544 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2545 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2546 | PostSetSessionDescriptionFailure( |
| 2547 | observer, |
| 2548 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2549 | return; |
| 2550 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2551 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2552 | // If a session error has occurred the PeerConnection is in a possibly |
| 2553 | // inconsistent state so fail right away. |
| 2554 | if (session_error() != SessionError::kNone) { |
| 2555 | std::string error_message = GetSessionErrorMsg(); |
| 2556 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2557 | PostSetSessionDescriptionFailure( |
| 2558 | observer, |
| 2559 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2560 | return; |
| 2561 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2562 | |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 2563 | // For SLD we support only explicit rollback. |
| 2564 | if (desc->GetType() == SdpType::kRollback) { |
| 2565 | if (IsUnifiedPlan()) { |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 2566 | RTCError error = Rollback(desc->GetType()); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 2567 | if (error.ok()) { |
| 2568 | PostSetSessionDescriptionSuccess(observer); |
| 2569 | } else { |
| 2570 | PostSetSessionDescriptionFailure(observer, std::move(error)); |
| 2571 | } |
| 2572 | } else { |
| 2573 | PostSetSessionDescriptionFailure( |
| 2574 | observer, RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 2575 | "Rollback not supported in Plan B")); |
| 2576 | } |
| 2577 | return; |
| 2578 | } |
| 2579 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2580 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2581 | if (!error.ok()) { |
| 2582 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2583 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2584 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2585 | PostSetSessionDescriptionFailure( |
| 2586 | observer, |
| 2587 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2588 | return; |
| 2589 | } |
| 2590 | |
| 2591 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2592 | // which may destroy it before returning. |
| 2593 | const SdpType type = desc->GetType(); |
| 2594 | |
| 2595 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2596 | // |desc| may be destroyed at this point. |
| 2597 | |
| 2598 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2599 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2600 | // inconsistent state, so act conservatively here and set the session error |
| 2601 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2602 | SetSessionError(SessionError::kContent, error.message()); |
| 2603 | std::string error_message = |
| 2604 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2605 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2606 | PostSetSessionDescriptionFailure( |
| 2607 | observer, |
| 2608 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2609 | return; |
| 2610 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2611 | RTC_DCHECK(local_description()); |
| 2612 | |
| 2613 | PostSetSessionDescriptionSuccess(observer); |
| 2614 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 2615 | // MaybeStartGathering needs to be called after posting |
| 2616 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2617 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2618 | transport_controller_->MaybeStartGathering(); |
| 2619 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2620 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2621 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2622 | // MaybeStartGathering... |
| 2623 | network_thread()->Invoke<void>( |
| 2624 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2625 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2626 | // Make UMA notes about what was agreed to. |
| 2627 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2628 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2629 | |
| 2630 | if (IsUnifiedPlan()) { |
| 2631 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2632 | UpdateNegotiationNeeded(); |
| 2633 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2634 | is_negotiation_needed_) { |
| 2635 | Observer()->OnRenegotiationNeeded(); |
| 2636 | } |
| 2637 | } |
| 2638 | |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 2639 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | RTCError PeerConnection::ApplyLocalDescription( |
| 2643 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2644 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2645 | RTC_DCHECK(desc); |
| 2646 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2647 | // Update stats here so that we have the most recent stats for tracks and |
| 2648 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2649 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2650 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2651 | // Take a reference to the old local description since it's used below to |
| 2652 | // compare against the new local description. When setting the new local |
| 2653 | // description, grab ownership of the replaced session description in case it |
| 2654 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2655 | // of the method. |
| 2656 | const SessionDescriptionInterface* old_local_description = |
| 2657 | local_description(); |
| 2658 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2659 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2660 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2661 | replaced_local_description = pending_local_description_ |
| 2662 | ? std::move(pending_local_description_) |
| 2663 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2664 | current_local_description_ = std::move(desc); |
| 2665 | pending_local_description_ = nullptr; |
| 2666 | current_remote_description_ = std::move(pending_remote_description_); |
| 2667 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2668 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2669 | pending_local_description_ = std::move(desc); |
| 2670 | } |
| 2671 | // The session description to apply now must be accessed by |
| 2672 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2673 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2674 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 2675 | // Report statistics about any use of simulcast. |
| 2676 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 2677 | *local_description()->description()); |
| 2678 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2679 | if (!is_caller_) { |
| 2680 | if (remote_description()) { |
| 2681 | // Remote description was applied first, so this PC is the callee. |
| 2682 | is_caller_ = false; |
| 2683 | } else { |
| 2684 | // Local description is applied first, so this PC is the caller. |
| 2685 | is_caller_ = true; |
| 2686 | } |
| 2687 | } |
| 2688 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2689 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2690 | if (!error.ok()) { |
| 2691 | return error; |
| 2692 | } |
| 2693 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2694 | if (IsUnifiedPlan()) { |
| 2695 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2696 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2697 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2698 | if (!error.ok()) { |
| 2699 | return error; |
| 2700 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2701 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2702 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2703 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2704 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2705 | // Note that code paths that don't set MID won't be able to use |
| 2706 | // information about DTLS transports. |
| 2707 | if (transceiver->mid()) { |
| 2708 | auto dtls_transport = |
| 2709 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2710 | transceiver->internal()->sender_internal()->set_transport( |
| 2711 | dtls_transport); |
| 2712 | transceiver->internal()->receiver_internal()->set_transport( |
| 2713 | dtls_transport); |
| 2714 | } |
| 2715 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2716 | const ContentInfo* content = |
| 2717 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2718 | if (!content) { |
| 2719 | continue; |
| 2720 | } |
| 2721 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2722 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2723 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2724 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2725 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2726 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2727 | // "recvonly", process the removal of a remote track for the media |
| 2728 | // description, given transceiver, removeList, and muteTracks. |
| 2729 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2730 | (transceiver->internal()->fired_direction() && |
| 2731 | RtpTransceiverDirectionHasRecv( |
| 2732 | *transceiver->internal()->fired_direction()))) { |
| 2733 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2734 | &removed_streams); |
| 2735 | } |
| 2736 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2737 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2738 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2739 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2740 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2741 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2742 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2743 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2744 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2745 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2746 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2747 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2748 | } |
| 2749 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2750 | // Media channels will be created only when offer is set. These may use new |
| 2751 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2752 | if (type == SdpType::kOffer) { |
| 2753 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2754 | // description is applied. Restore back to old description. |
| 2755 | RTCError error = CreateChannels(*local_description()->description()); |
| 2756 | if (!error.ok()) { |
| 2757 | return error; |
| 2758 | } |
| 2759 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2760 | // Remove unused channels if MediaContentDescription is rejected. |
| 2761 | RemoveUnusedChannels(local_description()->description()); |
| 2762 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2763 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2764 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2765 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2766 | if (!error.ok()) { |
| 2767 | return error; |
| 2768 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2769 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2770 | if (remote_description()) { |
| 2771 | // Now that we have a local description, we can push down remote candidates. |
| 2772 | UseCandidatesInSessionDescription(remote_description()); |
| 2773 | } |
| 2774 | |
| 2775 | pending_ice_restarts_.clear(); |
| 2776 | if (session_error() != SessionError::kNone) { |
| 2777 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2778 | } |
| 2779 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2780 | // If setting the description decided our SSL role, allocate any necessary |
| 2781 | // SCTP sids. |
| 2782 | rtc::SSLRole role; |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 2783 | if (DataChannel::IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 2784 | data_channel_controller_.AllocateSctpSids(role); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2785 | } |
| 2786 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2787 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2788 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2789 | const ContentInfo* content = |
| 2790 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2791 | if (!content) { |
| 2792 | continue; |
| 2793 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2794 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2795 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2796 | // 0 is a special value meaning "this sender has no associated send |
| 2797 | // stream". Need to call this so the sender won't attempt to configure |
| 2798 | // a no longer existing stream and run into DCHECKs in the lower |
| 2799 | // layers. |
| 2800 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2801 | } else { |
| 2802 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2803 | const std::vector<StreamParams>& streams = channel->local_streams(); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 2804 | transceiver->internal()->sender_internal()->set_stream_ids( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2805 | streams[0].stream_ids()); |
| 2806 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2807 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | } else { |
| 2811 | // Plan B semantics. |
| 2812 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2813 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2814 | // local session description. |
| 2815 | const cricket::ContentInfo* audio_content = |
| 2816 | GetFirstAudioContent(local_description()->description()); |
| 2817 | if (audio_content) { |
| 2818 | if (audio_content->rejected) { |
| 2819 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2820 | } else { |
| 2821 | const cricket::AudioContentDescription* audio_desc = |
| 2822 | audio_content->media_description()->as_audio(); |
| 2823 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2824 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2825 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2826 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2827 | const cricket::ContentInfo* video_content = |
| 2828 | GetFirstVideoContent(local_description()->description()); |
| 2829 | if (video_content) { |
| 2830 | if (video_content->rejected) { |
| 2831 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2832 | } else { |
| 2833 | const cricket::VideoContentDescription* video_desc = |
| 2834 | video_content->media_description()->as_video(); |
| 2835 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2836 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2837 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2841 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2842 | if (data_content) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 2843 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 2844 | data_content->media_description()->as_rtp_data(); |
| 2845 | // rtp_data_desc will be null if this is an SCTP description. |
| 2846 | if (rtp_data_desc) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 2847 | data_channel_controller_.UpdateLocalRtpDataChannels( |
| 2848 | rtp_data_desc->streams()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 2852 | if (type == SdpType::kAnswer && |
| 2853 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 2854 | *current_local_description_)) { |
| 2855 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 2856 | } |
| 2857 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2858 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2861 | // The SDP parser used to populate these values by default for the 'content |
| 2862 | // name' if an a=mid line was absent. |
| 2863 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2864 | switch (media_type) { |
| 2865 | case cricket::MEDIA_TYPE_AUDIO: |
| 2866 | return cricket::CN_AUDIO; |
| 2867 | case cricket::MEDIA_TYPE_VIDEO: |
| 2868 | return cricket::CN_VIDEO; |
| 2869 | case cricket::MEDIA_TYPE_DATA: |
| 2870 | return cricket::CN_DATA; |
| 2871 | } |
| 2872 | RTC_NOTREACHED(); |
| 2873 | return ""; |
| 2874 | } |
| 2875 | |
| 2876 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2877 | cricket::SessionDescription* new_remote_description) { |
| 2878 | RTC_DCHECK(new_remote_description); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2879 | const cricket::ContentInfos no_infos; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2880 | const cricket::ContentInfos& local_contents = |
| 2881 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2882 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2883 | const cricket::ContentInfos& remote_contents = |
| 2884 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2885 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2886 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2887 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2888 | if (!content.name.empty()) { |
| 2889 | continue; |
| 2890 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2891 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2892 | absl::string_view source_explanation; |
| 2893 | if (IsUnifiedPlan()) { |
| 2894 | if (i < local_contents.size()) { |
| 2895 | new_mid = local_contents[i].name; |
| 2896 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2897 | } else if (i < remote_contents.size()) { |
| 2898 | new_mid = remote_contents[i].name; |
| 2899 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2900 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2901 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2902 | source_explanation = "generated just now"; |
| 2903 | } |
| 2904 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2905 | new_mid = std::string( |
| 2906 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2907 | source_explanation = "to match pre-existing behavior"; |
| 2908 | } |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2909 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2910 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2911 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2912 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2913 | << " is missing an a=mid line. Filling in the value '" |
| 2914 | << new_mid << "' " << source_explanation << "."; |
| 2915 | } |
| 2916 | } |
| 2917 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2918 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2919 | SetSessionDescriptionObserver* observer, |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2920 | SessionDescriptionInterface* desc_ptr) { |
| 2921 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2922 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2923 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2924 | // lambda will execute immediately. |
| 2925 | operations_chain_->ChainOperation( |
| 2926 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2927 | observer_refptr = |
| 2928 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 2929 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 2930 | std::function<void()> operations_chain_callback) mutable { |
| 2931 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2932 | if (!this_weak_ptr) { |
| 2933 | // For consistency with SetRemoteDescriptionObserverAdapter, we DO NOT |
| 2934 | // inform the |observer_refptr| that the operation failed in this |
| 2935 | // case. |
| 2936 | // TODO(hbos): If/when we process SRD messages in ~PeerConnection, |
| 2937 | // the consistent thing would be to inform the observer here. |
| 2938 | operations_chain_callback(); |
| 2939 | return; |
| 2940 | } |
| 2941 | this_weak_ptr->DoSetRemoteDescription( |
| 2942 | std::move(desc), |
| 2943 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2944 | new SetRemoteDescriptionObserverAdapter( |
| 2945 | this_weak_ptr.get(), std::move(observer_refptr)))); |
| 2946 | // DoSetRemoteDescription() is currently implemented as a synchronous |
| 2947 | // operation but where SetRemoteDescriptionObserverAdapter ensures that |
| 2948 | // the |observer|'s callbacks are invoked asynchronously in a post to |
| 2949 | // OnMessage(). |
| 2950 | // For backwards-compatability reasons, we declare the operation as |
Henrik Boström | 4e19670 | 2019-10-30 10:35:50 +0100 | [diff] [blame] | 2951 | // completed here (rather than in OnMessage()). This ensures that |
| 2952 | // subsequent offer/answer operations can start immediately (without |
| 2953 | // waiting for OnMessage()). |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2954 | operations_chain_callback(); |
| 2955 | }); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | void PeerConnection::SetRemoteDescription( |
| 2959 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2960 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2961 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 12:09:49 +0100 | [diff] [blame] | 2962 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2963 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2964 | // lambda will execute immediately. |
| 2965 | operations_chain_->ChainOperation( |
| 2966 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer, |
| 2967 | desc = std::move(desc)]( |
| 2968 | std::function<void()> operations_chain_callback) mutable { |
| 2969 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2970 | if (!this_weak_ptr) { |
| 2971 | // For consistency with DoSetRemoteDescription(), we DO inform the |
| 2972 | // |observer| that the operation failed in this case. |
| 2973 | observer->OnSetRemoteDescriptionComplete(RTCError( |
| 2974 | RTCErrorType::INVALID_STATE, |
| 2975 | "Failed to set remote offer sdp: failed because the session was " |
| 2976 | "shut down")); |
| 2977 | operations_chain_callback(); |
| 2978 | return; |
| 2979 | } |
| 2980 | this_weak_ptr->DoSetRemoteDescription(std::move(desc), |
| 2981 | std::move(observer)); |
| 2982 | // DoSetRemoteDescription() is currently implemented as a synchronous |
| 2983 | // operation. The |observer| will already have been informed that it |
| 2984 | // completed, and we can mark this operation as complete without any |
| 2985 | // loose ends. |
| 2986 | operations_chain_callback(); |
| 2987 | }); |
| 2988 | } |
| 2989 | |
| 2990 | void PeerConnection::DoSetRemoteDescription( |
| 2991 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2992 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
| 2993 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2994 | TRACE_EVENT0("webrtc", "PeerConnection::DoSetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2995 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2996 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2997 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2998 | return; |
| 2999 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3000 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3001 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3002 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3003 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3004 | return; |
| 3005 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 3006 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 3007 | // If a session error has occurred the PeerConnection is in a possibly |
| 3008 | // inconsistent state so fail right away. |
| 3009 | if (session_error() != SessionError::kNone) { |
| 3010 | std::string error_message = GetSessionErrorMsg(); |
| 3011 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 3012 | observer->OnSetRemoteDescriptionComplete( |
| 3013 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 3014 | return; |
| 3015 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 3016 | if (IsUnifiedPlan()) { |
| 3017 | if (configuration_.enable_implicit_rollback) { |
| 3018 | if (desc->GetType() == SdpType::kOffer && |
| 3019 | signaling_state() == kHaveLocalOffer) { |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 3020 | Rollback(desc->GetType()); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 3021 | } |
| 3022 | } |
| 3023 | // Explicit rollback. |
| 3024 | if (desc->GetType() == SdpType::kRollback) { |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 3025 | observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType())); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 3026 | return; |
| 3027 | } |
| 3028 | } else if (desc->GetType() == SdpType::kRollback) { |
| 3029 | observer->OnSetRemoteDescriptionComplete( |
| 3030 | RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 3031 | "Rollback not supported in Plan B")); |
| 3032 | return; |
| 3033 | } |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 3034 | if (desc->GetType() == SdpType::kOffer) { |
| 3035 | // Report to UMA the format of the received offer. |
| 3036 | ReportSdpFormatReceived(*desc); |
| 3037 | } |
| 3038 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 3039 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 3040 | // points. |
| 3041 | FillInMissingRemoteMids(desc->description()); |
| 3042 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 3043 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 3044 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 3045 | std::string error_message = GetSetDescriptionErrorMessage( |
| 3046 | cricket::CS_REMOTE, desc->GetType(), error); |
| 3047 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 3048 | observer->OnSetRemoteDescriptionComplete( |
| 3049 | RTCError(error.type(), std::move(error_message))); |
| 3050 | return; |
| 3051 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 3052 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 3053 | // Grab the description type before moving ownership to |
| 3054 | // ApplyRemoteDescription, which may destroy it before returning. |
| 3055 | const SdpType type = desc->GetType(); |
| 3056 | |
| 3057 | error = ApplyRemoteDescription(std::move(desc)); |
| 3058 | // |desc| may be destroyed at this point. |
| 3059 | |
| 3060 | if (!error.ok()) { |
| 3061 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 3062 | // inconsistent state, so act conservatively here and set the session error |
| 3063 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 3064 | SetSessionError(SessionError::kContent, error.message()); |
| 3065 | std::string error_message = |
| 3066 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 3067 | RTC_LOG(LS_ERROR) << error_message; |
| 3068 | observer->OnSetRemoteDescriptionComplete( |
| 3069 | RTCError(error.type(), std::move(error_message))); |
| 3070 | return; |
| 3071 | } |
| 3072 | RTC_DCHECK(remote_description()); |
| 3073 | |
| 3074 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3075 | // TODO(deadbeef): We already had to hop to the network thread for |
| 3076 | // MaybeStartGathering... |
| 3077 | network_thread()->Invoke<void>( |
| 3078 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3079 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 3080 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 3081 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3082 | } |
| 3083 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 3084 | if (IsUnifiedPlan()) { |
| 3085 | bool was_negotiation_needed = is_negotiation_needed_; |
| 3086 | UpdateNegotiationNeeded(); |
| 3087 | if (signaling_state() == kStable && was_negotiation_needed && |
| 3088 | is_negotiation_needed_) { |
| 3089 | Observer()->OnRenegotiationNeeded(); |
| 3090 | } |
| 3091 | } |
| 3092 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3093 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 3094 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | RTCError PeerConnection::ApplyRemoteDescription( |
| 3098 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 3099 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3100 | RTC_DCHECK(desc); |
| 3101 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3102 | // Update stats here so that we have the most recent stats for tracks and |
| 3103 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3104 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3105 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3106 | // Take a reference to the old remote description since it's used below to |
| 3107 | // compare against the new remote description. When setting the new remote |
| 3108 | // description, grab ownership of the replaced session description in case it |
| 3109 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 3110 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3111 | const SessionDescriptionInterface* old_remote_description = |
| 3112 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3113 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 3114 | SdpType type = desc->GetType(); |
| 3115 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3116 | replaced_remote_description = pending_remote_description_ |
| 3117 | ? std::move(pending_remote_description_) |
| 3118 | : std::move(current_remote_description_); |
| 3119 | current_remote_description_ = std::move(desc); |
| 3120 | pending_remote_description_ = nullptr; |
| 3121 | current_local_description_ = std::move(pending_local_description_); |
| 3122 | } else { |
| 3123 | replaced_remote_description = std::move(pending_remote_description_); |
| 3124 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3125 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3126 | // The session description to apply now must be accessed by |
| 3127 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 3128 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3129 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 3130 | // Report statistics about any use of simulcast. |
| 3131 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 3132 | *remote_description()->description()); |
| 3133 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3134 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 3135 | if (!error.ok()) { |
| 3136 | return error; |
| 3137 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3138 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3139 | if (IsUnifiedPlan()) { |
| 3140 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3141 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 3142 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3143 | if (!error.ok()) { |
| 3144 | return error; |
| 3145 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3146 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3147 | // Media channels will be created only when offer is set. These may use new |
| 3148 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3149 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3150 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3151 | // description is applied. Restore back to old description. |
| 3152 | RTCError error = CreateChannels(*remote_description()->description()); |
| 3153 | if (!error.ok()) { |
| 3154 | return error; |
| 3155 | } |
| 3156 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3157 | // Remove unused channels if MediaContentDescription is rejected. |
| 3158 | RemoveUnusedChannels(remote_description()->description()); |
| 3159 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3160 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3161 | // NOTE: Candidates allocation will be initiated only when |
| 3162 | // SetLocalDescription is called. |
| 3163 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 3164 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3165 | if (!error.ok()) { |
| 3166 | return error; |
| 3167 | } |
| 3168 | |
| 3169 | if (local_description() && |
| 3170 | !UseCandidatesInSessionDescription(remote_description())) { |
| 3171 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 3172 | } |
| 3173 | |
| 3174 | if (old_remote_description) { |
| 3175 | for (const cricket::ContentInfo& content : |
| 3176 | old_remote_description->description()->contents()) { |
| 3177 | // Check if this new SessionDescription contains new ICE ufrag and |
| 3178 | // password that indicates the remote peer requests an ICE restart. |
| 3179 | // TODO(deadbeef): When we start storing both the current and pending |
| 3180 | // remote description, this should reset pending_ice_restarts and compare |
| 3181 | // against the current description. |
| 3182 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 3183 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 3184 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3185 | pending_ice_restarts_.insert(content.name); |
| 3186 | } |
| 3187 | } else { |
| 3188 | // We retain all received candidates only if ICE is not restarted. |
| 3189 | // When ICE is restarted, all previous candidates belong to an old |
| 3190 | // generation and should not be kept. |
| 3191 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 3192 | // description should only contain candidates from the last set remote |
| 3193 | // description plus any candidates added since then. We should remove |
| 3194 | // this once we're sure it won't break anything. |
| 3195 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 3196 | old_remote_description, content.name, mutable_remote_description()); |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | if (session_error() != SessionError::kNone) { |
| 3202 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 3203 | } |
| 3204 | |
| 3205 | // Set the the ICE connection state to connecting since the connection may |
| 3206 | // become writable with peer reflexive candidates before any remote candidate |
| 3207 | // is signaled. |
| 3208 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 3209 | // is to have a new signal the indicates a change in checking state from the |
| 3210 | // transport and expose a new checking() member from transport that can be |
| 3211 | // read to determine the current checking state. The existing SignalConnecting |
| 3212 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 3213 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 3214 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3215 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 3216 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 3217 | } |
| 3218 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3219 | // If setting the description decided our SSL role, allocate any necessary |
| 3220 | // SCTP sids. |
| 3221 | rtc::SSLRole role; |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 3222 | if (DataChannel::IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 3223 | data_channel_controller_.AllocateSctpSids(role); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3226 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 3227 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 3228 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3229 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 3230 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 3231 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3232 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3233 | const ContentInfo* content = |
| 3234 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 3235 | if (!content) { |
| 3236 | continue; |
| 3237 | } |
| 3238 | const MediaContentDescription* media_desc = content->media_description(); |
| 3239 | RtpTransceiverDirection local_direction = |
| 3240 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3241 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 3242 | // RTCSessionDescription: Set the associated remote streams given |
| 3243 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 3244 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 3245 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 3246 | std::vector<std::string> stream_ids; |
| 3247 | if (!media_desc->streams().empty()) { |
| 3248 | // The remote description has signaled the stream IDs. |
| 3249 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 3250 | } |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 3251 | transceiver_stable_states_by_transceivers_[transceiver] |
| 3252 | .SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids()); |
| 3253 | |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3254 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 3255 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 3256 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 3257 | stream_ids, &added_streams, |
| 3258 | &removed_streams); |
| 3259 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 3260 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 3261 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 3262 | // process the addition of a remote track for the media description. |
| 3263 | if (!transceiver->fired_direction() || |
| 3264 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 3265 | RTC_LOG(LS_INFO) |
| 3266 | << "Processing the addition of a remote track for MID=" |
| 3267 | << content->name << "."; |
| 3268 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 11:25:05 +0100 | [diff] [blame] | 3269 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3270 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3271 | // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3272 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 3273 | // removal of a remote track for the media description, given transceiver, |
| 3274 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3275 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3276 | (transceiver->fired_direction() && |
| 3277 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 3278 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 3279 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3280 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3281 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3282 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3283 | // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3284 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3285 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3286 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3287 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3288 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3289 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 3290 | if (transceiver->mid()) { |
| 3291 | auto dtls_transport = |
| 3292 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 3293 | transceiver->internal()->sender_internal()->set_transport( |
| 3294 | dtls_transport); |
| 3295 | transceiver->internal()->receiver_internal()->set_transport( |
| 3296 | dtls_transport); |
| 3297 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3298 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3299 | // 2.2.8.1.12: If the media description is rejected, and transceiver is |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3300 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3301 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3302 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 3303 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3304 | transceiver->Stop(); |
| 3305 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 3306 | if (!content->rejected && |
| 3307 | RtpTransceiverDirectionHasRecv(local_direction)) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 3308 | if (!media_desc->streams().empty() && |
| 3309 | media_desc->streams()[0].has_ssrcs()) { |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 3310 | uint32_t ssrc = media_desc->streams()[0].first_ssrc(); |
| 3311 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
| 3312 | } else { |
| 3313 | transceiver->internal() |
| 3314 | ->receiver_internal() |
| 3315 | ->SetupUnsignaledMediaChannel(); |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 3316 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3317 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3318 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 3319 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3320 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3321 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 3322 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3323 | observer->OnTrack(transceiver); |
| 3324 | observer->OnAddTrack(transceiver->receiver(), |
| 3325 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 3326 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3327 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3328 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 3329 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3330 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3331 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 3332 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3333 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3334 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 3335 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3336 | } |
| 3337 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 3338 | const cricket::ContentInfo* audio_content = |
| 3339 | GetFirstAudioContent(remote_description()->description()); |
| 3340 | const cricket::ContentInfo* video_content = |
| 3341 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3342 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 3343 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3344 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 3345 | GetFirstVideoContentDescription(remote_description()->description()); |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 3346 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 3347 | GetFirstRtpDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3348 | |
| 3349 | // Check if the descriptions include streams, just in case the peer supports |
| 3350 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 3351 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3352 | (audio_desc && !audio_desc->streams().empty()) || |
| 3353 | (video_desc && !video_desc->streams().empty())) { |
| 3354 | remote_peer_supports_msid_ = true; |
| 3355 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3356 | |
| 3357 | // We wait to signal new streams until we finish processing the description, |
| 3358 | // since only at that point will new streams have all their tracks. |
| 3359 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 3360 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3361 | if (!IsUnifiedPlan()) { |
| 3362 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 3363 | // rejected media section, there is some cleanup logic that expects the |
| 3364 | // voice/ video channel to still be set. But in this method the voice/video |
| 3365 | // channel would have been destroyed by the SetRemoteDescription caller |
| 3366 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 3367 | // calls should be moved to right before the DestroyChannel calls to fix |
| 3368 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 3369 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3370 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 3371 | // and MediaStreams. |
| 3372 | if (audio_content) { |
| 3373 | if (audio_content->rejected) { |
| 3374 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 3375 | } else { |
| 3376 | bool default_audio_track_needed = |
| 3377 | !remote_peer_supports_msid_ && |
| 3378 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 3379 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 3380 | default_audio_track_needed, audio_desc->type(), |
| 3381 | new_streams); |
| 3382 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 3383 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3384 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3385 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 3386 | // and MediaStreams. |
| 3387 | if (video_content) { |
| 3388 | if (video_content->rejected) { |
| 3389 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 3390 | } else { |
| 3391 | bool default_video_track_needed = |
| 3392 | !remote_peer_supports_msid_ && |
| 3393 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 3394 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 3395 | default_video_track_needed, video_desc->type(), |
| 3396 | new_streams); |
| 3397 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 3398 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3399 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 3400 | // If this is an RTP data transport, update the DataChannels with the |
| 3401 | // information from the remote peer. |
| 3402 | if (rtp_data_desc) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 3403 | data_channel_controller_.UpdateRemoteRtpDataChannels( |
| 3404 | GetActiveStreams(rtp_data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3405 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3406 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3407 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3408 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3409 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 3410 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 3411 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3412 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3413 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 3414 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3415 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3416 | UpdateEndedRemoteMediaStreams(); |
| 3417 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3418 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 3419 | if (type == SdpType::kAnswer && |
| 3420 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 3421 | *current_local_description_)) { |
| 3422 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 3423 | } |
| 3424 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3425 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3428 | void PeerConnection::SetAssociatedRemoteStreams( |
| 3429 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 3430 | const std::vector<std::string>& stream_ids, |
| 3431 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 3432 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3433 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 3434 | for (const std::string& stream_id : stream_ids) { |
| 3435 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3436 | remote_streams_->find(stream_id); |
| 3437 | if (!stream) { |
| 3438 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3439 | MediaStream::Create(stream_id)); |
| 3440 | remote_streams_->AddStream(stream); |
| 3441 | added_streams->push_back(stream); |
| 3442 | } |
| 3443 | media_streams.push_back(stream); |
| 3444 | } |
| 3445 | // Special case: "a=msid" missing, use random stream ID. |
| 3446 | if (media_streams.empty() && |
| 3447 | !(remote_description()->description()->msid_signaling() & |
| 3448 | cricket::kMsidSignalingMediaSection)) { |
| 3449 | if (!missing_msid_default_stream_) { |
| 3450 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 3451 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 3452 | added_streams->push_back(missing_msid_default_stream_); |
| 3453 | } |
| 3454 | media_streams.push_back(missing_msid_default_stream_); |
| 3455 | } |
| 3456 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 3457 | receiver->streams(); |
| 3458 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 3459 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 3460 | // update the stream-track relationships in a later step. We do this earlier, |
| 3461 | // changing the order of things, but the end-result is the same. |
| 3462 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 3463 | // instead. https://crbug.com/webrtc/9480 |
| 3464 | receiver->SetStreams(media_streams); |
| 3465 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3466 | } |
| 3467 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3468 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 3469 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3470 | transceiver, |
| 3471 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 3472 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3473 | RTC_DCHECK(transceiver->mid()); |
| 3474 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 3475 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3476 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3477 | transceiver->internal()->receiver_internal()->streams(); |
| 3478 | // This will remove the remote track from the streams. |
| 3479 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 3480 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3481 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3482 | } |
| 3483 | |
| 3484 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 3485 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 3486 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3487 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 3488 | // streams, see if the stream was removed by checking if this was the last |
| 3489 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3490 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3491 | if (remote_stream->GetAudioTracks().empty() && |
| 3492 | remote_stream->GetVideoTracks().empty()) { |
| 3493 | remote_streams_->RemoveStream(remote_stream); |
| 3494 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3495 | } |
| 3496 | } |
| 3497 | } |
| 3498 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3499 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 3500 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3501 | const SessionDescriptionInterface& new_session, |
| 3502 | const SessionDescriptionInterface* old_local_description, |
| 3503 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3504 | RTC_DCHECK(IsUnifiedPlan()); |
| 3505 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 3506 | const cricket::ContentGroup* bundle_group = nullptr; |
| 3507 | if (new_session.GetType() == SdpType::kOffer) { |
| 3508 | auto bundle_group_or_error = |
| 3509 | GetEarlyBundleGroup(*new_session.description()); |
| 3510 | if (!bundle_group_or_error.ok()) { |
| 3511 | return bundle_group_or_error.MoveError(); |
| 3512 | } |
| 3513 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3514 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3515 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3516 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3517 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 3518 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3519 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 3520 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3521 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3522 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3523 | const cricket::ContentInfo* old_local_content = nullptr; |
| 3524 | if (old_local_description && |
| 3525 | i < old_local_description->description()->contents().size()) { |
| 3526 | old_local_content = |
| 3527 | &old_local_description->description()->contents()[i]; |
| 3528 | } |
| 3529 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 3530 | if (old_remote_description && |
| 3531 | i < old_remote_description->description()->contents().size()) { |
| 3532 | old_remote_content = |
| 3533 | &old_remote_description->description()->contents()[i]; |
| 3534 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3535 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3536 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 3537 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3538 | if (!transceiver_or_error.ok()) { |
| 3539 | return transceiver_or_error.MoveError(); |
| 3540 | } |
| 3541 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3542 | RTCError error = |
| 3543 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 3544 | if (!error.ok()) { |
| 3545 | return error; |
| 3546 | } |
| 3547 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3548 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 3549 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3550 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 3551 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3552 | continue; |
| 3553 | } |
| 3554 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 3555 | if (!error.ok()) { |
| 3556 | return error; |
| 3557 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3558 | } else { |
| 3559 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3560 | "Unknown section type."); |
| 3561 | } |
| 3562 | } |
| 3563 | |
| 3564 | return RTCError::OK(); |
| 3565 | } |
| 3566 | |
| 3567 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 3568 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3569 | transceiver, |
| 3570 | const cricket::ContentInfo& content, |
| 3571 | const cricket::ContentGroup* bundle_group) { |
| 3572 | RTC_DCHECK(IsUnifiedPlan()); |
| 3573 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3574 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3575 | if (content.rejected) { |
| 3576 | if (channel) { |
| 3577 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3578 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3579 | } |
| 3580 | } else { |
| 3581 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3582 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3583 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3584 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3585 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3586 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3587 | } |
| 3588 | if (!channel) { |
| 3589 | LOG_AND_RETURN_ERROR( |
| 3590 | RTCErrorType::INTERNAL_ERROR, |
| 3591 | "Failed to create channel for mid=" + content.name); |
| 3592 | } |
| 3593 | transceiver->internal()->SetChannel(channel); |
| 3594 | } |
| 3595 | } |
| 3596 | return RTCError::OK(); |
| 3597 | } |
| 3598 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3599 | RTCError PeerConnection::UpdateDataChannel( |
| 3600 | cricket::ContentSource source, |
| 3601 | const cricket::ContentInfo& content, |
| 3602 | const cricket::ContentGroup* bundle_group) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 3603 | if (data_channel_type() == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3604 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3605 | // will take care of rejecting it. |
| 3606 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3607 | } |
| 3608 | if (content.rejected) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3609 | RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid(); |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 3610 | DestroyDataChannelTransport(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3611 | } else { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 3612 | if (!data_channel_controller_.rtp_data_channel() && |
| 3613 | !data_channel_controller_.data_channel_transport()) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3614 | RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3615 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3616 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3617 | "Failed to create data channel."); |
| 3618 | } |
| 3619 | } |
| 3620 | if (source == cricket::CS_REMOTE) { |
| 3621 | const MediaContentDescription* data_desc = content.media_description(); |
| 3622 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 3623 | data_channel_controller_.UpdateRemoteRtpDataChannels( |
| 3624 | GetActiveStreams(data_desc)); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3625 | } |
| 3626 | } |
| 3627 | } |
| 3628 | return RTCError::OK(); |
| 3629 | } |
| 3630 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3631 | // This method will extract any send encodings that were sent by the remote |
| 3632 | // connection. This is currently only relevant for Simulcast scenario (where |
| 3633 | // the number of layers may be communicated by the server). |
| 3634 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 3635 | const MediaContentDescription& desc) { |
| 3636 | if (!desc.HasSimulcast()) { |
| 3637 | return {}; |
| 3638 | } |
| 3639 | std::vector<RtpEncodingParameters> result; |
| 3640 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 3641 | |
| 3642 | // This is a remote description, the parameters we are after should appear |
| 3643 | // as receive streams. |
| 3644 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 3645 | RTC_DCHECK(!alternatives.empty()); |
| 3646 | // There is currently no way to specify or choose from alternatives. |
| 3647 | // We will always use the first alternative, which is the most preferred. |
| 3648 | const SimulcastLayer& layer = alternatives[0]; |
| 3649 | RtpEncodingParameters parameters; |
| 3650 | parameters.rid = layer.rid; |
| 3651 | parameters.active = !layer.is_paused; |
| 3652 | result.push_back(parameters); |
| 3653 | } |
| 3654 | |
| 3655 | return result; |
| 3656 | } |
| 3657 | |
| 3658 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 3659 | const std::vector<SimulcastLayer>& layers, |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3660 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3661 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3662 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3663 | std::vector<std::string> disabled_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3664 | |
| 3665 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 3666 | // So we will iterate over the send encodings rather than the layers. |
| 3667 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3668 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 3669 | [&encoding](const SimulcastLayer& layer) { |
| 3670 | return layer.rid == encoding.rid; |
| 3671 | }); |
| 3672 | // A layer that cannot be found may have been removed by the remote party. |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3673 | if (iter == layers.end()) { |
| 3674 | disabled_layers.push_back(encoding.rid); |
| 3675 | continue; |
| 3676 | } |
| 3677 | |
| 3678 | encoding.active = !iter->is_paused; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3679 | } |
| 3680 | |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3681 | RTCError result = sender->SetParametersInternal(parameters); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3682 | if (result.ok()) { |
| 3683 | result = sender->DisableEncodingLayers(disabled_layers); |
| 3684 | } |
| 3685 | |
| 3686 | return result; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3687 | } |
| 3688 | |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3689 | static bool SimulcastIsRejected( |
| 3690 | const ContentInfo* local_content, |
| 3691 | const MediaContentDescription& answer_media_desc) { |
| 3692 | bool simulcast_offered = local_content && |
| 3693 | local_content->media_description() && |
| 3694 | local_content->media_description()->HasSimulcast(); |
| 3695 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 3696 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 3697 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 3698 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 3699 | } |
| 3700 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3701 | static RTCError DisableSimulcastInSender( |
| 3702 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3703 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3704 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3705 | if (parameters.encodings.size() <= 1) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3706 | return RTCError::OK(); |
| 3707 | } |
| 3708 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3709 | std::vector<std::string> disabled_layers; |
| 3710 | std::transform( |
| 3711 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 3712 | std::back_inserter(disabled_layers), |
| 3713 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 3714 | return sender->DisableEncodingLayers(disabled_layers); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3715 | } |
| 3716 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3717 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3718 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3719 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3720 | size_t mline_index, |
| 3721 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3722 | const ContentInfo* old_local_content, |
| 3723 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3724 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3725 | // If this is an offer then the m= section might be recycled. If the m= |
| 3726 | // section is being recycled (defined as: rejected in the current local or |
| 3727 | // remote description and not rejected in new description), dissociate the |
| 3728 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3729 | // and discard the mapping between the transceiver and its m= section index. |
| 3730 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3731 | old_remote_content)) { |
| 3732 | // We want to dissociate the transceiver that has the rejected mid. |
| 3733 | const std::string& old_mid = |
| 3734 | (old_local_content && old_local_content->rejected) |
| 3735 | ? old_local_content->name |
| 3736 | : old_remote_content->name; |
| 3737 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3738 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3739 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3740 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3741 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3742 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3743 | } |
| 3744 | } |
| 3745 | const MediaContentDescription* media_desc = content.media_description(); |
| 3746 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3747 | if (source == cricket::CS_LOCAL) { |
| 3748 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3749 | // mapping between transceivers and m= section indices established when |
| 3750 | // creating the offer. |
| 3751 | if (!transceiver) { |
| 3752 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3753 | } |
| 3754 | if (!transceiver) { |
| 3755 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3756 | "Unknown transceiver"); |
| 3757 | } |
| 3758 | } else { |
| 3759 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3760 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3761 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3762 | // When simulcast is requested, a transceiver cannot be associated because |
| 3763 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3764 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3765 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3766 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3767 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3768 | } |
| 3769 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3770 | // recvonly direction. |
| 3771 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3772 | RTC_LOG(LS_INFO) << "Adding " |
| 3773 | << cricket::MediaTypeToString(media_desc->type()) |
| 3774 | << " transceiver for MID=" << content.name |
| 3775 | << " at i=" << mline_index |
| 3776 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3777 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3778 | std::vector<RtpEncodingParameters> send_encodings = |
| 3779 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3780 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3781 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3782 | std::string receiver_id; |
| 3783 | if (!media_desc->streams().empty()) { |
| 3784 | receiver_id = media_desc->streams()[0].id; |
| 3785 | } else { |
| 3786 | receiver_id = rtc::CreateRandomUuid(); |
| 3787 | } |
| 3788 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3789 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3790 | transceiver->internal()->set_direction( |
| 3791 | RtpTransceiverDirection::kRecvOnly); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 3792 | if (type == SdpType::kOffer) { |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 3793 | transceiver_stable_states_by_transceivers_[transceiver] |
| 3794 | .set_newly_created(); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 3795 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3796 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3797 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3798 | // This can happen when simulcast is not supported on the remote party. |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3799 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 3800 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3801 | RTCError error = |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3802 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3803 | if (!error.ok()) { |
| 3804 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3805 | return std::move(error); |
| 3806 | } |
| 3807 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3808 | } |
| 3809 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3810 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3811 | LOG_AND_RETURN_ERROR( |
| 3812 | RTCErrorType::INVALID_PARAMETER, |
| 3813 | "Transceiver type does not match media description type."); |
| 3814 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3815 | if (media_desc->HasSimulcast()) { |
| 3816 | std::vector<SimulcastLayer> layers = |
| 3817 | source == cricket::CS_LOCAL |
| 3818 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3819 | : media_desc->simulcast_description() |
| 3820 | .receive_layers() |
| 3821 | .GetAllLayers(); |
| 3822 | RTCError error = UpdateSimulcastLayerStatusInSender( |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3823 | layers, transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3824 | if (!error.ok()) { |
| 3825 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3826 | return std::move(error); |
| 3827 | } |
| 3828 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 3829 | if (type == SdpType::kOffer) { |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 3830 | bool state_changes = transceiver->internal()->mid() != content.name || |
| 3831 | transceiver->internal()->mline_index() != mline_index; |
| 3832 | if (state_changes) { |
| 3833 | transceiver_stable_states_by_transceivers_[transceiver] |
| 3834 | .SetMSectionIfUnset(transceiver->internal()->mid(), |
| 3835 | transceiver->internal()->mline_index()); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 3836 | } |
| 3837 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3838 | // Associate the found or created RtpTransceiver with the m= section by |
| 3839 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3840 | // section, and establish a mapping between the transceiver and the index of |
| 3841 | // the m= section. |
| 3842 | transceiver->internal()->set_mid(content.name); |
| 3843 | transceiver->internal()->set_mline_index(mline_index); |
| 3844 | return std::move(transceiver); |
| 3845 | } |
| 3846 | |
| 3847 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3848 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3849 | RTC_DCHECK(IsUnifiedPlan()); |
| 3850 | for (auto transceiver : transceivers_) { |
| 3851 | if (transceiver->mid() == mid) { |
| 3852 | return transceiver; |
| 3853 | } |
| 3854 | } |
| 3855 | return nullptr; |
| 3856 | } |
| 3857 | |
| 3858 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3859 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3860 | RTC_DCHECK(IsUnifiedPlan()); |
| 3861 | for (auto transceiver : transceivers_) { |
| 3862 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3863 | return transceiver; |
| 3864 | } |
| 3865 | } |
| 3866 | return nullptr; |
| 3867 | } |
| 3868 | |
| 3869 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3870 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3871 | cricket::MediaType media_type) const { |
| 3872 | RTC_DCHECK(IsUnifiedPlan()); |
| 3873 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3874 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3875 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3876 | // associated with any m= section and are not stopped, find the first such |
| 3877 | // RtpTransceiver. |
| 3878 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3879 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3880 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3881 | !transceiver->stopped()) { |
| 3882 | return transceiver; |
| 3883 | } |
| 3884 | } |
| 3885 | return nullptr; |
| 3886 | } |
| 3887 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3888 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3889 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3890 | transceiver, |
| 3891 | const SessionDescriptionInterface* sdesc) const { |
| 3892 | RTC_DCHECK(transceiver); |
| 3893 | RTC_DCHECK(sdesc); |
| 3894 | if (IsUnifiedPlan()) { |
| 3895 | if (!transceiver->internal()->mid()) { |
| 3896 | // This transceiver is not associated with a media section yet. |
| 3897 | return nullptr; |
| 3898 | } |
| 3899 | return sdesc->description()->GetContentByName( |
| 3900 | *transceiver->internal()->mid()); |
| 3901 | } else { |
| 3902 | // Plan B only allows at most one audio and one video section, so use the |
| 3903 | // first media section of that type. |
| 3904 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3905 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3906 | } |
| 3907 | } |
| 3908 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3909 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3910 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3911 | return configuration_; |
| 3912 | } |
| 3913 | |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3914 | RTCError PeerConnection::SetConfiguration( |
| 3915 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3916 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3917 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3918 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3919 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 3920 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3921 | } |
| 3922 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3923 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3924 | // size is not allowed, and changing the set of ICE servers will not result |
| 3925 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3926 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3927 | configuration_.ice_candidate_pool_size) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3928 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3929 | "Can't change candidate pool size after calling " |
| 3930 | "SetLocalDescription."); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3931 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3932 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3933 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3934 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3935 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3936 | "Can't change crypto_options after calling " |
| 3937 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3938 | } |
| 3939 | |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3940 | if (local_description() && configuration.use_datagram_transport != |
| 3941 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3942 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3943 | "Can't change use_datagram_transport " |
| 3944 | "after calling SetLocalDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | if (remote_description() && configuration.use_datagram_transport != |
| 3948 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3949 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3950 | "Can't change use_datagram_transport " |
| 3951 | "after calling SetRemoteDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3952 | } |
| 3953 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3954 | if (local_description() && |
| 3955 | configuration.use_datagram_transport_for_data_channels != |
| 3956 | configuration_.use_datagram_transport_for_data_channels) { |
| 3957 | LOG_AND_RETURN_ERROR( |
| 3958 | RTCErrorType::INVALID_MODIFICATION, |
| 3959 | "Can't change use_datagram_transport_for_data_channels " |
| 3960 | "after calling SetLocalDescription."); |
| 3961 | } |
| 3962 | |
| 3963 | if (remote_description() && |
| 3964 | configuration.use_datagram_transport_for_data_channels != |
| 3965 | configuration_.use_datagram_transport_for_data_channels) { |
| 3966 | LOG_AND_RETURN_ERROR( |
| 3967 | RTCErrorType::INVALID_MODIFICATION, |
| 3968 | "Can't change use_datagram_transport_for_data_channels " |
| 3969 | "after calling SetRemoteDescription."); |
| 3970 | } |
| 3971 | |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 3972 | if (local_description() && |
| 3973 | configuration.use_datagram_transport_for_data_channels_receive_only != |
| 3974 | configuration_ |
| 3975 | .use_datagram_transport_for_data_channels_receive_only) { |
| 3976 | LOG_AND_RETURN_ERROR( |
| 3977 | RTCErrorType::INVALID_MODIFICATION, |
| 3978 | "Can't change use_datagram_transport_for_data_channels_receive_only " |
| 3979 | "after calling SetLocalDescription."); |
| 3980 | } |
| 3981 | |
| 3982 | if (remote_description() && |
| 3983 | configuration.use_datagram_transport_for_data_channels_receive_only != |
| 3984 | configuration_ |
| 3985 | .use_datagram_transport_for_data_channels_receive_only) { |
| 3986 | LOG_AND_RETURN_ERROR( |
| 3987 | RTCErrorType::INVALID_MODIFICATION, |
| 3988 | "Can't change use_datagram_transport_for_data_channels_receive_only " |
| 3989 | "after calling SetRemoteDescription."); |
| 3990 | } |
| 3991 | |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 3992 | if ((configuration.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3993 | *configuration.use_datagram_transport) || |
| 3994 | (configuration.use_datagram_transport_for_data_channels && |
| 3995 | *configuration.use_datagram_transport_for_data_channels)) { |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3996 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 3997 | << "Media transport requires MaxBundle policy."; |
| 3998 | } |
| 3999 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4000 | // The simplest (and most future-compatible) way to tell if the config was |
| 4001 | // modified in an invalid way is to copy each property we do support |
| 4002 | // modifying, then use operator==. There are far more properties we don't |
| 4003 | // support modifying than those we do, and more could be added. |
| 4004 | RTCConfiguration modified_config = configuration_; |
| 4005 | modified_config.servers = configuration.servers; |
| 4006 | modified_config.type = configuration.type; |
| 4007 | modified_config.ice_candidate_pool_size = |
| 4008 | configuration.ice_candidate_pool_size; |
| 4009 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 4010 | modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy; |
Qingsi Wang | bca1485 | 2019-06-26 14:56:02 -0700 | [diff] [blame] | 4011 | modified_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 4012 | configuration.surface_ice_candidates_on_ice_transport_type_changed; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 4013 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 4014 | modified_config.ice_check_interval_strong_connectivity = |
| 4015 | configuration.ice_check_interval_strong_connectivity; |
| 4016 | modified_config.ice_check_interval_weak_connectivity = |
| 4017 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 4018 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 4019 | modified_config.ice_unwritable_min_checks = |
| 4020 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 4021 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4022 | modified_config.stun_candidate_keepalive_interval = |
| 4023 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4024 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4025 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 4026 | modified_config.active_reset_srtp_params = |
| 4027 | configuration.active_reset_srtp_params; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 4028 | modified_config.use_datagram_transport = configuration.use_datagram_transport; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4029 | modified_config.use_datagram_transport_for_data_channels = |
| 4030 | configuration.use_datagram_transport_for_data_channels; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 4031 | modified_config.use_datagram_transport_for_data_channels_receive_only = |
| 4032 | configuration.use_datagram_transport_for_data_channels_receive_only; |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 4033 | modified_config.turn_logging_id = configuration.turn_logging_id; |
philipel | 16cec3b | 2019-10-25 12:23:02 +0200 | [diff] [blame] | 4034 | modified_config.allow_codec_switching = configuration.allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4035 | if (configuration != modified_config) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 4036 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 4037 | "Modifying the configuration in an unsupported way."); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4038 | } |
| 4039 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 4040 | // Validate the modified configuration. |
| 4041 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 4042 | if (!validate_error.ok()) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 4043 | return validate_error; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 4044 | } |
| 4045 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4046 | // Note that this isn't possible through chromium, since it's an unsigned |
| 4047 | // short in WebIDL. |
| 4048 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 4049 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 4050 | return RTCError(RTCErrorType::INVALID_RANGE); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | // Parse ICE servers before hopping to network thread. |
| 4054 | cricket::ServerAddresses stun_servers; |
| 4055 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 4056 | RTCErrorType parse_error = |
| 4057 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 4058 | if (parse_error != RTCErrorType::NONE) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 4059 | return RTCError(parse_error); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4060 | } |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 4061 | // Add the turn logging id to all turn servers |
| 4062 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 4063 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 4064 | } |
| 4065 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4066 | // Note if STUN or TURN servers were supplied. |
| 4067 | if (!stun_servers.empty()) { |
| 4068 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 4069 | } |
| 4070 | if (!turn_servers.empty()) { |
| 4071 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 4072 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4073 | |
| 4074 | // In theory this shouldn't fail. |
| 4075 | if (!network_thread()->Invoke<bool>( |
| 4076 | RTC_FROM_HERE, |
| 4077 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 4078 | stun_servers, turn_servers, modified_config.type, |
| 4079 | modified_config.ice_candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 4080 | modified_config.GetTurnPortPrunePolicy(), |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4081 | modified_config.turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4082 | modified_config.stun_candidate_keepalive_interval, |
| 4083 | static_cast<bool>(local_description())))) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 4084 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4085 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4086 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4087 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 4088 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 4089 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 4090 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4091 | if (modified_config.servers != configuration_.servers || |
| 4092 | modified_config.type != configuration_.type || |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 4093 | modified_config.GetTurnPortPrunePolicy() != |
| 4094 | configuration_.GetTurnPortPrunePolicy()) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4095 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 4096 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 4097 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 4098 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 4099 | |
| 4100 | use_datagram_transport_ = datagram_transport_config_.enabled && |
| 4101 | modified_config.use_datagram_transport.value_or( |
| 4102 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4103 | use_datagram_transport_for_data_channels_ = |
| 4104 | datagram_transport_data_channel_config_.enabled && |
| 4105 | modified_config.use_datagram_transport_for_data_channels.value_or( |
| 4106 | datagram_transport_data_channel_config_.default_value); |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 4107 | use_datagram_transport_for_data_channels_receive_only_ = |
| 4108 | modified_config.use_datagram_transport_for_data_channels_receive_only |
| 4109 | .value_or(datagram_transport_data_channel_config_.receive_only); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 4110 | transport_controller_->SetMediaTransportSettings( |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 4111 | use_datagram_transport_, use_datagram_transport_for_data_channels_, |
| 4112 | use_datagram_transport_for_data_channels_receive_only_); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 4113 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 4114 | if (configuration_.active_reset_srtp_params != |
| 4115 | modified_config.active_reset_srtp_params) { |
| 4116 | transport_controller_->SetActiveResetSrtpParams( |
| 4117 | modified_config.active_reset_srtp_params); |
| 4118 | } |
| 4119 | |
philipel | 16cec3b | 2019-10-25 12:23:02 +0200 | [diff] [blame] | 4120 | if (modified_config.allow_codec_switching.has_value()) { |
philipel | 01294f0 | 2019-11-14 13:03:25 +0100 | [diff] [blame] | 4121 | cricket::VideoMediaChannel* video_channel = video_media_channel(); |
| 4122 | if (video_channel) { |
| 4123 | video_channel->SetVideoCodecSwitchingEnabled( |
| 4124 | *modified_config.allow_codec_switching); |
| 4125 | } |
philipel | 16cec3b | 2019-10-25 12:23:02 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4128 | configuration_ = modified_config; |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 4129 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4132 | bool PeerConnection::AddIceCandidate( |
| 4133 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 4134 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 4135 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4136 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4137 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4138 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4139 | return false; |
| 4140 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4141 | |
| 4142 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4143 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4144 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4145 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4146 | return false; |
| 4147 | } |
| 4148 | |
| 4149 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4150 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4151 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4152 | return false; |
| 4153 | } |
| 4154 | |
| 4155 | bool valid = false; |
| 4156 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 4157 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4158 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4159 | return false; |
| 4160 | } |
| 4161 | |
| 4162 | // Add this candidate to the remote session description. |
| 4163 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4164 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4165 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4166 | return false; |
| 4167 | } |
| 4168 | |
| 4169 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4170 | bool result = UseCandidate(ice_candidate); |
| 4171 | if (result) { |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 4172 | NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED); |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4173 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 4174 | } else { |
| 4175 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 4176 | } |
| 4177 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4178 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4179 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 4180 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4181 | return true; |
| 4182 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
Henrik Boström | ee6f4f6 | 2019-11-06 12:36:12 +0100 | [diff] [blame] | 4185 | void PeerConnection::AddIceCandidate( |
| 4186 | std::unique_ptr<IceCandidateInterface> candidate, |
| 4187 | std::function<void(RTCError)> callback) { |
| 4188 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4189 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 4190 | // this operation will be queued to be invoked, otherwise the contents of the |
| 4191 | // lambda will execute immediately. |
| 4192 | operations_chain_->ChainOperation( |
| 4193 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 4194 | candidate = std::move(candidate), callback = std::move(callback)]( |
| 4195 | std::function<void()> operations_chain_callback) { |
| 4196 | if (!this_weak_ptr) { |
| 4197 | operations_chain_callback(); |
| 4198 | callback(RTCError( |
| 4199 | RTCErrorType::INVALID_STATE, |
| 4200 | "AddIceCandidate failed because the session was shut down")); |
| 4201 | return; |
| 4202 | } |
| 4203 | if (!this_weak_ptr->AddIceCandidate(candidate.get())) { |
| 4204 | operations_chain_callback(); |
| 4205 | // Fail with an error type and message consistent with Chromium. |
| 4206 | // TODO(hbos): Fail with error types according to spec. |
| 4207 | callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 4208 | "Error processing ICE candidate")); |
| 4209 | return; |
| 4210 | } |
| 4211 | operations_chain_callback(); |
| 4212 | callback(RTCError::OK()); |
| 4213 | }); |
| 4214 | } |
| 4215 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4216 | bool PeerConnection::RemoveIceCandidates( |
| 4217 | const std::vector<cricket::Candidate>& candidates) { |
| 4218 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 4219 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4220 | if (IsClosed()) { |
| 4221 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 4222 | return false; |
| 4223 | } |
| 4224 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4225 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4226 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 4227 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4228 | return false; |
| 4229 | } |
| 4230 | |
| 4231 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4232 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4233 | return false; |
| 4234 | } |
| 4235 | |
| 4236 | size_t number_removed = |
| 4237 | mutable_remote_description()->RemoveCandidates(candidates); |
| 4238 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4239 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4240 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4241 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4242 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4246 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 4247 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 4248 | RTC_LOG(LS_ERROR) |
| 4249 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 4250 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 4251 | } |
| 4252 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4253 | } |
| 4254 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 4255 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 4256 | if (!worker_thread()->IsCurrent()) { |
| 4257 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4258 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4259 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 4260 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4261 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 4262 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 4263 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 4264 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4265 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 4266 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4267 | "min_bitrate_bps <= 0"); |
| 4268 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 4269 | if (has_start) { |
| 4270 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4271 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 4272 | "start_bitrate_bps < min_bitrate_bps"); |
| 4273 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4274 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4275 | "curent_bitrate_bps < 0"); |
| 4276 | } |
| 4277 | } |
| 4278 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4279 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4280 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 4281 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4282 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 4283 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4284 | "max_bitrate_bps < min_bitrate_bps"); |
| 4285 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 4286 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4287 | "max_bitrate_bps < 0"); |
| 4288 | } |
| 4289 | } |
| 4290 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4291 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 10:31:54 -0700 | [diff] [blame] | 4292 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 4293 | |
| 4294 | return RTCError::OK(); |
| 4295 | } |
| 4296 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 4297 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 4298 | if (!worker_thread()->IsCurrent()) { |
| 4299 | worker_thread()->Invoke<void>( |
| 4300 | RTC_FROM_HERE, |
| 4301 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 4302 | return; |
| 4303 | } |
| 4304 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 4305 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 4306 | audio_state->SetPlayout(playout); |
| 4307 | } |
| 4308 | |
| 4309 | void PeerConnection::SetAudioRecording(bool recording) { |
| 4310 | if (!worker_thread()->IsCurrent()) { |
| 4311 | worker_thread()->Invoke<void>( |
| 4312 | RTC_FROM_HERE, |
| 4313 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 4314 | return; |
| 4315 | } |
| 4316 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 4317 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 4318 | audio_state->SetRecording(recording); |
| 4319 | } |
| 4320 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 4321 | std::unique_ptr<rtc::SSLCertificate> |
| 4322 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 4323 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 4324 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 4325 | return nullptr; |
| 4326 | } |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 4327 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 4328 | } |
| 4329 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 4330 | std::unique_ptr<rtc::SSLCertChain> |
| 4331 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 4332 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 4333 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 4334 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 4335 | return nullptr; |
| 4336 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 4337 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 4338 | audio_transceiver->internal()->channel()->transport_name()); |
| 4339 | } |
| 4340 | |
| 4341 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4342 | PeerConnection::GetFirstAudioTransceiver() const { |
| 4343 | for (auto transceiver : transceivers_) { |
| 4344 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4345 | return transceiver; |
| 4346 | } |
| 4347 | } |
| 4348 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 4349 | } |
| 4350 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4351 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 4352 | int64_t output_period_ms) { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4353 | return worker_thread()->Invoke<bool>( |
Danil Chapovalov | 116ffe7 | 2019-09-05 10:21:11 +0200 | [diff] [blame] | 4354 | RTC_FROM_HERE, |
| 4355 | [this, output = std::move(output), output_period_ms]() mutable { |
| 4356 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 4357 | }); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
Niels Möller | f00ca1a | 2019-05-10 11:33:12 +0200 | [diff] [blame] | 4360 | bool PeerConnection::StartRtcEventLog( |
| 4361 | std::unique_ptr<RtcEventLogOutput> output) { |
Niels Möller | 695cf6a | 2019-05-13 12:27:23 +0200 | [diff] [blame] | 4362 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 4363 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 4364 | output_period_ms = 5000; |
| 4365 | } |
| 4366 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 11:33:12 +0200 | [diff] [blame] | 4367 | } |
| 4368 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4369 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 4370 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4371 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 4372 | } |
| 4373 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 4374 | rtc::scoped_refptr<DtlsTransportInterface> |
| 4375 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 4376 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 4377 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 4378 | } |
| 4379 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 4380 | rtc::scoped_refptr<DtlsTransport> |
| 4381 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 4382 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 4383 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 4384 | } |
| 4385 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 4386 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 4387 | const { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 4388 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 4389 | if (!sctp_mid_s_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 4390 | return nullptr; |
| 4391 | } |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 4392 | return transport_controller_->GetSctpTransport(*sctp_mid_s_); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 4393 | } |
| 4394 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4395 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4396 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4397 | return pending_local_description_ ? pending_local_description_.get() |
| 4398 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4402 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4403 | return pending_remote_description_ ? pending_remote_description_.get() |
| 4404 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 4407 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 4408 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4409 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4410 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 4414 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4415 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4416 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 4420 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4421 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4422 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 4426 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4427 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4428 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 4429 | } |
| 4430 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4431 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 4432 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 4433 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4434 | // Update stats here so that we have the most recent stats for tracks and |
| 4435 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 4436 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4437 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4438 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4439 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 4440 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 4441 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 4442 | transceiver->Stop(); |
| 4443 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 4444 | |
| 4445 | // Ensure that all asynchronous stats requests are completed before destroying |
| 4446 | // the transport controller below. |
| 4447 | if (stats_collector_) { |
| 4448 | stats_collector_->WaitForPendingRequest(); |
| 4449 | } |
| 4450 | |
| 4451 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 4452 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 4453 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4454 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 4455 | // The event log is used in the transport controller, which must be outlived |
| 4456 | // by the former. CreateOffer by the peer connection is implemented |
| 4457 | // asynchronously and if the peer connection is closed without resetting the |
| 4458 | // WebRTC session description factory, the session description factory would |
| 4459 | // call the transport controller. |
| 4460 | webrtc_session_desc_factory_.reset(); |
| 4461 | transport_controller_.reset(); |
| 4462 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 4463 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4464 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 4465 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4466 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 4467 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 4468 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 4469 | call_.reset(); |
| 4470 | // The event log must outlive call (and any other object that uses it). |
| 4471 | event_log_.reset(); |
| 4472 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4473 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4474 | // The .h file says that observer can be discarded after close() returns. |
| 4475 | // Make sure this is true. |
| 4476 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4479 | void PeerConnection::OnMessage(rtc::Message* msg) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 4480 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4481 | switch (msg->message_id) { |
| 4482 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 4483 | SetSessionDescriptionMsg* param = |
| 4484 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4485 | param->observer->OnSuccess(); |
| 4486 | delete param; |
| 4487 | break; |
| 4488 | } |
| 4489 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 4490 | SetSessionDescriptionMsg* param = |
| 4491 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4492 | param->observer->OnFailure(std::move(param->error)); |
| 4493 | delete param; |
| 4494 | break; |
| 4495 | } |
| 4496 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 4497 | CreateSessionDescriptionMsg* param = |
| 4498 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 4499 | param->observer->OnFailure(std::move(param->error)); |
| 4500 | delete param; |
| 4501 | break; |
| 4502 | } |
| 4503 | case MSG_GETSTATS: { |
| 4504 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 4505 | StatsReports reports; |
| 4506 | stats_->GetStats(param->track, &reports); |
| 4507 | param->observer->OnComplete(reports); |
| 4508 | delete param; |
| 4509 | break; |
| 4510 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4511 | case MSG_REPORT_USAGE_PATTERN: { |
| 4512 | ReportUsagePattern(); |
| 4513 | break; |
| 4514 | } |
| 4515 | default: |
| 4516 | RTC_NOTREACHED() << "Not implemented"; |
| 4517 | break; |
| 4518 | } |
| 4519 | } |
| 4520 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 4521 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 4522 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4523 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 4524 | GetAudioTransceiver()->internal()->channel()); |
| 4525 | if (voice_channel) { |
| 4526 | return voice_channel->media_channel(); |
| 4527 | } else { |
| 4528 | return nullptr; |
| 4529 | } |
| 4530 | } |
| 4531 | |
| 4532 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 4533 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4534 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 4535 | GetVideoTransceiver()->internal()->channel()); |
| 4536 | if (video_channel) { |
| 4537 | return video_channel->media_channel(); |
| 4538 | } else { |
| 4539 | return nullptr; |
| 4540 | } |
| 4541 | } |
| 4542 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4543 | void PeerConnection::CreateAudioReceiver( |
| 4544 | MediaStreamInterface* stream, |
| 4545 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 4546 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4547 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 4548 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4549 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4550 | auto* audio_receiver = new AudioRtpReceiver( |
| 4551 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4552 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 4553 | if (remote_sender_info.sender_id == kDefaultAudioSenderId) { |
| 4554 | audio_receiver->SetupUnsignaledMediaChannel(); |
| 4555 | } else { |
| 4556 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4557 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4558 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4559 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4560 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 4561 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4562 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4565 | void PeerConnection::CreateVideoReceiver( |
| 4566 | MediaStreamInterface* stream, |
| 4567 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 4568 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4569 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 4570 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4571 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4572 | auto* video_receiver = new VideoRtpReceiver( |
| 4573 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4574 | video_receiver->SetMediaChannel(video_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 4575 | if (remote_sender_info.sender_id == kDefaultVideoSenderId) { |
| 4576 | video_receiver->SetupUnsignaledMediaChannel(); |
| 4577 | } else { |
| 4578 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4579 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4580 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4581 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4582 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 4583 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4584 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4587 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 4588 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4589 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4590 | const RtpSenderInfo& remote_sender_info) { |
| 4591 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 4592 | if (!receiver) { |
| 4593 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 4594 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4595 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4596 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4597 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4598 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 4599 | } else { |
| 4600 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 4601 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4602 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4605 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 4606 | MediaStreamInterface* stream) { |
| 4607 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4608 | RTC_DCHECK(track); |
| 4609 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4610 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4611 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4612 | // We already have a sender for this track, so just change the stream_id |
| 4613 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 4614 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4615 | return; |
| 4616 | } |
| 4617 | |
| 4618 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4619 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4620 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4621 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4622 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4623 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 4624 | // which will connect the sender to the underlying transport. This can |
| 4625 | // occur if a local session description that contains the ID of the sender |
| 4626 | // is set before AddStream is called. It can also occur if the local |
| 4627 | // session description is not changed and RemoveStream is called, and |
| 4628 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4629 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4630 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4631 | if (sender_info) { |
| 4632 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 4637 | // indefinitely, when we have unified plan SDP. |
| 4638 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 4639 | MediaStreamInterface* stream) { |
| 4640 | RTC_DCHECK(!IsClosed()); |
| 4641 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4642 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4643 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4644 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4645 | return; |
| 4646 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4647 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 4651 | MediaStreamInterface* stream) { |
| 4652 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4653 | RTC_DCHECK(track); |
| 4654 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4655 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4656 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4657 | // We already have a sender for this track, so just change the stream_id |
| 4658 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 4659 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4660 | return; |
| 4661 | } |
| 4662 | |
| 4663 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4664 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4665 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4666 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4667 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 4668 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4669 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4670 | if (sender_info) { |
| 4671 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 4676 | MediaStreamInterface* stream) { |
| 4677 | RTC_DCHECK(!IsClosed()); |
| 4678 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4679 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4680 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4681 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4682 | return; |
| 4683 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4684 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4685 | } |
| 4686 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4687 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4688 | if (ice_connection_state_ == new_state) { |
| 4689 | return; |
| 4690 | } |
| 4691 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4692 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4693 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4694 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4695 | return; |
| 4696 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4697 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4698 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 4699 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4700 | RTC_DCHECK(ice_connection_state_ != |
| 4701 | PeerConnectionInterface::kIceConnectionClosed); |
| 4702 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4703 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4704 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4707 | void PeerConnection::SetStandardizedIceConnectionState( |
| 4708 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4709 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4710 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4714 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 4718 | << standardized_ice_connection_state_ << " => " << new_state; |
| 4719 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4720 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 11:25:14 +0100 | [diff] [blame] | 4721 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4724 | void PeerConnection::SetConnectionState( |
| 4725 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4726 | if (connection_state_ == new_state) |
| 4727 | return; |
| 4728 | if (IsClosed()) |
| 4729 | return; |
| 4730 | connection_state_ = new_state; |
| 4731 | Observer()->OnConnectionChange(new_state); |
| 4732 | } |
| 4733 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4734 | void PeerConnection::OnIceGatheringChange( |
| 4735 | PeerConnectionInterface::IceGatheringState new_state) { |
| 4736 | if (IsClosed()) { |
| 4737 | return; |
| 4738 | } |
| 4739 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4740 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 4743 | void PeerConnection::OnIceCandidate( |
| 4744 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4745 | if (IsClosed()) { |
| 4746 | return; |
| 4747 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 4748 | ReportIceCandidateCollected(candidate->candidate()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4749 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
Eldar Rello | 0095d37 | 2019-12-02 22:22:07 +0200 | [diff] [blame] | 4752 | void PeerConnection::OnIceCandidateError(const std::string& address, |
| 4753 | int port, |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 4754 | const std::string& url, |
| 4755 | int error_code, |
| 4756 | const std::string& error_text) { |
| 4757 | if (IsClosed()) { |
| 4758 | return; |
| 4759 | } |
Eldar Rello | 0095d37 | 2019-12-02 22:22:07 +0200 | [diff] [blame] | 4760 | Observer()->OnIceCandidateError(address, port, url, error_code, error_text); |
| 4761 | // Leftover not to break wpt test during migration to the new API. |
| 4762 | Observer()->OnIceCandidateError(address + ":", url, error_code, error_text); |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 4763 | } |
| 4764 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4765 | void PeerConnection::OnIceCandidatesRemoved( |
| 4766 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4767 | if (IsClosed()) { |
| 4768 | return; |
| 4769 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4770 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4771 | } |
| 4772 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 4773 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 4774 | const cricket::CandidatePairChangeEvent& event) { |
| 4775 | if (IsClosed()) { |
| 4776 | return; |
| 4777 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 4778 | |
Qingsi Wang | cc46b10 | 2019-09-12 11:19:01 -0700 | [diff] [blame] | 4779 | if (event.selected_candidate_pair.local_candidate().type() == |
| 4780 | LOCAL_PORT_TYPE && |
| 4781 | event.selected_candidate_pair.remote_candidate().type() == |
| 4782 | LOCAL_PORT_TYPE) { |
| 4783 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 4784 | } |
| 4785 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 4786 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 4787 | } |
| 4788 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4789 | void PeerConnection::ChangeSignalingState( |
| 4790 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4791 | if (signaling_state_ == signaling_state) { |
| 4792 | return; |
| 4793 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4794 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 4795 | << GetSignalingStateString(signaling_state_) |
| 4796 | << " New state: " |
| 4797 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4798 | signaling_state_ = signaling_state; |
| 4799 | if (signaling_state == kClosed) { |
| 4800 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4801 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4802 | standardized_ice_connection_state_ = |
| 4803 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4804 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 4805 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4806 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4807 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4810 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4811 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4812 | if (IsClosed()) { |
| 4813 | return; |
| 4814 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4815 | AddAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4816 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4817 | } |
| 4818 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4819 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4820 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4821 | if (IsClosed()) { |
| 4822 | return; |
| 4823 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4824 | RemoveAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4825 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4829 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4830 | if (IsClosed()) { |
| 4831 | return; |
| 4832 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4833 | AddVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4834 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4838 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4839 | if (IsClosed()) { |
| 4840 | return; |
| 4841 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4842 | RemoveVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4843 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4844 | } |
| 4845 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4846 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4847 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4848 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4849 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4850 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4851 | } |
| 4852 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4853 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4854 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4855 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4856 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4857 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4858 | msg->error = std::move(error); |
| 4859 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4860 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4864 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4865 | RTCError error) { |
| 4866 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4867 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4868 | msg->error = std::move(error); |
| 4869 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4870 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4871 | } |
| 4872 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4873 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4874 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4875 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4876 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4877 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4878 | if (IsUnifiedPlan()) { |
| 4879 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4880 | } else { |
| 4881 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 4882 | } |
| 4883 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4884 | // Intentionally unset the data channel type for RTP data channel with the |
| 4885 | // second condition. Otherwise the RTP data channels would be successfully |
| 4886 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4887 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4888 | // people won't try to use them. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 4889 | if (data_channel_controller_.HasRtpDataChannels() || |
| 4890 | data_channel_type() != cricket::DCT_RTP) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4891 | session_options->data_channel_type = data_channel_type(); |
| 4892 | } |
| 4893 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4894 | // Apply ICE restart flag and renomination flag. |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 4895 | bool ice_restart = offer_answer_options.ice_restart || |
| 4896 | local_ice_credentials_to_replace_->HasIceCredentials(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4897 | for (auto& options : session_options->media_description_options) { |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 4898 | options.transport_options.ice_restart = ice_restart; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4899 | options.transport_options.enable_ice_renomination = |
| 4900 | configuration_.enable_ice_renomination; |
| 4901 | } |
| 4902 | |
| 4903 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4904 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4905 | session_options->pooled_ice_credentials = |
| 4906 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4907 | RTC_FROM_HERE, |
| 4908 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4909 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 4910 | session_options->offer_extmap_allow_mixed = |
| 4911 | configuration_.offer_extmap_allow_mixed; |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 4912 | |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4913 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4914 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4915 | for (auto& options : session_options->media_description_options) { |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame] | 4916 | absl::optional<cricket::OpaqueTransportParameters> params = |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4917 | transport_controller_->GetTransportParameters(options.mid); |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame] | 4918 | if (!params) { |
| 4919 | continue; |
| 4920 | } |
| 4921 | options.transport_options.opaque_parameters = params; |
| 4922 | if ((use_datagram_transport_ && |
| 4923 | (options.type == cricket::MEDIA_TYPE_AUDIO || |
| 4924 | options.type == cricket::MEDIA_TYPE_VIDEO)) || |
| 4925 | (use_datagram_transport_for_data_channels_ && |
| 4926 | options.type == cricket::MEDIA_TYPE_DATA)) { |
| 4927 | options.alt_protocol = params->protocol; |
| 4928 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4929 | } |
| 4930 | } |
| 4931 | |
Harald Alvestrand | 4aa1192 | 2019-05-14 22:00:01 +0200 | [diff] [blame] | 4932 | // Allow fallback for using obsolete SCTP syntax. |
| 4933 | // Note that the default in |session_options| is true, while |
| 4934 | // the default in |options| is false. |
| 4935 | session_options->use_obsolete_sctp_sdp = |
| 4936 | offer_answer_options.use_obsolete_sctp_sdp; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4940 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4941 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4942 | // Figure out transceiver directional preferences. |
| 4943 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4944 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4945 | |
| 4946 | // By default, generate sendrecv/recvonly m= sections. |
| 4947 | bool recv_audio = true; |
| 4948 | bool recv_video = true; |
| 4949 | |
| 4950 | // By default, only offer a new m= section if we have media to send with it. |
| 4951 | bool offer_new_audio_description = send_audio; |
| 4952 | bool offer_new_video_description = send_video; |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 4953 | bool offer_new_data_description = data_channel_controller_.HasDataChannels(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4954 | |
| 4955 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4956 | if (offer_answer_options.offer_to_receive_audio != |
| 4957 | RTCOfferAnswerOptions::kUndefined) { |
| 4958 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4959 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4960 | offer_new_audio_description || |
| 4961 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4962 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4963 | if (offer_answer_options.offer_to_receive_video != |
| 4964 | RTCOfferAnswerOptions::kUndefined) { |
| 4965 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4966 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4967 | offer_new_video_description || |
| 4968 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4969 | } |
| 4970 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4971 | absl::optional<size_t> audio_index; |
| 4972 | absl::optional<size_t> video_index; |
| 4973 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4974 | // If a current description exists, generate m= sections in the same order, |
| 4975 | // using the first audio/video/data section that appears and rejecting |
| 4976 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4977 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4978 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4979 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4980 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4981 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4982 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4983 | } |
| 4984 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4985 | // Add audio/video/data m= sections to the end if needed. |
| 4986 | if (!audio_index && offer_new_audio_description) { |
| 4987 | session_options->media_description_options.push_back( |
| 4988 | cricket::MediaDescriptionOptions( |
| 4989 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4990 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4991 | false)); |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 4992 | |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4993 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4994 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4995 | if (!video_index && offer_new_video_description) { |
| 4996 | session_options->media_description_options.push_back( |
| 4997 | cricket::MediaDescriptionOptions( |
| 4998 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4999 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 5000 | false)); |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 5001 | |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5002 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 5003 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5004 | if (!data_index && offer_new_data_description) { |
| 5005 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5006 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5007 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5008 | } |
| 5009 | |
| 5010 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 5011 | !audio_index ? nullptr |
| 5012 | : &session_options->media_description_options[*audio_index]; |
| 5013 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 5014 | !video_index ? nullptr |
| 5015 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5016 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 5017 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 5018 | audio_media_description_options, |
| 5019 | video_media_description_options, |
| 5020 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5021 | } |
| 5022 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5023 | static cricket::MediaDescriptionOptions |
| 5024 | GetMediaDescriptionOptionsForTransceiver( |
| 5025 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5026 | transceiver, |
| 5027 | const std::string& mid) { |
| 5028 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5029 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5030 | transceiver->stopped()); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 5031 | media_description_options.codec_preferences = |
| 5032 | transceiver->codec_preferences(); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 5033 | // This behavior is specified in JSEP. The gist is that: |
| 5034 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 5035 | // sendrecv. |
| 5036 | // 2. If the MSID is included, then it must be included in any subsequent |
| 5037 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 5038 | if (transceiver->stopped() || |
| 5039 | (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 5040 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 5041 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 5042 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 5043 | |
| 5044 | cricket::SenderOptions sender_options; |
| 5045 | sender_options.track_id = transceiver->sender()->id(); |
| 5046 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 5047 | |
| 5048 | // The following sets up RIDs and Simulcast. |
| 5049 | // RIDs are included if Simulcast is requested or if any RID was specified. |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 5050 | RtpParameters send_parameters = |
| 5051 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 5052 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 5053 | send_parameters.encodings.end(), |
| 5054 | [](const RtpEncodingParameters& encoding) { |
| 5055 | return !encoding.rid.empty(); |
| 5056 | }); |
| 5057 | |
Amit Hilbuch | b7446ed | 2019-01-28 12:25:25 -0800 | [diff] [blame] | 5058 | std::vector<RidDescription> send_rids; |
| 5059 | SimulcastLayerList send_layers; |
| 5060 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 5061 | if (encoding.rid.empty()) { |
| 5062 | continue; |
| 5063 | } |
| 5064 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 5065 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 5066 | } |
| 5067 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 5068 | if (has_rids) { |
| 5069 | sender_options.rids = send_rids; |
| 5070 | } |
| 5071 | |
| 5072 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 5073 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 5074 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 5075 | // simulcast, or simulcast is acheived by munging the SDP. |
| 5076 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 5077 | media_description_options.sender_options.push_back(sender_options); |
| 5078 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5079 | return media_description_options; |
| 5080 | } |
| 5081 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5082 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 5083 | static const ContentInfo* GetContentByIndex( |
| 5084 | const SessionDescriptionInterface* sdesc, |
| 5085 | size_t i) { |
| 5086 | if (!sdesc) { |
| 5087 | return nullptr; |
| 5088 | } |
| 5089 | const ContentInfos& contents = sdesc->description()->contents(); |
| 5090 | return (i < contents.size() ? &contents[i] : nullptr); |
| 5091 | } |
| 5092 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5093 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 5094 | const RTCOfferAnswerOptions& offer_answer_options, |
| 5095 | cricket::MediaSessionOptions* session_options) { |
| 5096 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 5097 | // Offers) and 5.2.2 (Subsequent Offers). |
| 5098 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 5099 | const ContentInfos no_infos; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5100 | const ContentInfos& local_contents = |
| 5101 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 5102 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5103 | const ContentInfos& remote_contents = |
| 5104 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 5105 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5106 | // The mline indices that can be recycled. New transceivers should reuse these |
| 5107 | // slots first. |
| 5108 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5109 | // First, go through each media section that exists in either the local or |
| 5110 | // remote description and generate a media section in this offer for the |
| 5111 | // associated transceiver. If a media section can be recycled, generate a |
| 5112 | // default, rejected media section here that can be later overwritten. |
| 5113 | for (size_t i = 0; |
| 5114 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 5115 | // Either |local_content| or |remote_content| is non-null. |
| 5116 | const ContentInfo* local_content = |
| 5117 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5118 | const ContentInfo* current_local_content = |
| 5119 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5120 | const ContentInfo* remote_content = |
| 5121 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5122 | const ContentInfo* current_remote_content = |
| 5123 | GetContentByIndex(current_remote_description(), i); |
| 5124 | bool had_been_rejected = |
| 5125 | (current_local_content && current_local_content->rejected) || |
| 5126 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5127 | const std::string& mid = |
| 5128 | (local_content ? local_content->name : remote_content->name); |
| 5129 | cricket::MediaType media_type = |
| 5130 | (local_content ? local_content->media_description()->type() |
| 5131 | : remote_content->media_description()->type()); |
| 5132 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5133 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5134 | auto transceiver = GetAssociatedTransceiver(mid); |
| 5135 | RTC_CHECK(transceiver); |
| 5136 | // A media section is considered eligible for recycling if it is marked as |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5137 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5138 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5139 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5140 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 5141 | RtpTransceiverDirection::kInactive, |
| 5142 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5143 | recycleable_mline_indices.push(i); |
| 5144 | } else { |
| 5145 | session_options->media_description_options.push_back( |
| 5146 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 5147 | // CreateOffer shouldn't really cause any state changes in |
| 5148 | // PeerConnection, but we need a way to match new transceivers to new |
| 5149 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 5150 | // by recording the index of the media section generated for the |
| 5151 | // transceiver in the offer. |
| 5152 | transceiver->internal()->set_mline_index(i); |
| 5153 | } |
| 5154 | } else { |
| 5155 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | c8ff160 | 2020-02-05 13:53:38 -0800 | [diff] [blame] | 5156 | if (had_been_rejected) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5157 | session_options->media_description_options.push_back( |
| 5158 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 5159 | } else { |
Steve Anton | c8ff160 | 2020-02-05 13:53:38 -0800 | [diff] [blame] | 5160 | RTC_CHECK(GetDataMid()); |
| 5161 | if (mid == *GetDataMid()) { |
| 5162 | session_options->media_description_options.push_back( |
| 5163 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 5164 | } else { |
| 5165 | session_options->media_description_options.push_back( |
| 5166 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 5167 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5168 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5169 | } |
| 5170 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 5171 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5172 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 5173 | // and not associated). Reuse media sections marked as recyclable first, |
| 5174 | // otherwise append to the end of the offer. New media sections should be |
| 5175 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5176 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5177 | if (transceiver->mid() || transceiver->stopped()) { |
| 5178 | continue; |
| 5179 | } |
| 5180 | size_t mline_index; |
| 5181 | if (!recycleable_mline_indices.empty()) { |
| 5182 | mline_index = recycleable_mline_indices.front(); |
| 5183 | recycleable_mline_indices.pop(); |
| 5184 | session_options->media_description_options[mline_index] = |
| 5185 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 5186 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5187 | } else { |
| 5188 | mline_index = session_options->media_description_options.size(); |
| 5189 | session_options->media_description_options.push_back( |
| 5190 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 5191 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5192 | } |
| 5193 | // See comment above for why CreateOffer changes the transceiver's state. |
| 5194 | transceiver->internal()->set_mline_index(mline_index); |
| 5195 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5196 | // Lastly, add a m-section if we have local data channels and an m section |
| 5197 | // does not already exist. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 5198 | if (!GetDataMid() && data_channel_controller_.HasDataChannels()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5199 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 5200 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5201 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | void PeerConnection::GetOptionsForAnswer( |
| 5205 | const RTCOfferAnswerOptions& offer_answer_options, |
| 5206 | cricket::MediaSessionOptions* session_options) { |
| 5207 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 5208 | |
| 5209 | if (IsUnifiedPlan()) { |
| 5210 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 5211 | } else { |
| 5212 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 5213 | } |
| 5214 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5215 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 5216 | // the RTP data channels would be successfully negotiated by default and the |
| 5217 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 5218 | // We want to leave RTP data channels broken, so people won't try to use them. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 5219 | if (data_channel_controller_.HasRtpDataChannels() || |
| 5220 | data_channel_type() != cricket::DCT_RTP) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5221 | session_options->data_channel_type = data_channel_type(); |
| 5222 | } |
| 5223 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5224 | // Apply ICE renomination flag. |
| 5225 | for (auto& options : session_options->media_description_options) { |
| 5226 | options.transport_options.enable_ice_renomination = |
| 5227 | configuration_.enable_ice_renomination; |
| 5228 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 5229 | |
| 5230 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 5231 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 5232 | session_options->pooled_ice_credentials = |
| 5233 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 5234 | RTC_FROM_HERE, |
| 5235 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 5236 | port_allocator_.get())); |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 5237 | |
| 5238 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 5239 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 5240 | for (auto& options : session_options->media_description_options) { |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame] | 5241 | absl::optional<cricket::OpaqueTransportParameters> params = |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 5242 | transport_controller_->GetTransportParameters(options.mid); |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame] | 5243 | if (!params) { |
| 5244 | continue; |
| 5245 | } |
| 5246 | options.transport_options.opaque_parameters = params; |
| 5247 | if ((use_datagram_transport_ && |
| 5248 | (options.type == cricket::MEDIA_TYPE_AUDIO || |
| 5249 | options.type == cricket::MEDIA_TYPE_VIDEO)) || |
| 5250 | (use_datagram_transport_for_data_channels_ && |
| 5251 | options.type == cricket::MEDIA_TYPE_DATA)) { |
| 5252 | options.alt_protocol = params->protocol; |
| 5253 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 5254 | } |
| 5255 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5256 | } |
| 5257 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5258 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 5259 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 5260 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5261 | // Figure out transceiver directional preferences. |
| 5262 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 5263 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 5264 | |
| 5265 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 5266 | // restricted by the direction in the offer. |
| 5267 | bool recv_audio = true; |
| 5268 | bool recv_video = true; |
| 5269 | |
| 5270 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5271 | if (offer_answer_options.offer_to_receive_audio != |
| 5272 | RTCOfferAnswerOptions::kUndefined) { |
| 5273 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 5274 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5275 | if (offer_answer_options.offer_to_receive_video != |
| 5276 | RTCOfferAnswerOptions::kUndefined) { |
| 5277 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5278 | } |
| 5279 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5280 | absl::optional<size_t> audio_index; |
| 5281 | absl::optional<size_t> video_index; |
| 5282 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 5283 | |
| 5284 | // Generate m= sections that match those in the offer. |
| 5285 | // Note that mediasession.cc will handle intersection our preferred |
| 5286 | // direction with the offered direction. |
| 5287 | GenerateMediaDescriptionOptions( |
| 5288 | remote_description(), |
| 5289 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 5290 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 5291 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5292 | |
| 5293 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 5294 | !audio_index ? nullptr |
| 5295 | : &session_options->media_description_options[*audio_index]; |
| 5296 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 5297 | !video_index ? nullptr |
| 5298 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5299 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 5300 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 5301 | audio_media_description_options, |
| 5302 | video_media_description_options, |
| 5303 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5304 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 5305 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5306 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 5307 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 5308 | cricket::MediaSessionOptions* session_options) { |
| 5309 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 5310 | // Answers) and 5.3.2 (Subsequent Answers). |
| 5311 | RTC_DCHECK(remote_description()); |
| 5312 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 5313 | for (const ContentInfo& content : |
| 5314 | remote_description()->description()->contents()) { |
| 5315 | cricket::MediaType media_type = content.media_description()->type(); |
| 5316 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5317 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5318 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 5319 | RTC_CHECK(transceiver); |
| 5320 | session_options->media_description_options.push_back( |
| 5321 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 5322 | } else { |
| 5323 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 5324 | // Reject all data sections if data channels are disabled. |
| 5325 | // Reject a data section if it has already been rejected. |
| 5326 | // Reject all data sections except for the first one. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 5327 | if (data_channel_type() == cricket::DCT_NONE || content.rejected || |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 5328 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5329 | session_options->media_description_options.push_back( |
| 5330 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 5331 | } else { |
| 5332 | session_options->media_description_options.push_back( |
| 5333 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 5334 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5335 | } |
| 5336 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 5337 | } |
| 5338 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5339 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 5340 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 5341 | RtpTransceiverDirection audio_direction, |
| 5342 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5343 | absl::optional<size_t>* audio_index, |
| 5344 | absl::optional<size_t>* video_index, |
| 5345 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 5346 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5347 | for (const cricket::ContentInfo& content : |
| 5348 | session_desc->description()->contents()) { |
| 5349 | if (IsAudioContent(&content)) { |
| 5350 | // If we already have an audio m= section, reject this extra one. |
| 5351 | if (*audio_index) { |
| 5352 | session_options->media_description_options.push_back( |
| 5353 | cricket::MediaDescriptionOptions( |
| 5354 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5355 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5356 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5357 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5358 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5359 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 5360 | content.name, audio_direction, |
| 5361 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5362 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5363 | } |
| 5364 | } else if (IsVideoContent(&content)) { |
| 5365 | // If we already have an video m= section, reject this extra one. |
| 5366 | if (*video_index) { |
| 5367 | session_options->media_description_options.push_back( |
| 5368 | cricket::MediaDescriptionOptions( |
| 5369 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5370 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5371 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5372 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5373 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 5374 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 5375 | content.name, video_direction, |
| 5376 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5377 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5378 | } |
| 5379 | } else { |
| 5380 | RTC_DCHECK(IsDataContent(&content)); |
| 5381 | // If we already have an data m= section, reject this extra one. |
| 5382 | if (*data_index) { |
| 5383 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5384 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5385 | } else { |
| 5386 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5387 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5388 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5389 | } |
| 5390 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 5391 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5392 | } |
| 5393 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5394 | cricket::MediaDescriptionOptions |
| 5395 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 5396 | const std::string& mid) const { |
| 5397 | // Direction for data sections is meaningless, but legacy endpoints might |
| 5398 | // expect sendrecv. |
| 5399 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 5400 | RtpTransceiverDirection::kSendRecv, |
| 5401 | /*stopped=*/false); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 5402 | AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(), |
| 5403 | &options); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5404 | return options; |
| 5405 | } |
| 5406 | |
| 5407 | cricket::MediaDescriptionOptions |
| 5408 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 5409 | const std::string& mid) const { |
| 5410 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 5411 | RtpTransceiverDirection::kInactive, |
| 5412 | /*stopped=*/true); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 5413 | AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(), |
| 5414 | &options); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5415 | return options; |
| 5416 | } |
| 5417 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5418 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 5419 | switch (data_channel_type()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5420 | case cricket::DCT_RTP: |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 5421 | if (!data_channel_controller_.rtp_data_channel()) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5422 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5423 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 5424 | return data_channel_controller_.rtp_data_channel()->content_name(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5425 | case cricket::DCT_SCTP: |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 5426 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
| 5427 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 5428 | return sctp_mid_s_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5429 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5430 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5434 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 5435 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 5436 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5437 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 5438 | } |
| 5439 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5440 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5441 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5442 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5443 | cricket::MediaType media_type, |
| 5444 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5445 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5446 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5447 | std::vector<RtpSenderInfo>* current_senders = |
| 5448 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5449 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5450 | // Find removed senders. I.e., senders where the sender id or ssrc don't match |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5451 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5452 | for (auto sender_it = current_senders->begin(); |
| 5453 | sender_it != current_senders->end(); |
| 5454 | /* incremented manually */) { |
| 5455 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5456 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5457 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5458 | std::string params_stream_id; |
| 5459 | if (params) { |
| 5460 | params_stream_id = |
| 5461 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 5462 | : kDefaultStreamId); |
| 5463 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5464 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5465 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5466 | // If this is a default track, and we still need it, don't remove it. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5467 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5468 | sender_exists) { |
| 5469 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5470 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5471 | OnRemoteSenderRemoved(info, media_type); |
| 5472 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5473 | } |
| 5474 | } |
| 5475 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5476 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5477 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 5478 | if (!params.has_ssrcs()) { |
| 5479 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 5480 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 5481 | // create a default. |
| 5482 | default_sender_needed = true; |
| 5483 | break; |
| 5484 | } |
| 5485 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5486 | // |params.id| is the sender id and the stream id uses the first of |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5487 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 5488 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 5489 | // not supported in Plan B, we just take the first here and create the |
| 5490 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5491 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5492 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 5493 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5494 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5495 | uint32_t ssrc = params.first_ssrc(); |
| 5496 | |
| 5497 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5498 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5499 | if (!stream) { |
| 5500 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5501 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5502 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5503 | remote_streams_->AddStream(stream); |
| 5504 | new_streams->AddStream(stream); |
| 5505 | } |
| 5506 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5507 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5508 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5509 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5510 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5511 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5512 | } |
| 5513 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5514 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5515 | // Add default sender if necessary. |
| 5516 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5517 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5518 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5519 | if (!default_stream) { |
| 5520 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5521 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5522 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5523 | remote_streams_->AddStream(default_stream); |
| 5524 | new_streams->AddStream(default_stream); |
| 5525 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5526 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5527 | ? kDefaultAudioSenderId |
| 5528 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5529 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5530 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5531 | if (!default_sender_info) { |
| 5532 | current_senders->push_back( |
Ruslan Burakov | 7ea4605 | 2019-02-16 02:07:05 +0100 | [diff] [blame] | 5533 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5534 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5535 | } |
| 5536 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5537 | } |
| 5538 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5539 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 5540 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 5541 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 5542 | << " receiver for track_id=" << sender_info.sender_id |
| 5543 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5544 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 5545 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5546 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5547 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5548 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5549 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5550 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 5551 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5552 | } |
| 5553 | } |
| 5554 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5555 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 5556 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5557 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 5558 | << " receiver for track_id=" << sender_info.sender_id |
| 5559 | << " and stream_id=" << sender_info.stream_id; |
| 5560 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5561 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5562 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5563 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5564 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5565 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 5566 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5567 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5568 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5569 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5570 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5571 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5572 | } |
| 5573 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5574 | // Stopping or destroying a VideoRtpReceiver will end the |
| 5575 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5576 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5577 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5578 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5579 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5580 | // There's no guarantee the track is still available, e.g. the track may |
| 5581 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5582 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5583 | } |
| 5584 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 5585 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5586 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5587 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5588 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5589 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 5593 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 5594 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 5595 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 5596 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 5597 | streams_to_remove.push_back(stream); |
| 5598 | } |
| 5599 | } |
| 5600 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 5601 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5602 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5603 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5607 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5608 | const std::vector<cricket::StreamParams>& streams, |
| 5609 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5610 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5611 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5612 | // Find removed tracks. I.e., tracks where the track id, stream id or ssrc |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5613 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5614 | for (auto sender_it = current_senders->begin(); |
| 5615 | sender_it != current_senders->end(); |
| 5616 | /* incremented manually */) { |
| 5617 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5618 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5619 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 5620 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5621 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5622 | OnLocalSenderRemoved(info, media_type); |
| 5623 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5624 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5625 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5626 | } |
| 5627 | } |
| 5628 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5629 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5630 | for (const cricket::StreamParams& params : streams) { |
| 5631 | // The sync_label is the MediaStream label and the |stream.id| is the |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5632 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5633 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5634 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5635 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5636 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5637 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5638 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5639 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5640 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5641 | } |
| 5642 | } |
| 5643 | } |
| 5644 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5645 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 5646 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5647 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5648 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5649 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5650 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 5651 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5652 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5653 | return; |
| 5654 | } |
| 5655 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5656 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5657 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5658 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5659 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5660 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5661 | |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 5662 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5663 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5664 | } |
| 5665 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5666 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 5667 | cricket::MediaType media_type) { |
| 5668 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5669 | if (!sender) { |
| 5670 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5671 | // SessionDescriptions has been renegotiated. |
| 5672 | return; |
| 5673 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5674 | |
| 5675 | // A sender has been removed from the SessionDescription but it's still |
| 5676 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 5677 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 5678 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5679 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5680 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5681 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5682 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5683 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5684 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5685 | } |
| 5686 | |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 5687 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
| 5688 | // Since data_channel_controller doesn't do signals, this |
| 5689 | // signal is relayed here. |
| 5690 | data_channel_controller_.OnSctpDataChannelClosed(channel); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5691 | } |
| 5692 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5693 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5694 | PeerConnection::GetAudioTransceiver() const { |
| 5695 | // This method only works with Plan B SDP, where there is a single |
| 5696 | // audio/video transceiver. |
| 5697 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5698 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5699 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5700 | return transceiver; |
| 5701 | } |
| 5702 | } |
| 5703 | RTC_NOTREACHED(); |
| 5704 | return nullptr; |
| 5705 | } |
| 5706 | |
| 5707 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5708 | PeerConnection::GetVideoTransceiver() const { |
| 5709 | // This method only works with Plan B SDP, where there is a single |
| 5710 | // audio/video transceiver. |
| 5711 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5712 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5713 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5714 | return transceiver; |
| 5715 | } |
| 5716 | } |
| 5717 | RTC_NOTREACHED(); |
| 5718 | return nullptr; |
| 5719 | } |
| 5720 | |
| 5721 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 5722 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5723 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5724 | switch (type) { |
| 5725 | case cricket::MEDIA_TYPE_AUDIO: |
| 5726 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 5727 | case cricket::MEDIA_TYPE_VIDEO: |
| 5728 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 5729 | case cricket::MEDIA_TYPE_DATA: |
| 5730 | return false; |
| 5731 | } |
| 5732 | RTC_NOTREACHED(); |
| 5733 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5734 | } |
| 5735 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5736 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5737 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5738 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5739 | for (auto sender : transceiver->internal()->senders()) { |
| 5740 | if (sender->track() == track) { |
| 5741 | return sender; |
| 5742 | } |
| 5743 | } |
| 5744 | } |
| 5745 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5746 | } |
| 5747 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5748 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5749 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5750 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5751 | for (auto sender : transceiver->internal()->senders()) { |
| 5752 | if (sender->id() == sender_id) { |
| 5753 | return sender; |
| 5754 | } |
| 5755 | } |
| 5756 | } |
| 5757 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5758 | } |
| 5759 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5760 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 5761 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5762 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5763 | for (auto receiver : transceiver->internal()->receivers()) { |
| 5764 | if (receiver->id() == receiver_id) { |
| 5765 | return receiver; |
| 5766 | } |
| 5767 | } |
| 5768 | } |
| 5769 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5770 | } |
| 5771 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5772 | std::vector<PeerConnection::RtpSenderInfo>* |
| 5773 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 5774 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5775 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 5776 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5777 | ? &remote_audio_sender_infos_ |
| 5778 | : &remote_video_sender_infos_; |
| 5779 | } |
| 5780 | |
| 5781 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5782 | cricket::MediaType media_type) { |
| 5783 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5784 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5785 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 5786 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5787 | } |
| 5788 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5789 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 5790 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5791 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5792 | const std::string sender_id) const { |
| 5793 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5794 | if (sender_info.stream_id == stream_id && |
| 5795 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5796 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5797 | } |
| 5798 | } |
| 5799 | return nullptr; |
| 5800 | } |
| 5801 | |
| 5802 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 5803 | return data_channel_controller_.FindDataChannelBySid(sid); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5804 | } |
| 5805 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5806 | PeerConnection::InitializePortAllocatorResult |
| 5807 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5808 | const cricket::ServerAddresses& stun_servers, |
| 5809 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5810 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5811 | RTC_DCHECK_RUN_ON(network_thread()); |
| 5812 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 5813 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5814 | // To handle both internal and externally created port allocator, we will |
| 5815 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5816 | int port_allocator_flags = port_allocator_->flags(); |
| 5817 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5818 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5819 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5820 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5821 | // by experiment. |
| 5822 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5823 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Mirko Bonadei | 57cabed | 2020-04-01 12:03:11 +0200 | [diff] [blame] | 5824 | } else if (absl::StartsWith( |
| 5825 | webrtc::field_trial::FindFullName("WebRTC-IPv6Default"), |
| 5826 | "Disabled")) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5827 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5828 | } |
| 5829 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5830 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5831 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5832 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5833 | } |
| 5834 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5835 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5836 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5837 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5838 | } |
| 5839 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5840 | if (configuration.candidate_network_policy == |
| 5841 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5842 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5843 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5844 | } |
| 5845 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5846 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5847 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5848 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5849 | } |
| 5850 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5851 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5852 | // No step delay is used while allocating ports. |
| 5853 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5854 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5855 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 5856 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5857 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5858 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5859 | for (auto& turn_server : turn_servers_copy) { |
| 5860 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 5861 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5862 | // Call this last since it may create pooled allocator sessions using the |
| 5863 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5864 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5865 | stun_servers, std::move(turn_servers_copy), |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 5866 | configuration.ice_candidate_pool_size, |
| 5867 | configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5868 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5869 | |
| 5870 | InitializePortAllocatorResult res; |
| 5871 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 5872 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5873 | } |
| 5874 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 5875 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5876 | const cricket::ServerAddresses& stun_servers, |
| 5877 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5878 | IceTransportsType type, |
| 5879 | int candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 5880 | PortPrunePolicy turn_port_prune_policy, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5881 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 5882 | absl::optional<int> stun_candidate_keepalive_interval, |
| 5883 | bool have_local_description) { |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5884 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5885 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5886 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5887 | // size is not allowed, and changing the set of ICE servers will not result |
| 5888 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 5889 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5890 | port_allocator_->FreezeCandidatePool(); |
| 5891 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5892 | // Add the custom tls turn servers if they exist. |
| 5893 | auto turn_servers_copy = turn_servers; |
| 5894 | for (auto& turn_server : turn_servers_copy) { |
| 5895 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5896 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5897 | // Call this last since it may create pooled allocator sessions using the |
| 5898 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 5899 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5900 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 5901 | turn_port_prune_policy, turn_customizer, |
| 5902 | stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5903 | } |
| 5904 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 5905 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5906 | return factory_->channel_manager(); |
| 5907 | } |
| 5908 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 5909 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5910 | std::unique_ptr<RtcEventLogOutput> output, |
| 5911 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5912 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5913 | if (!event_log_) { |
| 5914 | return false; |
| 5915 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5916 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5920 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5921 | if (event_log_) { |
| 5922 | event_log_->StopLogging(); |
| 5923 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5924 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 5925 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5926 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5927 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5928 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5929 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5930 | if (channel && channel->content_name() == content_name) { |
| 5931 | return channel; |
| 5932 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5933 | } |
| 5934 | if (rtp_data_channel() && |
| 5935 | rtp_data_channel()->content_name() == content_name) { |
| 5936 | return rtp_data_channel(); |
| 5937 | } |
| 5938 | return nullptr; |
| 5939 | } |
| 5940 | |
| 5941 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5942 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5943 | if (!local_description() || !remote_description()) { |
Harald Alvestrand | 977b265 | 2019-12-12 13:40:50 +0100 | [diff] [blame] | 5944 | RTC_LOG(LS_VERBOSE) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5945 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5946 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5947 | return false; |
| 5948 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 5949 | if (!data_channel_controller_.data_channel_transport()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5950 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5951 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5952 | return false; |
| 5953 | } |
| 5954 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5955 | absl::optional<rtc::SSLRole> dtls_role; |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 5956 | if (sctp_mid_s_) { |
| 5957 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_s_); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5958 | if (!dtls_role && is_caller_.has_value()) { |
| 5959 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5960 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5961 | *role = *dtls_role; |
| 5962 | return true; |
| 5963 | } |
| 5964 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5965 | } |
| 5966 | |
| 5967 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5968 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 5969 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5970 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5971 | RTC_LOG(LS_INFO) |
| 5972 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5973 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5974 | return false; |
| 5975 | } |
| 5976 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5977 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5978 | if (dtls_role) { |
| 5979 | *role = *dtls_role; |
| 5980 | return true; |
| 5981 | } |
| 5982 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5983 | } |
| 5984 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5985 | void PeerConnection::SetSessionError(SessionError error, |
| 5986 | const std::string& error_desc) { |
| 5987 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5988 | if (error != session_error_) { |
| 5989 | session_error_ = error; |
| 5990 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5991 | } |
| 5992 | } |
| 5993 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5994 | RTCError PeerConnection::UpdateSessionState( |
| 5995 | SdpType type, |
| 5996 | cricket::ContentSource source, |
| 5997 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5998 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5999 | |
| 6000 | // If there's already a pending error then no state transition should happen. |
| 6001 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6002 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6003 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6004 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6005 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6006 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6007 | } |
| 6008 | |
| 6009 | // Update the signaling state according to the specified state machine (see |
| 6010 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6011 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6012 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 6013 | ? PeerConnectionInterface::kHaveLocalOffer |
| 6014 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6015 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6016 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 6017 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 6018 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6019 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6020 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6021 | ChangeSignalingState(PeerConnectionInterface::kStable); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 6022 | transceiver_stable_states_by_transceivers_.clear(); |
Eldar Rello | d9ebe01 | 2020-03-18 20:41:45 +0200 | [diff] [blame] | 6023 | have_pending_rtp_data_channel_ = false; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6024 | } |
| 6025 | |
| 6026 | // Update internal objects according to the session description's media |
| 6027 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6028 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6029 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 6030 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 6031 | } |
| 6032 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6033 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6034 | } |
| 6035 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6036 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6037 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6038 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6039 | const SessionDescriptionInterface* sdesc = |
| 6040 | (source == cricket::CS_LOCAL ? local_description() |
| 6041 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6042 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6043 | |
| 6044 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6045 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6046 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6047 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6048 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6049 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6050 | continue; |
| 6051 | } |
| 6052 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6053 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6054 | if (!content_desc) { |
| 6055 | continue; |
| 6056 | } |
| 6057 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 6058 | bool success = (source == cricket::CS_LOCAL) |
| 6059 | ? channel->SetLocalContent(content_desc, type, &error) |
| 6060 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6061 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 6062 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6063 | } |
| 6064 | } |
| 6065 | |
| 6066 | // If using the RtpDataChannel, push down the new SDP section for it too. |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6067 | if (data_channel_controller_.rtp_data_channel()) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6068 | const ContentInfo* data_content = |
| 6069 | cricket::GetFirstDataContent(sdesc->description()); |
| 6070 | if (data_content && !data_content->rejected) { |
| 6071 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6072 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6073 | if (data_desc) { |
| 6074 | std::string error; |
| 6075 | bool success = |
| 6076 | (source == cricket::CS_LOCAL) |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6077 | ? data_channel_controller_.rtp_data_channel()->SetLocalContent( |
| 6078 | data_desc, type, &error) |
| 6079 | : data_channel_controller_.rtp_data_channel()->SetRemoteContent( |
| 6080 | data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6081 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 6082 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6083 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6084 | } |
| 6085 | } |
| 6086 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6087 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6088 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 6089 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6090 | if (sctp_mid_s_ && local_description() && remote_description()) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6091 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6092 | transport_controller_->GetSctpTransport(*sctp_mid_s_); |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 6093 | auto local_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 6094 | local_description()->description()); |
| 6095 | auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 6096 | remote_description()->description()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6097 | if (sctp_transport && local_sctp_description && remote_sctp_description) { |
Harald Alvestrand | 8d3d6cf | 2019-05-16 11:49:17 +0200 | [diff] [blame] | 6098 | int max_message_size; |
| 6099 | // A remote max message size of zero means "any size supported". |
| 6100 | // We configure the connection with our own max message size. |
| 6101 | if (remote_sctp_description->max_message_size() == 0) { |
| 6102 | max_message_size = local_sctp_description->max_message_size(); |
| 6103 | } else { |
| 6104 | max_message_size = |
| 6105 | std::min(local_sctp_description->max_message_size(), |
| 6106 | remote_sctp_description->max_message_size()); |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 6107 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6108 | sctp_transport->Start(local_sctp_description->port(), |
| 6109 | remote_sctp_description->port(), max_message_size); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6110 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6111 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6112 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6113 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6114 | } |
| 6115 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6116 | RTCError PeerConnection::PushdownTransportDescription( |
| 6117 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6118 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6119 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6120 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6121 | if (source == cricket::CS_LOCAL) { |
| 6122 | const SessionDescriptionInterface* sdesc = local_description(); |
| 6123 | RTC_DCHECK(sdesc); |
| 6124 | return transport_controller_->SetLocalDescription(type, |
| 6125 | sdesc->description()); |
| 6126 | } else { |
| 6127 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 6128 | RTC_DCHECK(sdesc); |
| 6129 | return transport_controller_->SetRemoteDescription(type, |
| 6130 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6131 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6132 | } |
| 6133 | |
| 6134 | bool PeerConnection::GetTransportDescription( |
| 6135 | const SessionDescription* description, |
| 6136 | const std::string& content_name, |
| 6137 | cricket::TransportDescription* tdesc) { |
| 6138 | if (!description || !tdesc) { |
| 6139 | return false; |
| 6140 | } |
| 6141 | const TransportInfo* transport_info = |
| 6142 | description->GetTransportInfoByName(content_name); |
| 6143 | if (!transport_info) { |
| 6144 | return false; |
| 6145 | } |
| 6146 | *tdesc = transport_info->description; |
| 6147 | return true; |
| 6148 | } |
| 6149 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6150 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 6151 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 6152 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6153 | switch (config.continual_gathering_policy) { |
| 6154 | case PeerConnectionInterface::GATHER_ONCE: |
| 6155 | gathering_policy = cricket::GATHER_ONCE; |
| 6156 | break; |
| 6157 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 6158 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 6159 | break; |
| 6160 | default: |
| 6161 | RTC_NOTREACHED(); |
| 6162 | gathering_policy = cricket::GATHER_ONCE; |
| 6163 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 6164 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6165 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 6166 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 6167 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6168 | ice_config.prioritize_most_likely_candidate_pairs = |
| 6169 | config.prioritize_most_likely_ice_candidate_pairs; |
| 6170 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 6171 | RTCConfigurationToIceConfigOptionalInt( |
| 6172 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6173 | ice_config.continual_gathering_policy = gathering_policy; |
| 6174 | ice_config.presume_writable_when_fully_relayed = |
| 6175 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 6176 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 6177 | config.surface_ice_candidates_on_ice_transport_type_changed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 6178 | ice_config.ice_check_interval_strong_connectivity = |
| 6179 | config.ice_check_interval_strong_connectivity; |
| 6180 | ice_config.ice_check_interval_weak_connectivity = |
| 6181 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6182 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-29 23:08:51 -0800 | [diff] [blame] | 6183 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 6184 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 6185 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 6186 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 6187 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6188 | return ice_config; |
| 6189 | } |
| 6190 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6191 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 6192 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6193 | if (sctp_mid_s_ && transport_controller_) { |
| 6194 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_s_); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6195 | if (dtls_transport) { |
| 6196 | return dtls_transport->transport_name(); |
| 6197 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6198 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6199 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6200 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6201 | } |
| 6202 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 6203 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 6204 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 6205 | network_thread()->Invoke<void>( |
| 6206 | RTC_FROM_HERE, |
| 6207 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 6208 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 6209 | return candidate_states_list; |
| 6210 | } |
| 6211 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6212 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 6213 | const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 6214 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6215 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6216 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6217 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6218 | if (channel) { |
| 6219 | transport_names_by_mid[channel->content_name()] = |
| 6220 | channel->transport_name(); |
| 6221 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6222 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6223 | if (data_channel_controller_.rtp_data_channel()) { |
| 6224 | transport_names_by_mid[data_channel_controller_.rtp_data_channel() |
| 6225 | ->content_name()] = |
| 6226 | data_channel_controller_.rtp_data_channel()->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6227 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6228 | if (data_channel_controller_.data_channel_transport()) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6229 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6230 | RTC_DCHECK(transport_name); |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6231 | transport_names_by_mid[*sctp_mid_s_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6232 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6233 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6234 | } |
| 6235 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6236 | std::map<std::string, cricket::TransportStats> |
| 6237 | PeerConnection::GetTransportStatsByNames( |
| 6238 | const std::set<std::string>& transport_names) { |
| 6239 | if (!network_thread()->IsCurrent()) { |
| 6240 | return network_thread() |
| 6241 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 6242 | RTC_FROM_HERE, |
| 6243 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6244 | } |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 6245 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6246 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 6247 | for (const std::string& transport_name : transport_names) { |
| 6248 | cricket::TransportStats transport_stats; |
| 6249 | bool success = |
| 6250 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 6251 | if (success) { |
| 6252 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 6253 | } else { |
| 6254 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 6255 | << transport_name; |
| 6256 | } |
| 6257 | } |
| 6258 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6259 | } |
| 6260 | |
| 6261 | bool PeerConnection::GetLocalCertificate( |
| 6262 | const std::string& transport_name, |
| 6263 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6264 | if (!certificate) { |
| 6265 | return false; |
| 6266 | } |
| 6267 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 6268 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6269 | } |
| 6270 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 6271 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6272 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 6273 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6274 | } |
| 6275 | |
| 6276 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 6277 | return data_channel_controller_.data_channel_type(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6278 | } |
| 6279 | |
| 6280 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 15:36:53 +0200 | [diff] [blame] | 6281 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6282 | return pending_ice_restarts_.find(content_name) != |
| 6283 | pending_ice_restarts_.end(); |
| 6284 | } |
| 6285 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6286 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 6287 | return transport_controller_->NeedsIceRestart(content_name); |
| 6288 | } |
| 6289 | |
| 6290 | void PeerConnection::OnCertificateReady( |
| 6291 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 6292 | transport_controller_->SetLocalCertificate(certificate); |
| 6293 | } |
| 6294 | |
| 6295 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6296 | SetSessionError(SessionError::kTransport, |
| 6297 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6298 | } |
| 6299 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 6300 | void PeerConnection::OnTransportControllerConnectionState( |
| 6301 | cricket::IceConnectionState state) { |
| 6302 | switch (state) { |
| 6303 | case cricket::kIceConnectionConnecting: |
| 6304 | // If the current state is Connected or Completed, then there were |
| 6305 | // writable channels but now there are not, so the next state must |
| 6306 | // be Disconnected. |
| 6307 | // kIceConnectionConnecting is currently used as the default, |
| 6308 | // un-connected state by the TransportController, so its only use is |
| 6309 | // detecting disconnections. |
| 6310 | if (ice_connection_state_ == |
| 6311 | PeerConnectionInterface::kIceConnectionConnected || |
| 6312 | ice_connection_state_ == |
| 6313 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 6314 | SetIceConnectionState( |
| 6315 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 6316 | } |
| 6317 | break; |
| 6318 | case cricket::kIceConnectionFailed: |
| 6319 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 6320 | break; |
| 6321 | case cricket::kIceConnectionConnected: |
| 6322 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 6323 | "all transports are writable."; |
| 6324 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6325 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6326 | break; |
| 6327 | case cricket::kIceConnectionCompleted: |
| 6328 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 6329 | "all transports are complete."; |
| 6330 | if (ice_connection_state_ != |
| 6331 | PeerConnectionInterface::kIceConnectionConnected) { |
| 6332 | // If jumping directly from "checking" to "connected", |
| 6333 | // signal "connected" first. |
| 6334 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6335 | } |
| 6336 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 6337 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6338 | ReportTransportStats(); |
| 6339 | break; |
| 6340 | default: |
| 6341 | RTC_NOTREACHED(); |
| 6342 | } |
| 6343 | } |
| 6344 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6345 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 6346 | const std::string& transport_name, |
| 6347 | const cricket::Candidates& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6348 | int sdp_mline_index; |
| 6349 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6350 | RTC_LOG(LS_ERROR) |
| 6351 | << "OnTransportControllerCandidatesGathered: content name " |
| 6352 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6353 | return; |
| 6354 | } |
| 6355 | |
| 6356 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 6357 | citer != candidates.end(); ++citer) { |
| 6358 | // Use transport_name as the candidate media id. |
| 6359 | std::unique_ptr<JsepIceCandidate> candidate( |
| 6360 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 6361 | if (local_description()) { |
| 6362 | mutable_local_description()->AddCandidate(candidate.get()); |
| 6363 | } |
| 6364 | OnIceCandidate(std::move(candidate)); |
| 6365 | } |
| 6366 | } |
| 6367 | |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 6368 | void PeerConnection::OnTransportControllerCandidateError( |
| 6369 | const cricket::IceCandidateErrorEvent& event) { |
Eldar Rello | 0095d37 | 2019-12-02 22:22:07 +0200 | [diff] [blame] | 6370 | OnIceCandidateError(event.address, event.port, event.url, event.error_code, |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 6371 | event.error_text); |
| 6372 | } |
| 6373 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6374 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 6375 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6376 | // Sanity check. |
| 6377 | for (const cricket::Candidate& candidate : candidates) { |
| 6378 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6379 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6380 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6381 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6382 | return; |
| 6383 | } |
| 6384 | } |
| 6385 | |
| 6386 | if (local_description()) { |
| 6387 | mutable_local_description()->RemoveCandidates(candidates); |
| 6388 | } |
| 6389 | OnIceCandidatesRemoved(candidates); |
| 6390 | } |
| 6391 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 6392 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 6393 | const cricket::CandidatePairChangeEvent& event) { |
| 6394 | OnSelectedCandidatePairChanged(event); |
| 6395 | } |
| 6396 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6397 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 6398 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6399 | RTC_HISTOGRAM_ENUMERATION( |
| 6400 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 6401 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6402 | } |
| 6403 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6404 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6405 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6406 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6407 | if (channel && !channel->enabled()) { |
| 6408 | channel->Enable(true); |
| 6409 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6410 | } |
| 6411 | |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6412 | if (data_channel_controller_.rtp_data_channel() && |
| 6413 | !data_channel_controller_.rtp_data_channel()->enabled()) { |
| 6414 | data_channel_controller_.rtp_data_channel()->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6415 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6416 | } |
| 6417 | |
| 6418 | // Returns the media index for a local ice candidate given the content name. |
| 6419 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 6420 | const std::string& content_name, |
| 6421 | int* sdp_mline_index) { |
| 6422 | if (!local_description() || !sdp_mline_index) { |
| 6423 | return false; |
| 6424 | } |
| 6425 | |
| 6426 | bool content_found = false; |
| 6427 | const ContentInfos& contents = local_description()->description()->contents(); |
| 6428 | for (size_t index = 0; index < contents.size(); ++index) { |
| 6429 | if (contents[index].name == content_name) { |
| 6430 | *sdp_mline_index = static_cast<int>(index); |
| 6431 | content_found = true; |
| 6432 | break; |
| 6433 | } |
| 6434 | } |
| 6435 | return content_found; |
| 6436 | } |
| 6437 | |
| 6438 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 6439 | const SessionDescriptionInterface* remote_desc) { |
| 6440 | if (!remote_desc) { |
| 6441 | return true; |
| 6442 | } |
| 6443 | bool ret = true; |
| 6444 | |
| 6445 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 6446 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 6447 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 6448 | const IceCandidateInterface* candidate = candidates->at(n); |
| 6449 | bool valid = false; |
| 6450 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 6451 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6452 | RTC_LOG(LS_INFO) |
| 6453 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6454 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6455 | } |
| 6456 | continue; |
| 6457 | } |
| 6458 | ret = UseCandidate(candidate); |
| 6459 | if (!ret) { |
| 6460 | break; |
| 6461 | } |
| 6462 | } |
| 6463 | } |
| 6464 | return ret; |
| 6465 | } |
| 6466 | |
| 6467 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6468 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 6469 | FindContentInfo(remote_description(), candidate); |
| 6470 | if (!result.ok()) { |
| 6471 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. " |
| 6472 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6473 | return false; |
| 6474 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6475 | std::vector<cricket::Candidate> candidates; |
| 6476 | candidates.push_back(candidate->candidate()); |
| 6477 | // Invoking BaseSession method to handle remote candidates. |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6478 | RTCError error = transport_controller_->AddRemoteCandidates( |
| 6479 | result.value()->name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6480 | if (error.ok()) { |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6481 | ReportRemoteIceCandidateAdded(candidate->candidate()); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6482 | // Candidates successfully submitted for checking. |
| 6483 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6484 | ice_connection_state_ == |
| 6485 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6486 | // If state is New, then the session has just gotten its first remote ICE |
| 6487 | // candidates, so go to Checking. |
| 6488 | // If state is Disconnected, the session is re-using old candidates or |
| 6489 | // receiving additional ones, so go to Checking. |
| 6490 | // If state is Connected, stay Connected. |
| 6491 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6492 | // newly added transport, then the state actually _should_ move to |
| 6493 | // checking. Add a way to distinguish that case. |
| 6494 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6495 | } |
| 6496 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 6497 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6498 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6499 | } |
| 6500 | return true; |
| 6501 | } |
| 6502 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6503 | RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo( |
| 6504 | const SessionDescriptionInterface* description, |
| 6505 | const IceCandidateInterface* candidate) { |
| 6506 | if (candidate->sdp_mline_index() >= 0) { |
| 6507 | size_t mediacontent_index = |
| 6508 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 6509 | size_t content_size = description->description()->contents().size(); |
| 6510 | if (mediacontent_index < content_size) { |
| 6511 | return &description->description()->contents()[mediacontent_index]; |
| 6512 | } else { |
| 6513 | return RTCError(RTCErrorType::INVALID_RANGE, |
| 6514 | "Media line index (" + |
| 6515 | rtc::ToString(candidate->sdp_mline_index()) + |
| 6516 | ") out of range (number of mlines: " + |
| 6517 | rtc::ToString(content_size) + ")."); |
| 6518 | } |
| 6519 | } else if (!candidate->sdp_mid().empty()) { |
| 6520 | auto& contents = description->description()->contents(); |
| 6521 | auto it = absl::c_find_if( |
| 6522 | contents, [candidate](const cricket::ContentInfo& content_info) { |
| 6523 | return content_info.mid() == candidate->sdp_mid(); |
| 6524 | }); |
| 6525 | if (it == contents.end()) { |
| 6526 | return RTCError( |
| 6527 | RTCErrorType::INVALID_PARAMETER, |
| 6528 | "Mid " + candidate->sdp_mid() + |
| 6529 | " specified but no media section with that mid found."); |
| 6530 | } else { |
| 6531 | return &*it; |
| 6532 | } |
| 6533 | } |
| 6534 | |
| 6535 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 6536 | "Neither sdp_mline_index nor sdp_mid specified."); |
| 6537 | } |
| 6538 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6539 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6540 | // Destroy video channel first since it may have a pointer to the |
| 6541 | // voice channel. |
| 6542 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6543 | if (!video_info || video_info->rejected) { |
| 6544 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6545 | } |
| 6546 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6547 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6548 | if (!audio_info || audio_info->rejected) { |
| 6549 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6553 | if (!data_info || data_info->rejected) { |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 6554 | DestroyDataChannelTransport(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6555 | } |
| 6556 | } |
| 6557 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6558 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6559 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6560 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6561 | if (configuration_.bundle_policy == |
| 6562 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6563 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6564 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6565 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6566 | "max-bundle configured but session description " |
| 6567 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6568 | } |
| 6569 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 13:47:42 +0100 | [diff] [blame] | 6570 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6571 | } |
| 6572 | |
| 6573 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6574 | // Creating the media channels. Transports should already have been created |
| 6575 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6576 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6577 | if (voice && !voice->rejected && |
| 6578 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6579 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6580 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6581 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6582 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6583 | } |
| 6584 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 6585 | } |
| 6586 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6587 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6588 | if (video && !video->rejected && |
| 6589 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6590 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6591 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6592 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6593 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6594 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6595 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6596 | } |
| 6597 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6598 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 6599 | if (data_channel_type() != cricket::DCT_NONE && data && !data->rejected && |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6600 | !data_channel_controller_.rtp_data_channel() && |
| 6601 | !data_channel_controller_.data_channel_transport()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6602 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6603 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6604 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6605 | } |
| 6606 | } |
| 6607 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6608 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6609 | } |
| 6610 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6611 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6612 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6613 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6614 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6615 | MediaTransportConfig media_transport_config = |
| 6616 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6617 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6618 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6619 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6620 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 6621 | GetCryptoOptions(), &ssrc_generator_, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6622 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6623 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6624 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6625 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6626 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6627 | voice_channel->SignalSentPacket.connect(this, |
| 6628 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6629 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6630 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6631 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6632 | } |
| 6633 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6634 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6635 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6636 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6637 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6638 | MediaTransportConfig media_transport_config = |
| 6639 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6640 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6641 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6642 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6643 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 6644 | GetCryptoOptions(), &ssrc_generator_, video_options_, |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6645 | video_bitrate_allocator_factory_.get()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6646 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6647 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6648 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6649 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6650 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6651 | video_channel->SignalSentPacket.connect(this, |
| 6652 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6653 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6654 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6655 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6656 | } |
| 6657 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6658 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 6659 | switch (data_channel_type()) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6660 | case cricket::DCT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6661 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6662 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6663 | if (network_thread()->Invoke<bool>( |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6664 | RTC_FROM_HERE, |
| 6665 | rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this, |
| 6666 | mid))) { |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6667 | sctp_mid_s_ = mid; |
| 6668 | } else { |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6669 | return false; |
| 6670 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6671 | |
| 6672 | // All non-RTP data channels must initialize |sctp_data_channels_|. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 6673 | for (const auto& channel : |
| 6674 | *data_channel_controller_.sctp_data_channels()) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6675 | channel->OnTransportChannelCreated(); |
| 6676 | } |
| 6677 | return true; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6678 | case cricket::DCT_RTP: |
| 6679 | default: |
| 6680 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6681 | data_channel_controller_.set_rtp_data_channel( |
| 6682 | channel_manager()->CreateRtpDataChannel( |
| 6683 | configuration_.media_config, rtp_transport, signaling_thread(), |
| 6684 | mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_)); |
| 6685 | if (!data_channel_controller_.rtp_data_channel()) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6686 | return false; |
| 6687 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6688 | data_channel_controller_.rtp_data_channel() |
| 6689 | ->SignalDtlsSrtpSetupFailure.connect( |
| 6690 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6691 | data_channel_controller_.rtp_data_channel()->SignalSentPacket.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6692 | this, &PeerConnection::OnSentPacket_w); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6693 | data_channel_controller_.rtp_data_channel()->SetRtpTransport( |
| 6694 | rtp_transport); |
Eldar Rello | d9ebe01 | 2020-03-18 20:41:45 +0200 | [diff] [blame] | 6695 | have_pending_rtp_data_channel_ = true; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6696 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6697 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6698 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | Call::Stats PeerConnection::GetCallStats() { |
| 6702 | if (!worker_thread()->IsCurrent()) { |
| 6703 | return worker_thread()->Invoke<Call::Stats>( |
| 6704 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6705 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 6706 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6707 | if (call_) { |
| 6708 | return call_->GetStats(); |
| 6709 | } else { |
| 6710 | return Call::Stats(); |
| 6711 | } |
| 6712 | } |
| 6713 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6714 | bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6715 | DataChannelTransportInterface* transport = |
| 6716 | transport_controller_->GetDataChannelTransport(mid); |
| 6717 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 6718 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6719 | << "Data channel transport is not available for data channels, mid=" |
| 6720 | << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6721 | return false; |
| 6722 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6723 | RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6724 | |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6725 | data_channel_controller_.set_data_channel_transport(transport); |
| 6726 | data_channel_controller_.SetupDataChannelTransport_n(); |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6727 | sctp_mid_n_ = mid; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6728 | |
| 6729 | // Note: setting the data sink and checking initial state must be done last, |
| 6730 | // after setting up the data channel. Setting the data sink may trigger |
| 6731 | // callbacks to PeerConnection which require the transport to be completely |
| 6732 | // set up (eg. OnReadyToSend()). |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6733 | transport->SetDataSink(&data_channel_controller_); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6734 | return true; |
| 6735 | } |
| 6736 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6737 | void PeerConnection::TeardownDataChannelTransport_n() { |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6738 | if (!sctp_mid_n_ && !data_channel_controller_.data_channel_transport()) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6739 | return; |
| 6740 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6741 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6742 | << *sctp_mid_n_; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6743 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6744 | // |sctp_mid_| may still be active through an SCTP transport. If not, unset |
| 6745 | // it. |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 6746 | sctp_mid_n_.reset(); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 6747 | data_channel_controller_.TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6748 | } |
| 6749 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6750 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6751 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6752 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6753 | if (!bundle_enabled) |
| 6754 | return true; |
| 6755 | |
| 6756 | const cricket::ContentGroup* bundle_group = |
| 6757 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6758 | RTC_DCHECK(bundle_group != NULL); |
| 6759 | |
| 6760 | const cricket::ContentInfos& contents = desc->contents(); |
| 6761 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6762 | citer != contents.end(); ++citer) { |
| 6763 | const cricket::ContentInfo* content = (&*citer); |
| 6764 | RTC_DCHECK(content != NULL); |
| 6765 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 6766 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6767 | if (!HasRtcpMuxEnabled(content)) |
| 6768 | return false; |
| 6769 | } |
| 6770 | } |
| 6771 | // RTCP-MUX is enabled in all the contents. |
| 6772 | return true; |
| 6773 | } |
| 6774 | |
| 6775 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6776 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6777 | } |
| 6778 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6779 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6780 | std::set<std::string> mids; |
| 6781 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 11:32:20 -0800 | [diff] [blame] | 6782 | if (content.name.empty()) { |
| 6783 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6784 | "A media section is missing a MID attribute."); |
| 6785 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6786 | if (!mids.insert(content.name).second) { |
| 6787 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6788 | "Duplicate a=mid value '" + content.name + "'."); |
| 6789 | } |
| 6790 | } |
| 6791 | return RTCError::OK(); |
| 6792 | } |
| 6793 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6794 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6795 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6796 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6797 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6798 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6802 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6803 | } |
| 6804 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6805 | SdpType type = sdesc->GetType(); |
| 6806 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6807 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6808 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 6809 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6810 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6811 | } |
| 6812 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6813 | RTCError error = ValidateMids(*sdesc->description()); |
| 6814 | if (!error.ok()) { |
| 6815 | return error; |
| 6816 | } |
| 6817 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6818 | // Verify crypto settings. |
| 6819 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6820 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6821 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6822 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6823 | if (!crypto_error.ok()) { |
| 6824 | return crypto_error; |
| 6825 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6826 | } |
| 6827 | |
| 6828 | // Verify ice-ufrag and ice-pwd. |
| 6829 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6830 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6831 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6832 | } |
| 6833 | |
| 6834 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6835 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6836 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6840 | // m-lines that do not rtcp-mux enabled. |
| 6841 | |
| 6842 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6843 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6844 | // With an answer we want to compare the new answer session description with |
| 6845 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6846 | const cricket::SessionDescription* offer_desc = |
| 6847 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6848 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6849 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6850 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6851 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6852 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6853 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6854 | } |
| 6855 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6856 | // The re-offers should respect the order of m= sections in current |
| 6857 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6858 | // With a re-offer, either the current local or current remote descriptions |
| 6859 | // could be the most up to date, so we would like to check against both of |
| 6860 | // them if they exist. It could be the case that one of them has a 0 port |
| 6861 | // for a media section, but the other does not. This is important to check |
| 6862 | // against in the case that we are recycling an m= section. |
| 6863 | const cricket::SessionDescription* current_desc = nullptr; |
| 6864 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6865 | if (local_description()) { |
| 6866 | current_desc = local_description()->description(); |
| 6867 | if (remote_description()) { |
| 6868 | secondary_current_desc = remote_description()->description(); |
| 6869 | } |
| 6870 | } else if (remote_description()) { |
| 6871 | current_desc = remote_description()->description(); |
| 6872 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6873 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6874 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6875 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6876 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6877 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6878 | } |
| 6879 | } |
| 6880 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6881 | if (IsUnifiedPlan()) { |
| 6882 | // Ensure that each audio and video media section has at most one |
| 6883 | // "StreamParams". This will return an error if receiving a session |
| 6884 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6885 | // same type. With Unified Plan, there can only be at most one track per |
| 6886 | // media section. |
| 6887 | for (const ContentInfo& content : sdesc->description()->contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 6888 | const MediaContentDescription& desc = *content.media_description(); |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6889 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6890 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6891 | desc.streams().size() > 1u) { |
| 6892 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6893 | "Media section has more than one track specified " |
| 6894 | "with a=ssrc lines which is not supported with " |
| 6895 | "Unified Plan."); |
| 6896 | } |
| 6897 | } |
| 6898 | } |
| 6899 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6900 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6901 | } |
| 6902 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6903 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6904 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6905 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6906 | return (state == PeerConnectionInterface::kStable) || |
| 6907 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6908 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6909 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6910 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6911 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6912 | } |
| 6913 | } |
| 6914 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6915 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6916 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6917 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6918 | return (state == PeerConnectionInterface::kStable) || |
| 6919 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6920 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6921 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6922 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6923 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6924 | } |
| 6925 | } |
| 6926 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6927 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6928 | switch (error) { |
| 6929 | case SessionError::kNone: |
| 6930 | return "ERROR_NONE"; |
| 6931 | case SessionError::kContent: |
| 6932 | return "ERROR_CONTENT"; |
| 6933 | case SessionError::kTransport: |
| 6934 | return "ERROR_TRANSPORT"; |
| 6935 | } |
| 6936 | RTC_NOTREACHED(); |
| 6937 | return ""; |
| 6938 | } |
| 6939 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6940 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 6941 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6942 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6943 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 6944 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6945 | } |
| 6946 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6947 | void PeerConnection::ReportSdpFormatReceived( |
| 6948 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6949 | int num_audio_mlines = 0; |
| 6950 | int num_video_mlines = 0; |
| 6951 | int num_audio_tracks = 0; |
| 6952 | int num_video_tracks = 0; |
| 6953 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6954 | cricket::MediaType media_type = content.media_description()->type(); |
| 6955 | int num_tracks = std::max( |
| 6956 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6957 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6958 | num_audio_mlines += 1; |
| 6959 | num_audio_tracks += num_tracks; |
| 6960 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6961 | num_video_mlines += 1; |
| 6962 | num_video_tracks += num_tracks; |
| 6963 | } |
| 6964 | } |
| 6965 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6966 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6967 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6968 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6969 | format = kSdpFormatReceivedComplexPlanB; |
| 6970 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6971 | format = kSdpFormatReceivedSimple; |
| 6972 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6973 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6974 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6975 | } |
| 6976 | |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6977 | void PeerConnection::ReportIceCandidateCollected( |
| 6978 | const cricket::Candidate& candidate) { |
| 6979 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 6980 | if (candidate.address().IsPrivateIP()) { |
| 6981 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 6982 | } |
| 6983 | if (candidate.address().IsUnresolvedIP()) { |
| 6984 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 6985 | } |
| 6986 | if (candidate.address().family() == AF_INET6) { |
| 6987 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 6988 | } |
| 6989 | } |
| 6990 | |
| 6991 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 6992 | const cricket::Candidate& candidate) { |
| 6993 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 6994 | if (candidate.address().IsPrivateIP()) { |
| 6995 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 6996 | } |
| 6997 | if (candidate.address().IsUnresolvedIP()) { |
| 6998 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 6999 | } |
| 7000 | if (candidate.address().family() == AF_INET6) { |
| 7001 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 7002 | } |
| 7003 | } |
| 7004 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 7005 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 7006 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7007 | usage_event_accumulator_ |= static_cast<int>(event); |
| 7008 | } |
| 7009 | |
| 7010 | void PeerConnection::ReportUsagePattern() const { |
| 7011 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7012 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 7013 | usage_event_accumulator_, |
| 7014 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 7015 | const int bad_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 7016 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 7017 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 7018 | const int good_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 7019 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 7020 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 7021 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 7022 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 7023 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7024 | // If called after close(), we can't report, because observer may have |
| 7025 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 7026 | if (observer_) { |
| 7027 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 7028 | } else { |
| 7029 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 7030 | << usage_event_accumulator_ |
| 7031 | << " observed after observer shutdown"; |
| 7032 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 7033 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 7034 | } |
| 7035 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7036 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 7037 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7038 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7039 | switch (answer.description()->msid_signaling()) { |
| 7040 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7041 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7042 | break; |
| 7043 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7044 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7045 | break; |
| 7046 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7047 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7048 | break; |
| 7049 | case cricket::kMsidSignalingMediaSection | |
| 7050 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7051 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7052 | break; |
| 7053 | default: |
| 7054 | RTC_NOTREACHED(); |
| 7055 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7056 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 7057 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7058 | } |
| 7059 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7060 | // We need to check the local/remote description for the Transport instead of |
| 7061 | // the session, because a new Transport added during renegotiation may have |
| 7062 | // them unset while the session has them set from the previous negotiation. |
| 7063 | // Not doing so may trigger the auto generation of transport description and |
| 7064 | // mess up DTLS identity information, ICE credential, etc. |
| 7065 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 7066 | const IceCandidateInterface* candidate, |
| 7067 | const SessionDescriptionInterface* remote_desc, |
| 7068 | bool* valid) { |
| 7069 | *valid = true; |
| 7070 | |
| 7071 | const SessionDescriptionInterface* current_remote_desc = |
| 7072 | remote_desc ? remote_desc : remote_description(); |
| 7073 | |
| 7074 | if (!current_remote_desc) { |
| 7075 | return false; |
| 7076 | } |
| 7077 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 7078 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 7079 | FindContentInfo(current_remote_desc, candidate); |
| 7080 | if (!result.ok()) { |
| 7081 | RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. " |
| 7082 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7083 | |
| 7084 | *valid = false; |
| 7085 | return false; |
| 7086 | } |
| 7087 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 7088 | std::string transport_name = GetTransportName(result.value()->name); |
| 7089 | return !transport_name.empty(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7090 | } |
| 7091 | |
| 7092 | bool PeerConnection::SrtpRequired() const { |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 7093 | return !use_datagram_transport_ && |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 7094 | (dtls_enabled_ || |
| 7095 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7096 | } |
| 7097 | |
| 7098 | void PeerConnection::OnTransportControllerGatheringState( |
| 7099 | cricket::IceGatheringState state) { |
| 7100 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 7101 | if (state == cricket::kIceGatheringGathering) { |
| 7102 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 7103 | } else if (state == cricket::kIceGatheringComplete) { |
| 7104 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 7105 | } |
| 7106 | } |
| 7107 | |
| 7108 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7109 | std::map<std::string, std::set<cricket::MediaType>> |
| 7110 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 7111 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7112 | if (transceiver->internal()->channel()) { |
| 7113 | const std::string& transport_name = |
| 7114 | transceiver->internal()->channel()->transport_name(); |
| 7115 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 7116 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7117 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7118 | } |
| 7119 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7120 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 7121 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7122 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7123 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 7124 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7125 | if (transport_name) { |
| 7126 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7127 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7128 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7129 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7130 | for (const auto& entry : media_types_by_transport_name) { |
| 7131 | const std::string& transport_name = entry.first; |
| 7132 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7133 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7134 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7135 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7136 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7137 | } |
| 7138 | } |
| 7139 | } |
| 7140 | // Walk through the ConnectionInfos to gather best connection usage |
| 7141 | // for IPv4 and IPv6. |
| 7142 | void PeerConnection::ReportBestConnectionState( |
| 7143 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7144 | for (const cricket::TransportChannelStats& channel_stats : |
| 7145 | stats.channel_stats) { |
| 7146 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 7147 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7148 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7149 | continue; |
| 7150 | } |
| 7151 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7152 | const cricket::Candidate& local = connection_info.local_candidate; |
| 7153 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7154 | |
| 7155 | // Increment the counter for IceCandidatePairType. |
| 7156 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 7157 | (local.type() == RELAY_PORT_TYPE && |
| 7158 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7159 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 7160 | GetIceCandidatePairCounter(local, remote), |
| 7161 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7162 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7163 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 7164 | GetIceCandidatePairCounter(local, remote), |
| 7165 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7166 | } else { |
| 7167 | RTC_CHECK(0); |
| 7168 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7169 | |
| 7170 | // Increment the counter for IP type. |
| 7171 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7172 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7173 | kBestConnections_IPv4, |
| 7174 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7175 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7176 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7177 | kBestConnections_IPv6, |
| 7178 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7179 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 7180 | RTC_CHECK(!local.address().hostname().empty() && |
| 7181 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7182 | } |
| 7183 | |
| 7184 | return; |
| 7185 | } |
| 7186 | } |
| 7187 | } |
| 7188 | |
| 7189 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7190 | const cricket::TransportStats& stats, |
| 7191 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7192 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 7193 | return; |
| 7194 | } |
| 7195 | |
| 7196 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 7197 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 7198 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 7199 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7200 | return; |
| 7201 | } |
| 7202 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7203 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 7204 | for (cricket::MediaType media_type : media_types) { |
| 7205 | switch (media_type) { |
| 7206 | case cricket::MEDIA_TYPE_AUDIO: |
| 7207 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7208 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 7209 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7210 | break; |
| 7211 | case cricket::MEDIA_TYPE_VIDEO: |
| 7212 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7213 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 7214 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7215 | break; |
| 7216 | case cricket::MEDIA_TYPE_DATA: |
| 7217 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7218 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 7219 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7220 | break; |
| 7221 | default: |
| 7222 | RTC_NOTREACHED(); |
| 7223 | continue; |
| 7224 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7225 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7226 | } |
| 7227 | |
| 7228 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7229 | for (cricket::MediaType media_type : media_types) { |
| 7230 | switch (media_type) { |
| 7231 | case cricket::MEDIA_TYPE_AUDIO: |
| 7232 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7233 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 7234 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7235 | break; |
| 7236 | case cricket::MEDIA_TYPE_VIDEO: |
| 7237 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7238 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 7239 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7240 | break; |
| 7241 | case cricket::MEDIA_TYPE_DATA: |
| 7242 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7243 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 7244 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7245 | break; |
| 7246 | default: |
| 7247 | RTC_NOTREACHED(); |
| 7248 | continue; |
| 7249 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7250 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7251 | } |
| 7252 | } |
| 7253 | |
| 7254 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 7255 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7256 | RTC_DCHECK(call_); |
| 7257 | call_->OnSentPacket(sent_packet); |
| 7258 | } |
| 7259 | |
| 7260 | const std::string PeerConnection::GetTransportName( |
| 7261 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7262 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7263 | if (channel) { |
| 7264 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7265 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 7266 | if (data_channel_controller_.data_channel_transport()) { |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 7267 | RTC_DCHECK(sctp_mid_s_); |
| 7268 | if (content_name == *sctp_mid_s_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7269 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7270 | } |
| 7271 | } |
| 7272 | // Return an empty string if failed to retrieve the transport name. |
| 7273 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7274 | } |
| 7275 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7276 | void PeerConnection::DestroyTransceiverChannel( |
| 7277 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 7278 | transceiver) { |
| 7279 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7280 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7281 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7282 | if (channel) { |
| 7283 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7284 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7285 | } |
| 7286 | } |
| 7287 | |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 7288 | void PeerConnection::DestroyDataChannelTransport() { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 7289 | if (data_channel_controller_.rtp_data_channel()) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 7290 | data_channel_controller_.OnTransportChannelClosed(); |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 7291 | DestroyChannelInterface(data_channel_controller_.rtp_data_channel()); |
| 7292 | data_channel_controller_.set_rtp_data_channel(nullptr); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7293 | } |
| 7294 | |
| 7295 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 7296 | // grab a reference to this PeerConnection. If this is called from the |
| 7297 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 7298 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 7299 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 7300 | |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 7301 | if (sctp_mid_s_) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 7302 | data_channel_controller_.OnTransportChannelClosed(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7303 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 7304 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7305 | TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7306 | }); |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 7307 | sctp_mid_s_.reset(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7308 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7309 | } |
| 7310 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7311 | void PeerConnection::DestroyChannelInterface( |
| 7312 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7313 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7314 | switch (channel->media_type()) { |
| 7315 | case cricket::MEDIA_TYPE_AUDIO: |
| 7316 | channel_manager()->DestroyVoiceChannel( |
| 7317 | static_cast<cricket::VoiceChannel*>(channel)); |
| 7318 | break; |
| 7319 | case cricket::MEDIA_TYPE_VIDEO: |
| 7320 | channel_manager()->DestroyVideoChannel( |
| 7321 | static_cast<cricket::VideoChannel*>(channel)); |
| 7322 | break; |
| 7323 | case cricket::MEDIA_TYPE_DATA: |
| 7324 | channel_manager()->DestroyRtpDataChannel( |
| 7325 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 7326 | break; |
| 7327 | default: |
| 7328 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 7329 | break; |
| 7330 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7331 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7332 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7333 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7334 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7335 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 7336 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7337 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 13:08:37 +0100 | [diff] [blame] | 7338 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7339 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7340 | auto base_channel = GetChannel(mid); |
| 7341 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7342 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7343 | } |
Harald Alvestrand | 7a829a8 | 2020-02-12 07:38:21 +0100 | [diff] [blame] | 7344 | if (mid == sctp_mid_n_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 09:56:02 +0100 | [diff] [blame] | 7345 | data_channel_controller_.OnTransportChanged(data_channel_transport); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7346 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7347 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7348 | } |
| 7349 | |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 7350 | void PeerConnection::OnSetStreams() { |
| 7351 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7352 | if (IsUnifiedPlan()) |
| 7353 | UpdateNegotiationNeeded(); |
| 7354 | } |
| 7355 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7356 | PeerConnectionObserver* PeerConnection::Observer() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 7357 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7358 | RTC_DCHECK(observer_); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7359 | return observer_; |
| 7360 | } |
| 7361 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 7362 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 7363 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 7364 | // after it has been removed. |
| 7365 | return configuration_.crypto_options.has_value() |
| 7366 | ? *configuration_.crypto_options |
| 7367 | : factory_->options().crypto_options; |
| 7368 | } |
| 7369 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 7370 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 7371 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 7372 | if (stats_collector_) { |
| 7373 | stats_collector_->ClearCachedStatsReport(); |
| 7374 | } |
| 7375 | } |
| 7376 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7377 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 7378 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 7379 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7380 | } |
| 7381 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7382 | void PeerConnection::UpdateNegotiationNeeded() { |
| 7383 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7384 | if (!IsUnifiedPlan()) { |
| 7385 | Observer()->OnRenegotiationNeeded(); |
| 7386 | return; |
| 7387 | } |
| 7388 | |
| 7389 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7390 | if (IsClosed()) |
| 7391 | return; |
| 7392 | |
| 7393 | // If connection's signaling state is not "stable", abort these steps. |
| 7394 | if (signaling_state() != kStable) |
| 7395 | return; |
| 7396 | |
| 7397 | // NOTE |
| 7398 | // The negotiation-needed flag will be updated once the state transitions to |
| 7399 | // "stable", as part of the steps for setting an RTCSessionDescription. |
| 7400 | |
| 7401 | // If the result of checking if negotiation is needed is false, clear the |
| 7402 | // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot |
| 7403 | // to false, and abort these steps. |
| 7404 | bool is_negotiation_needed = CheckIfNegotiationIsNeeded(); |
| 7405 | if (!is_negotiation_needed) { |
| 7406 | is_negotiation_needed_ = false; |
| 7407 | return; |
| 7408 | } |
| 7409 | |
| 7410 | // If connection's [[NegotiationNeeded]] slot is already true, abort these |
| 7411 | // steps. |
| 7412 | if (is_negotiation_needed_) |
| 7413 | return; |
| 7414 | |
| 7415 | // Set connection's [[NegotiationNeeded]] slot to true. |
| 7416 | is_negotiation_needed_ = true; |
| 7417 | |
| 7418 | // Queue a task that runs the following steps: |
| 7419 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7420 | // If connection's [[NegotiationNeeded]] slot is false, abort these steps. |
| 7421 | // Fire an event named negotiationneeded at connection. |
| 7422 | Observer()->OnRenegotiationNeeded(); |
| 7423 | } |
| 7424 | |
| 7425 | bool PeerConnection::CheckIfNegotiationIsNeeded() { |
| 7426 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7427 | // 1. If any implementation-specific negotiation is required, as described at |
| 7428 | // the start of this section, return true. |
| 7429 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7430 | // 2. If connection's [[RestartIce]] internal slot is true, return true. |
| 7431 | if (local_ice_credentials_to_replace_->HasIceCredentials()) { |
| 7432 | return true; |
| 7433 | } |
| 7434 | |
| 7435 | // 3. Let description be connection.[[CurrentLocalDescription]]. |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7436 | const SessionDescriptionInterface* description = current_local_description(); |
| 7437 | if (!description) |
| 7438 | return true; |
| 7439 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7440 | // 4. If connection has created any RTCDataChannels, and no m= section in |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7441 | // description has been negotiated yet for data, return true. |
Harald Alvestrand | 05e4d08 | 2019-12-03 14:04:21 +0100 | [diff] [blame] | 7442 | if (data_channel_controller_.HasSctpDataChannels()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7443 | if (!cricket::GetFirstDataContent(description->description()->contents())) |
| 7444 | return true; |
| 7445 | } |
| 7446 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7447 | // 5. For each transceiver in connection's set of transceivers, perform the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7448 | // following checks: |
| 7449 | for (const auto& transceiver : transceivers_) { |
| 7450 | const ContentInfo* current_local_msection = |
| 7451 | FindTransceiverMSection(transceiver.get(), description); |
| 7452 | |
| 7453 | const ContentInfo* current_remote_msection = FindTransceiverMSection( |
| 7454 | transceiver.get(), current_remote_description()); |
| 7455 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7456 | // 5.3 If transceiver is stopped and is associated with an m= section, |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7457 | // but the associated m= section is not yet rejected in |
| 7458 | // connection.[[CurrentLocalDescription]] or |
| 7459 | // connection.[[CurrentRemoteDescription]], return true. |
| 7460 | if (transceiver->stopped()) { |
| 7461 | if (current_local_msection && !current_local_msection->rejected && |
| 7462 | ((current_remote_msection && !current_remote_msection->rejected) || |
| 7463 | !current_remote_msection)) { |
| 7464 | return true; |
| 7465 | } |
| 7466 | continue; |
| 7467 | } |
| 7468 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7469 | // 5.1 If transceiver isn't stopped and isn't yet associated with an m= |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7470 | // section in description, return true. |
| 7471 | if (!current_local_msection) |
| 7472 | return true; |
| 7473 | |
| 7474 | const MediaContentDescription* current_local_media_description = |
| 7475 | current_local_msection->media_description(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7476 | // 5.2 If transceiver isn't stopped and is associated with an m= section |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7477 | // in description then perform the following checks: |
| 7478 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7479 | // 5.2.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7480 | // associated m= section in description either doesn't contain a single |
| 7481 | // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this |
| 7482 | // m= section, or the MSID values themselves, differ from what is in |
| 7483 | // transceiver.sender.[[AssociatedMediaStreamIds]], return true. |
| 7484 | if (RtpTransceiverDirectionHasSend(transceiver->direction())) { |
| 7485 | if (current_local_media_description->streams().size() == 0) |
| 7486 | return true; |
| 7487 | |
| 7488 | std::vector<std::string> msection_msids; |
| 7489 | for (const auto& stream : current_local_media_description->streams()) { |
| 7490 | for (const std::string& msid : stream.stream_ids()) |
| 7491 | msection_msids.push_back(msid); |
| 7492 | } |
| 7493 | |
| 7494 | std::vector<std::string> transceiver_msids = |
| 7495 | transceiver->sender()->stream_ids(); |
| 7496 | if (msection_msids.size() != transceiver_msids.size()) |
| 7497 | return true; |
| 7498 | |
| 7499 | absl::c_sort(transceiver_msids); |
| 7500 | absl::c_sort(msection_msids); |
| 7501 | if (transceiver_msids != msection_msids) |
| 7502 | return true; |
| 7503 | } |
| 7504 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7505 | // 5.2.2 If description is of type "offer", and the direction of the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7506 | // associated m= section in neither connection.[[CurrentLocalDescription]] |
| 7507 | // nor connection.[[CurrentRemoteDescription]] matches |
| 7508 | // transceiver.[[Direction]], return true. |
| 7509 | if (description->GetType() == SdpType::kOffer) { |
| 7510 | if (!current_remote_description()) |
| 7511 | return true; |
| 7512 | |
| 7513 | if (!current_remote_msection) |
| 7514 | return true; |
| 7515 | |
| 7516 | RtpTransceiverDirection current_local_direction = |
| 7517 | current_local_media_description->direction(); |
| 7518 | RtpTransceiverDirection current_remote_direction = |
| 7519 | current_remote_msection->media_description()->direction(); |
| 7520 | if (transceiver->direction() != current_local_direction && |
| 7521 | transceiver->direction() != |
| 7522 | RtpTransceiverDirectionReversed(current_remote_direction)) { |
| 7523 | return true; |
| 7524 | } |
| 7525 | } |
| 7526 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7527 | // 5.2.3 If description is of type "answer", and the direction of the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7528 | // associated m= section in the description does not match |
| 7529 | // transceiver.[[Direction]] intersected with the offered direction (as |
| 7530 | // described in [JSEP] (section 5.3.1.)), return true. |
| 7531 | if (description->GetType() == SdpType::kAnswer) { |
| 7532 | if (!remote_description()) |
| 7533 | return true; |
| 7534 | |
| 7535 | const ContentInfo* offered_remote_msection = |
| 7536 | FindTransceiverMSection(transceiver.get(), remote_description()); |
| 7537 | |
| 7538 | RtpTransceiverDirection offered_direction = |
| 7539 | offered_remote_msection |
| 7540 | ? offered_remote_msection->media_description()->direction() |
| 7541 | : RtpTransceiverDirection::kInactive; |
| 7542 | |
| 7543 | if (current_local_media_description->direction() != |
| 7544 | (RtpTransceiverDirectionIntersection( |
| 7545 | transceiver->direction(), |
| 7546 | RtpTransceiverDirectionReversed(offered_direction)))) { |
| 7547 | return true; |
| 7548 | } |
| 7549 | } |
| 7550 | } |
| 7551 | |
| 7552 | // If all the preceding checks were performed and true was not returned, |
| 7553 | // nothing remains to be negotiated; return false. |
| 7554 | return false; |
| 7555 | } |
| 7556 | |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 7557 | RTCError PeerConnection::Rollback(SdpType sdp_type) { |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7558 | auto state = signaling_state(); |
| 7559 | if (state != PeerConnectionInterface::kHaveLocalOffer && |
| 7560 | state != PeerConnectionInterface::kHaveRemoteOffer) { |
| 7561 | return RTCError(RTCErrorType::INVALID_STATE, |
| 7562 | "Called in wrong signalingState: " + |
| 7563 | GetSignalingStateString(signaling_state())); |
| 7564 | } |
| 7565 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7566 | RTC_DCHECK(IsUnifiedPlan()); |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 7567 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams; |
| 7568 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams; |
| 7569 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers; |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7570 | |
| 7571 | for (auto&& transceivers_stable_state_pair : |
| 7572 | transceiver_stable_states_by_transceivers_) { |
| 7573 | auto transceiver = transceivers_stable_state_pair.first; |
| 7574 | auto state = transceivers_stable_state_pair.second; |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 7575 | |
| 7576 | if (state.remote_stream_ids()) { |
| 7577 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
| 7578 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
| 7579 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 7580 | state.remote_stream_ids().value(), |
| 7581 | &added_streams, &removed_streams); |
| 7582 | all_added_streams.insert(all_added_streams.end(), added_streams.begin(), |
| 7583 | added_streams.end()); |
| 7584 | all_removed_streams.insert(all_removed_streams.end(), |
| 7585 | removed_streams.begin(), |
| 7586 | removed_streams.end()); |
| 7587 | if (!state.has_m_section() && !state.newly_created()) { |
| 7588 | continue; |
| 7589 | } |
| 7590 | } |
| 7591 | |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7592 | RTC_DCHECK(transceiver->internal()->mid().has_value()); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7593 | DestroyTransceiverChannel(transceiver); |
| 7594 | |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 7595 | if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer && |
| 7596 | transceiver->receiver()) { |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 7597 | removed_receivers.push_back(transceiver->receiver()); |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 7598 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7599 | if (state.newly_created()) { |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 7600 | if (transceiver->internal()->reused_for_addtrack()) { |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7601 | transceiver->internal()->set_created_by_addtrack(true); |
| 7602 | } else { |
| 7603 | int remaining_transceiver_count = 0; |
| 7604 | for (auto&& t : transceivers_) { |
| 7605 | if (t != transceiver) { |
| 7606 | transceivers_[remaining_transceiver_count++] = t; |
| 7607 | } |
| 7608 | } |
| 7609 | transceivers_.resize(remaining_transceiver_count); |
| 7610 | } |
| 7611 | } |
| 7612 | transceiver->internal()->sender_internal()->set_transport(nullptr); |
| 7613 | transceiver->internal()->receiver_internal()->set_transport(nullptr); |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7614 | transceiver->internal()->set_mid(state.mid()); |
| 7615 | transceiver->internal()->set_mline_index(state.mline_index()); |
| 7616 | } |
Eldar Rello | d85ea75 | 2020-02-19 20:41:07 +0200 | [diff] [blame] | 7617 | transport_controller_->RollbackTransports(); |
Eldar Rello | d9ebe01 | 2020-03-18 20:41:45 +0200 | [diff] [blame] | 7618 | if (have_pending_rtp_data_channel_) { |
| 7619 | DestroyDataChannelTransport(); |
| 7620 | have_pending_rtp_data_channel_ = false; |
| 7621 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7622 | transceiver_stable_states_by_transceivers_.clear(); |
| 7623 | pending_local_description_.reset(); |
| 7624 | pending_remote_description_.reset(); |
| 7625 | ChangeSignalingState(PeerConnectionInterface::kStable); |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 7626 | |
Eldar Rello | 353a718 | 2019-11-25 18:49:44 +0200 | [diff] [blame] | 7627 | // Once all processing has finished, fire off callbacks. |
| 7628 | for (const auto& receiver : removed_receivers) { |
| 7629 | Observer()->OnRemoveTrack(receiver); |
| 7630 | } |
| 7631 | for (const auto& stream : all_added_streams) { |
| 7632 | Observer()->OnAddStream(stream); |
| 7633 | } |
| 7634 | for (const auto& stream : all_removed_streams) { |
| 7635 | Observer()->OnRemoveStream(stream); |
| 7636 | } |
| 7637 | |
Eldar Rello | ead0ec9 | 2019-10-21 23:01:31 +0300 | [diff] [blame] | 7638 | // The assumption is that in case of implicit rollback UpdateNegotiationNeeded |
| 7639 | // gets called in SetRemoteDescription. |
| 7640 | if (sdp_type == SdpType::kRollback) { |
| 7641 | UpdateNegotiationNeeded(); |
| 7642 | if (is_negotiation_needed_) { |
| 7643 | Observer()->OnRenegotiationNeeded(); |
| 7644 | } |
| 7645 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 18:29:44 +0300 | [diff] [blame] | 7646 | return RTCError::OK(); |
| 7647 | } |
| 7648 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 7649 | } // namespace webrtc |