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" |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 59 | #include "rtc_base/system/fallthrough.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 60 | #include "rtc_base/trace_event.h" |
| 61 | #include "system_wrappers/include/clock.h" |
| 62 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 63 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 65 | using cricket::ContentInfo; |
| 66 | using cricket::ContentInfos; |
| 67 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 68 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 69 | using cricket::RidDescription; |
| 70 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 71 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 72 | using cricket::SimulcastDescription; |
| 73 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 74 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 75 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 76 | using cricket::TransportInfo; |
| 77 | |
| 78 | using cricket::LOCAL_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 79 | using cricket::PRFLX_PORT_TYPE; |
Jeroen de Borst | af242c8 | 2019-04-24 13:13:48 -0700 | [diff] [blame] | 80 | using cricket::RELAY_PORT_TYPE; |
| 81 | using cricket::STUN_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 82 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 83 | namespace webrtc { |
| 84 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 85 | // Error messages |
| 86 | const char kBundleWithoutRtcpMux[] = |
| 87 | "rtcp-mux must be enabled when BUNDLE " |
| 88 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 89 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 90 | const char kInvalidSdp[] = "Invalid session description."; |
| 91 | const char kMlineMismatchInAnswer[] = |
| 92 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 93 | "answer."; |
| 94 | const char kMlineMismatchInSubsequentOffer[] = |
| 95 | "The order of m-lines in subsequent offer doesn't match order from " |
| 96 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 97 | const char kSdpWithoutDtlsFingerprint[] = |
| 98 | "Called with SDP without DTLS fingerprint."; |
| 99 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 100 | const char kSdpWithoutIceUfragPwd[] = |
| 101 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 102 | const char kSessionError[] = "Session error code: "; |
| 103 | const char kSessionErrorDesc[] = "Session error description: "; |
| 104 | const char kDtlsSrtpSetupFailureRtp[] = |
| 105 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 106 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 107 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 109 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 111 | // Field trials. |
| 112 | // Controls datagram transport support. |
| 113 | const char kDatagramTransportFieldTrial[] = "WebRTC-DatagramTransport"; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 114 | // Controls datagram transport data channel support. |
| 115 | const char kDatagramTransportDataChannelFieldTrial[] = |
| 116 | "WebRTC-DatagramTransportDataChannels"; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 117 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 118 | // UMA metric names. |
| 119 | const char kSimulcastVersionApplyLocalDescription[] = |
| 120 | "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription"; |
| 121 | const char kSimulcastVersionApplyRemoteDescription[] = |
| 122 | "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription"; |
| 123 | const char kSimulcastNumberOfEncodings[] = |
| 124 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
| 125 | const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled"; |
| 126 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 127 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 128 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 129 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 130 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 131 | // The length of RTCP CNAMEs. |
| 132 | static const int kRtcpCnameLength = 16; |
| 133 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 134 | enum { |
| 135 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 136 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 137 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 138 | MSG_GETSTATS, |
| 139 | MSG_FREE_DATACHANNELS, |
| 140 | MSG_REPORT_USAGE_PATTERN, |
| 141 | }; |
| 142 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 143 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 144 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 145 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 146 | explicit SetSessionDescriptionMsg( |
| 147 | webrtc::SetSessionDescriptionObserver* observer) |
| 148 | : observer(observer) {} |
| 149 | |
| 150 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 151 | RTCError error; |
| 152 | }; |
| 153 | |
| 154 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 155 | explicit CreateSessionDescriptionMsg( |
| 156 | webrtc::CreateSessionDescriptionObserver* observer) |
| 157 | : observer(observer) {} |
| 158 | |
| 159 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 160 | RTCError error; |
| 161 | }; |
| 162 | |
| 163 | struct GetStatsMsg : public rtc::MessageData { |
| 164 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 165 | webrtc::MediaStreamTrackInterface* track) |
| 166 | : observer(observer), track(track) {} |
| 167 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 168 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 169 | }; |
| 170 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 171 | // Check if we can send |new_stream| on a PeerConnection. |
| 172 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 173 | webrtc::MediaStreamInterface* new_stream) { |
| 174 | if (!new_stream || !current_streams) { |
| 175 | return false; |
| 176 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 177 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 178 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 179 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 185 | // If the direction is "recvonly" or "inactive", treat the description |
| 186 | // as containing no streams. |
| 187 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 188 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 189 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 190 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 191 | ? desc->streams() |
| 192 | : std::vector<cricket::StreamParams>(); |
| 193 | } |
| 194 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 195 | bool IsValidOfferToReceiveMedia(int value) { |
| 196 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 197 | return (value >= Options::kUndefined) && |
| 198 | (value <= Options::kMaxOfferToReceiveMedia); |
| 199 | } |
| 200 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 201 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 202 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 203 | const std::vector<rtc::scoped_refptr< |
| 204 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 205 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 206 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 207 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 208 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 209 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 210 | if (audio_media_description_options) { |
| 211 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 212 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 213 | } |
| 214 | } else { |
| 215 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 216 | if (video_media_description_options) { |
| 217 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 218 | sender->id(), sender->internal()->stream_ids(), {}, |
| 219 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 222 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 223 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 224 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 225 | // Add options to |session_options| from |rtp_data_channels|. |
| 226 | void AddRtpDataChannelOptions( |
| 227 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 228 | rtp_data_channels, |
| 229 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 230 | if (!data_media_description_options) { |
| 231 | return; |
| 232 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 233 | // Check for data channels. |
| 234 | for (const auto& kv : rtp_data_channels) { |
| 235 | const DataChannel* channel = kv.second; |
| 236 | if (channel->state() == DataChannel::kConnecting || |
| 237 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 238 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 239 | // the data channel's label. |
| 240 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 241 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 246 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 247 | PeerConnectionInterface::IceTransportsType type) { |
| 248 | switch (type) { |
| 249 | case PeerConnectionInterface::kNone: |
| 250 | return cricket::CF_NONE; |
| 251 | case PeerConnectionInterface::kRelay: |
| 252 | return cricket::CF_RELAY; |
| 253 | case PeerConnectionInterface::kNoHost: |
| 254 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 255 | case PeerConnectionInterface::kAll: |
| 256 | return cricket::CF_ALL; |
| 257 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 258 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 259 | } |
| 260 | return cricket::CF_NONE; |
| 261 | } |
| 262 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 263 | std::string GetSignalingStateString( |
| 264 | PeerConnectionInterface::SignalingState state) { |
| 265 | switch (state) { |
| 266 | case PeerConnectionInterface::kStable: |
| 267 | return "kStable"; |
| 268 | case PeerConnectionInterface::kHaveLocalOffer: |
| 269 | return "kHaveLocalOffer"; |
| 270 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 271 | return "kHavePrAnswer"; |
| 272 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 273 | return "kHaveRemoteOffer"; |
| 274 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 275 | return "kHaveRemotePrAnswer"; |
| 276 | case PeerConnectionInterface::kClosed: |
| 277 | return "kClosed"; |
| 278 | } |
| 279 | RTC_NOTREACHED(); |
| 280 | return ""; |
| 281 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 282 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 283 | IceCandidatePairType GetIceCandidatePairCounter( |
| 284 | const cricket::Candidate& local, |
| 285 | const cricket::Candidate& remote) { |
| 286 | const auto& l = local.type(); |
| 287 | const auto& r = remote.type(); |
| 288 | const auto& host = LOCAL_PORT_TYPE; |
| 289 | const auto& srflx = STUN_PORT_TYPE; |
| 290 | const auto& relay = RELAY_PORT_TYPE; |
| 291 | const auto& prflx = PRFLX_PORT_TYPE; |
| 292 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 293 | bool local_hostname = |
| 294 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 295 | bool remote_hostname = !remote.address().hostname().empty() && |
| 296 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 297 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 298 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 299 | if (local_hostname) { |
| 300 | if (remote_hostname) { |
| 301 | return kIceCandidatePairHostNameHostName; |
| 302 | } else if (remote_private) { |
| 303 | return kIceCandidatePairHostNameHostPrivate; |
| 304 | } else { |
| 305 | return kIceCandidatePairHostNameHostPublic; |
| 306 | } |
| 307 | } else if (local_private) { |
| 308 | if (remote_hostname) { |
| 309 | return kIceCandidatePairHostPrivateHostName; |
| 310 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 311 | return kIceCandidatePairHostPrivateHostPrivate; |
| 312 | } else { |
| 313 | return kIceCandidatePairHostPrivateHostPublic; |
| 314 | } |
| 315 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 316 | if (remote_hostname) { |
| 317 | return kIceCandidatePairHostPublicHostName; |
| 318 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 319 | return kIceCandidatePairHostPublicHostPrivate; |
| 320 | } else { |
| 321 | return kIceCandidatePairHostPublicHostPublic; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | if (l == host && r == srflx) |
| 326 | return kIceCandidatePairHostSrflx; |
| 327 | if (l == host && r == relay) |
| 328 | return kIceCandidatePairHostRelay; |
| 329 | if (l == host && r == prflx) |
| 330 | return kIceCandidatePairHostPrflx; |
| 331 | if (l == srflx && r == host) |
| 332 | return kIceCandidatePairSrflxHost; |
| 333 | if (l == srflx && r == srflx) |
| 334 | return kIceCandidatePairSrflxSrflx; |
| 335 | if (l == srflx && r == relay) |
| 336 | return kIceCandidatePairSrflxRelay; |
| 337 | if (l == srflx && r == prflx) |
| 338 | return kIceCandidatePairSrflxPrflx; |
| 339 | if (l == relay && r == host) |
| 340 | return kIceCandidatePairRelayHost; |
| 341 | if (l == relay && r == srflx) |
| 342 | return kIceCandidatePairRelaySrflx; |
| 343 | if (l == relay && r == relay) |
| 344 | return kIceCandidatePairRelayRelay; |
| 345 | if (l == relay && r == prflx) |
| 346 | return kIceCandidatePairRelayPrflx; |
| 347 | if (l == prflx && r == host) |
| 348 | return kIceCandidatePairPrflxHost; |
| 349 | if (l == prflx && r == srflx) |
| 350 | return kIceCandidatePairPrflxSrflx; |
| 351 | if (l == prflx && r == relay) |
| 352 | return kIceCandidatePairPrflxRelay; |
| 353 | return kIceCandidatePairMax; |
| 354 | } |
| 355 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 356 | // Logic to decide if an m= section can be recycled. This means that the new |
| 357 | // m= section is not rejected, but the old local or remote m= section is |
| 358 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 359 | // of the old remote and old local descriptions in no particular order. |
| 360 | // We need to check both the old local and remote because either |
| 361 | // could be the most current from the latest negotation. |
| 362 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 363 | const ContentInfo& content, |
| 364 | const ContentInfo* old_content_one, |
| 365 | const ContentInfo* old_content_two) { |
| 366 | return type == SdpType::kOffer && !content.rejected && |
| 367 | ((old_content_one && old_content_one->rejected) || |
| 368 | (old_content_two && old_content_two->rejected)); |
| 369 | } |
| 370 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 371 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 372 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 373 | // less than |current_desc|. In the case of checking an answer's |
| 374 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 375 | // local or remote. In the case of checking an offer's |new_desc| we |
| 376 | // check against the local and remote descriptions stored from the last |
| 377 | // negotiation, because either of these could be the most up to date for |
| 378 | // possible rejected m sections. These are the |current_desc| and |
| 379 | // |secondary_current_desc|. |
| 380 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 381 | const SessionDescription* secondary_current_desc, |
| 382 | const SessionDescription& new_desc, |
| 383 | const SdpType type) { |
| 384 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 388 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 389 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 390 | if (secondary_current_desc && |
| 391 | i < secondary_current_desc->contents().size()) { |
| 392 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 393 | } |
| 394 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 395 | ¤t_desc.contents()[i], |
| 396 | secondary_content_info)) { |
| 397 | // For new offer descriptions, if the media section can be recycled, it's |
| 398 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 399 | continue; |
| 400 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 401 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 402 | return false; |
| 403 | } |
| 404 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 405 | new_desc.contents()[i].media_description(); |
| 406 | const MediaContentDescription* current_desc_mdesc = |
| 407 | current_desc.contents()[i].media_description(); |
| 408 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | return true; |
| 413 | } |
| 414 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 415 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 416 | const SessionDescription& desc2) { |
| 417 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 420 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 421 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 422 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 423 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 424 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 425 | static constexpr struct { |
| 426 | KeyExchangeProtocolType protocol_type; |
| 427 | cricket::MediaType media_type; |
| 428 | KeyExchangeProtocolMedia protocol_media; |
| 429 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 430 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 431 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 432 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 433 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 434 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 435 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 436 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 437 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 438 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 439 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 440 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 441 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 442 | }; |
| 443 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 444 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 445 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 446 | |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 447 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 448 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 449 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 450 | i.protocol_media, |
| 451 | kEnumCounterKeyProtocolMediaTypeMax); |
| 452 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 456 | void NoteAddIceCandidateResult(int result) { |
| 457 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 458 | kAddIceCandidateMax); |
| 459 | } |
| 460 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 461 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 462 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 463 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 464 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 465 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 466 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 467 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 468 | const cricket::ContentGroup* bundle = |
| 469 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 470 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 471 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 472 | continue; |
| 473 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 474 | // Note what media is used with each crypto protocol, for all sections. |
| 475 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 476 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 477 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 478 | const std::string& mid = content_info.name; |
| 479 | if (bundle && bundle->HasContentName(mid) && |
| 480 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 481 | // This isn't the first media section in the BUNDLE group, so it's not |
| 482 | // required to have crypto attributes, since only the crypto attributes |
| 483 | // from the first section actually get used. |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | // If the content isn't rejected or bundled into another m= section, crypto |
| 488 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 489 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 490 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 491 | if (!media || !tinfo) { |
| 492 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 493 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 494 | } |
| 495 | if (dtls_enabled) { |
| 496 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 497 | RTC_LOG(LS_WARNING) |
| 498 | << "Session description must have DTLS fingerprint if " |
| 499 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 500 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 501 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 502 | } |
| 503 | } else { |
| 504 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 505 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 506 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 507 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 511 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 515 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 516 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 517 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 518 | const cricket::ContentGroup* bundle = |
| 519 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 520 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 521 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 522 | continue; |
| 523 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 524 | const std::string& mid = content_info.name; |
| 525 | if (bundle && bundle->HasContentName(mid) && |
| 526 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 527 | // This isn't the first media section in the BUNDLE group, so it's not |
| 528 | // required to have ufrag/password, since only the ufrag/password from |
| 529 | // the first section actually get used. |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | // If the content isn't rejected or bundled into another m= section, |
| 534 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 535 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 536 | if (!tinfo) { |
| 537 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 538 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | if (tinfo->description.ice_ufrag.empty() || |
| 542 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 543 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 550 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 551 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 552 | const SessionDescriptionInterface* new_desc, |
| 553 | const std::string& content_name) { |
| 554 | if (!old_desc) { |
| 555 | return false; |
| 556 | } |
| 557 | const SessionDescription* new_sd = new_desc->description(); |
| 558 | const SessionDescription* old_sd = old_desc->description(); |
| 559 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 560 | if (!cinfo || cinfo->rejected) { |
| 561 | return false; |
| 562 | } |
| 563 | // If the content isn't rejected, check if ufrag and password has changed. |
| 564 | const cricket::TransportDescription* new_transport_desc = |
| 565 | new_sd->GetTransportDescriptionByName(content_name); |
| 566 | const cricket::TransportDescription* old_transport_desc = |
| 567 | old_sd->GetTransportDescriptionByName(content_name); |
| 568 | if (!new_transport_desc || !old_transport_desc) { |
| 569 | // No transport description exists. This is not an ICE restart. |
| 570 | return false; |
| 571 | } |
| 572 | if (cricket::IceCredentialsChanged( |
| 573 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 574 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 575 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 576 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 577 | return true; |
| 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 582 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 583 | // from an RTCError. |
| 584 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 585 | SdpType type, |
| 586 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 587 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 588 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 589 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 590 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 593 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 594 | std::string output = "streams=["; |
| 595 | const char* separator = ""; |
| 596 | for (const auto& stream_id : stream_ids) { |
| 597 | output.append(separator).append(stream_id); |
| 598 | separator = ", "; |
| 599 | } |
| 600 | output.append("]"); |
| 601 | return output; |
| 602 | } |
| 603 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 604 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 605 | int rtc_configuration_parameter) { |
| 606 | if (rtc_configuration_parameter == |
| 607 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 608 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 609 | } |
| 610 | return rtc_configuration_parameter; |
| 611 | } |
| 612 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 613 | void ReportSimulcastApiVersion(const char* name, |
| 614 | const SessionDescription& session) { |
| 615 | bool has_legacy = false; |
| 616 | bool has_spec_compliant = false; |
| 617 | for (const ContentInfo& content : session.contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 618 | if (!content.media_description()) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 619 | continue; |
| 620 | } |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 621 | has_spec_compliant |= content.media_description()->HasSimulcast(); |
| 622 | for (const StreamParams& sp : content.media_description()->streams()) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 623 | has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | if (has_legacy) { |
| 628 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy, |
| 629 | kSimulcastApiVersionMax); |
| 630 | } |
| 631 | if (has_spec_compliant) { |
| 632 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant, |
| 633 | kSimulcastApiVersionMax); |
| 634 | } |
| 635 | if (!has_legacy && !has_spec_compliant) { |
| 636 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone, |
| 637 | kSimulcastApiVersionMax); |
| 638 | } |
| 639 | } |
| 640 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 641 | const ContentInfo* FindTransceiverMSection( |
| 642 | RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver, |
| 643 | const SessionDescriptionInterface* session_description) { |
| 644 | return transceiver->mid() |
| 645 | ? session_description->description()->GetContentByName( |
| 646 | *transceiver->mid()) |
| 647 | : nullptr; |
| 648 | } |
| 649 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 650 | } // namespace |
| 651 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 652 | class PeerConnection::LocalIceCredentialsToReplace { |
| 653 | public: |
| 654 | // Sets the ICE credentials that need restarting to the ICE credentials of |
| 655 | // the current and pending descriptions. |
| 656 | void SetIceCredentialsFromLocalDescriptions( |
| 657 | const SessionDescriptionInterface* current_local_description, |
| 658 | const SessionDescriptionInterface* pending_local_description) { |
| 659 | ice_credentials_.clear(); |
| 660 | if (current_local_description) { |
| 661 | AppendIceCredentialsFromSessionDescription(*current_local_description); |
| 662 | } |
| 663 | if (pending_local_description) { |
| 664 | AppendIceCredentialsFromSessionDescription(*pending_local_description); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void ClearIceCredentials() { ice_credentials_.clear(); } |
| 669 | |
| 670 | // Returns true if we have ICE credentials that need restarting. |
| 671 | bool HasIceCredentials() const { return !ice_credentials_.empty(); } |
| 672 | |
| 673 | // Returns true if |local_description| shares no ICE credentials with the |
| 674 | // ICE credentials that need restarting. |
| 675 | bool SatisfiesIceRestart( |
| 676 | const SessionDescriptionInterface& local_description) const { |
| 677 | for (const auto& transport_info : |
| 678 | local_description.description()->transport_infos()) { |
| 679 | if (ice_credentials_.find(std::make_pair( |
| 680 | transport_info.description.ice_ufrag, |
| 681 | transport_info.description.ice_pwd)) != ice_credentials_.end()) { |
| 682 | return false; |
| 683 | } |
| 684 | } |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | private: |
| 689 | void AppendIceCredentialsFromSessionDescription( |
| 690 | const SessionDescriptionInterface& desc) { |
| 691 | for (const auto& transport_info : desc.description()->transport_infos()) { |
| 692 | ice_credentials_.insert( |
| 693 | std::make_pair(transport_info.description.ice_ufrag, |
| 694 | transport_info.description.ice_pwd)); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | std::set<std::pair<std::string, std::string>> ice_credentials_; |
| 699 | }; |
| 700 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 701 | // Upon completion, posts a task to execute the callback of the |
| 702 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 703 | // point, the state of the peer connection might no longer reflect the effects |
| 704 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 705 | // modified during the post. |
| 706 | // TODO(hbos): Remove this class once we remove the version of |
| 707 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 708 | // SetSessionDescriptionObserver as an argument. |
| 709 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 710 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 711 | public: |
| 712 | SetRemoteDescriptionObserverAdapter( |
| 713 | rtc::scoped_refptr<PeerConnection> pc, |
| 714 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 715 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 716 | |
| 717 | // SetRemoteDescriptionObserverInterface implementation. |
| 718 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 719 | if (error.ok()) |
| 720 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 721 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 722 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | private: |
| 726 | rtc::scoped_refptr<PeerConnection> pc_; |
| 727 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 728 | }; |
| 729 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 730 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 731 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 732 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 733 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 734 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 735 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 736 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 737 | BundlePolicy bundle_policy; |
| 738 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 739 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 740 | int ice_candidate_pool_size; |
| 741 | bool disable_ipv6; |
| 742 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 743 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 744 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 745 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 746 | absl::optional<int> screencast_min_bitrate; |
| 747 | absl::optional<bool> combined_audio_video_bwe; |
| 748 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 749 | TcpCandidatePolicy tcp_candidate_policy; |
| 750 | CandidateNetworkPolicy candidate_network_policy; |
| 751 | int audio_jitter_buffer_max_packets; |
| 752 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 753 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 754 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 755 | int ice_connection_receiving_timeout; |
| 756 | int ice_backup_candidate_pair_ping_interval; |
| 757 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 758 | bool prioritize_most_likely_ice_candidate_pairs; |
| 759 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 760 | bool prune_turn_ports; |
| 761 | bool presume_writable_when_fully_relayed; |
| 762 | bool enable_ice_renomination; |
| 763 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 764 | bool surface_ice_candidates_on_ice_transport_type_changed; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 765 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 766 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 767 | absl::optional<int> ice_check_min_interval; |
| 768 | absl::optional<int> ice_unwritable_timeout; |
| 769 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 770 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 771 | absl::optional<int> stun_candidate_keepalive_interval; |
| 772 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 773 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 774 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 775 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 776 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 777 | bool use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 778 | bool use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 779 | absl::optional<bool> use_datagram_transport; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 780 | absl::optional<bool> use_datagram_transport_for_data_channels; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 781 | absl::optional<bool> use_datagram_transport_for_data_channels_receive_only; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 782 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 783 | bool offer_extmap_allow_mixed; |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 784 | std::string turn_logging_id; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 785 | }; |
| 786 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 787 | "Did you add something to RTCConfiguration and forget to " |
| 788 | "update operator==?"); |
| 789 | return type == o.type && servers == o.servers && |
| 790 | bundle_policy == o.bundle_policy && |
| 791 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 792 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 793 | candidate_network_policy == o.candidate_network_policy && |
| 794 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 795 | audio_jitter_buffer_fast_accelerate == |
| 796 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 797 | audio_jitter_buffer_min_delay_ms == |
| 798 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 799 | audio_jitter_buffer_enable_rtx_handling == |
| 800 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 801 | ice_connection_receiving_timeout == |
| 802 | o.ice_connection_receiving_timeout && |
| 803 | ice_backup_candidate_pair_ping_interval == |
| 804 | o.ice_backup_candidate_pair_ping_interval && |
| 805 | continual_gathering_policy == o.continual_gathering_policy && |
| 806 | certificates == o.certificates && |
| 807 | prioritize_most_likely_ice_candidate_pairs == |
| 808 | o.prioritize_most_likely_ice_candidate_pairs && |
| 809 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 810 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 811 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 812 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 813 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 814 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 815 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 816 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 817 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 818 | prune_turn_ports == o.prune_turn_ports && |
| 819 | presume_writable_when_fully_relayed == |
| 820 | o.presume_writable_when_fully_relayed && |
| 821 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 822 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 823 | surface_ice_candidates_on_ice_transport_type_changed == |
| 824 | o.surface_ice_candidates_on_ice_transport_type_changed && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 825 | ice_check_interval_strong_connectivity == |
| 826 | o.ice_check_interval_strong_connectivity && |
| 827 | ice_check_interval_weak_connectivity == |
| 828 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 829 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 830 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 831 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 832 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 833 | stun_candidate_keepalive_interval == |
| 834 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 835 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 836 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 837 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 838 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 839 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 840 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 841 | use_media_transport_for_data_channels == |
| 842 | o.use_media_transport_for_data_channels && |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 843 | use_datagram_transport == o.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 844 | use_datagram_transport_for_data_channels == |
| 845 | o.use_datagram_transport_for_data_channels && |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 846 | use_datagram_transport_for_data_channels_receive_only == |
| 847 | o.use_datagram_transport_for_data_channels_receive_only && |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 848 | crypto_options == o.crypto_options && |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 849 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed && |
| 850 | turn_logging_id == o.turn_logging_id; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 854 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 855 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 856 | } |
| 857 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 858 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 859 | std::string GenerateRtcpCname() { |
| 860 | std::string cname; |
| 861 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 862 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 863 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 864 | } |
| 865 | return cname; |
| 866 | } |
| 867 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 868 | bool ValidateOfferAnswerOptions( |
| 869 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 870 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 871 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 872 | } |
| 873 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 874 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 875 | // m= sections (in other words, nothing that involves a map/array). |
| 876 | void ExtractSharedMediaSessionOptions( |
| 877 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 878 | cricket::MediaSessionOptions* session_options) { |
| 879 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 880 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
Mirta Dvornicic | 479a3c0 | 2019-06-04 15:38:50 +0200 | [diff] [blame] | 881 | session_options->raw_packetization_for_video = |
| 882 | rtc_options.raw_packetization_for_video; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 883 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 884 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 885 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 886 | std::unique_ptr<RtcEventLog> event_log, |
| 887 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 888 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 889 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 890 | event_log_ptr_(event_log_.get()), |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 891 | datagram_transport_config_( |
| 892 | field_trial::FindFullName(kDatagramTransportFieldTrial)), |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 893 | datagram_transport_data_channel_config_( |
| 894 | field_trial::FindFullName(kDatagramTransportDataChannelFieldTrial)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 895 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 896 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 897 | remote_streams_(StreamCollection::Create()), |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 898 | call_(std::move(call)), |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 899 | call_ptr_(call_.get()), |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 900 | data_channel_transport_(nullptr), |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 901 | local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 902 | |
| 903 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 904 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 905 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 906 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 907 | // Need to stop transceivers before destroying the stats collector because |
| 908 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 909 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 910 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 911 | transceiver->Stop(); |
| 912 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 913 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 914 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 915 | if (stats_collector_) { |
| 916 | stats_collector_->WaitForPendingRequest(); |
| 917 | stats_collector_ = nullptr; |
| 918 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 919 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 920 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 921 | // the last stats request can still read from the channels. |
| 922 | DestroyAllChannels(); |
| 923 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 924 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 925 | |
| 926 | webrtc_session_desc_factory_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 927 | sctp_factory_.reset(); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 928 | data_channel_transport_invoker_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 929 | transport_controller_.reset(); |
| 930 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 931 | // port_allocator_ lives on the network thread and should be destroyed there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 932 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 933 | RTC_DCHECK_RUN_ON(network_thread()); |
| 934 | port_allocator_.reset(); |
| 935 | }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 936 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 937 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 938 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 939 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 940 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 941 | event_log_.reset(); |
| 942 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 945 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 946 | // Destroy video channels first since they may have a pointer to a voice |
| 947 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 948 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 949 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 950 | DestroyTransceiverChannel(transceiver); |
| 951 | } |
| 952 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 953 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 954 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 955 | DestroyTransceiverChannel(transceiver); |
| 956 | } |
| 957 | } |
| 958 | DestroyDataChannel(); |
| 959 | } |
| 960 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 961 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 962 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 963 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 964 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 965 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 966 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 967 | |
| 968 | RTCError config_error = ValidateConfiguration(configuration); |
| 969 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 970 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 971 | return false; |
| 972 | } |
| 973 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 974 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 975 | RTC_LOG(LS_ERROR) |
| 976 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 977 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 978 | return false; |
| 979 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 980 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 981 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 982 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 983 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 984 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 985 | return false; |
| 986 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 987 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 988 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 989 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 990 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 991 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 992 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 993 | cricket::ServerAddresses stun_servers; |
| 994 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 995 | |
| 996 | RTCErrorType parse_error = |
| 997 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 998 | if (parse_error != RTCErrorType::NONE) { |
| 999 | return false; |
| 1000 | } |
| 1001 | |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 1002 | // Add the turn logging id to all turn servers |
| 1003 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 1004 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 1005 | } |
| 1006 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 1007 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1008 | // there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1009 | const auto pa_result = |
| 1010 | network_thread()->Invoke<InitializePortAllocatorResult>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 1011 | RTC_FROM_HERE, |
| 1012 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1013 | stun_servers, turn_servers, configuration)); |
| 1014 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 1015 | // If initialization was successful, note if STUN or TURN servers |
| 1016 | // were supplied. |
| 1017 | if (!stun_servers.empty()) { |
| 1018 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1019 | } |
| 1020 | if (!turn_servers.empty()) { |
| 1021 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1022 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1023 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1024 | // Send information about IPv4/IPv6 status. |
| 1025 | PeerConnectionAddressFamilyCounter address_family; |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1026 | if (pa_result.enable_ipv6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1027 | address_family = kPeerConnection_IPv6; |
| 1028 | } else { |
| 1029 | address_family = kPeerConnection_IPv4; |
| 1030 | } |
| 1031 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 1032 | kPeerConnectionAddressFamilyCounter_Max); |
| 1033 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1034 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 1035 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1036 | // RFC 3264: The numeric value of the session id and version in the |
| 1037 | // o line MUST be representable with a "64 bit signed integer". |
| 1038 | // Due to this constraint session id |session_id_| is max limited to |
| 1039 | // LLONG_MAX. |
| 1040 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1041 | JsepTransportController::Config config; |
| 1042 | config.redetermine_role_on_ice_restart = |
| 1043 | configuration.redetermine_role_on_ice_restart; |
| 1044 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 1045 | config.disable_encryption = options.disable_encryption; |
| 1046 | config.bundle_policy = configuration.bundle_policy; |
| 1047 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1048 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 1049 | // stub. |
| 1050 | config.crypto_options = configuration.crypto_options.has_value() |
| 1051 | ? *configuration.crypto_options |
| 1052 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1053 | config.transport_observer = this; |
Sebastian Jansson | 1b83a9e | 2019-09-18 18:22:12 +0200 | [diff] [blame] | 1054 | // It's safe to pass |this| and using |rtcp_invoker_| and the |call_| pointer |
| 1055 | // since the JsepTransportController instance is owned by this PeerConnection |
| 1056 | // instance and is destroyed before both |rtcp_invoker_| and the |call_| |
| 1057 | // pointer. |
| 1058 | config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet, |
| 1059 | int64_t packet_time_us) { |
| 1060 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1061 | rtcp_invoker_.AsyncInvoke<void>( |
| 1062 | RTC_FROM_HERE, worker_thread(), [this, packet, packet_time_us] { |
| 1063 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1064 | // |call_| is reset on the worker thread in the PeerConnection |
| 1065 | // destructor, so we check that it's still valid before propagating |
| 1066 | // the packet. |
| 1067 | if (call_) { |
| 1068 | call_->Receiver()->DeliverPacket(MediaType::ANY, packet, |
| 1069 | packet_time_us); |
| 1070 | } |
| 1071 | }); |
| 1072 | }; |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 1073 | config.event_log = event_log_ptr_; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1074 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 1075 | config.enable_external_auth = true; |
| 1076 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 1077 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1078 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1079 | use_datagram_transport_ = datagram_transport_config_.enabled && |
Bjorn A Mellem | 238aab9 | 2019-07-02 11:06:00 -0700 | [diff] [blame] | 1080 | configuration.use_datagram_transport.value_or( |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1081 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1082 | use_datagram_transport_for_data_channels_ = |
| 1083 | datagram_transport_data_channel_config_.enabled && |
| 1084 | configuration.use_datagram_transport_for_data_channels.value_or( |
| 1085 | datagram_transport_data_channel_config_.default_value); |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 1086 | use_datagram_transport_for_data_channels_receive_only_ = |
| 1087 | configuration.use_datagram_transport_for_data_channels_receive_only |
| 1088 | .value_or(datagram_transport_data_channel_config_.receive_only); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1089 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_ || |
| 1090 | configuration.use_media_transport || |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 1091 | configuration.use_media_transport_for_data_channels) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1092 | if (!factory_->media_transport_factory()) { |
| 1093 | RTC_DCHECK(false) |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 1094 | << "PeerConnecton is initialized with use_media_transport = true or " |
| 1095 | << "use_media_transport_for_data_channels = true " |
| 1096 | << "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1097 | return false; |
| 1098 | } |
| 1099 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 1100 | if (configuration.use_media_transport || |
| 1101 | configuration.use_media_transport_for_data_channels) { |
| 1102 | // TODO(bugs.webrtc.org/9719): This check will eventually go away, when |
| 1103 | // RTP media transport is introduced. But until then, we require SDES to |
| 1104 | // be enabled. |
| 1105 | if (configuration.enable_dtls_srtp.has_value() && |
| 1106 | configuration.enable_dtls_srtp.value()) { |
| 1107 | RTC_LOG(LS_WARNING) |
| 1108 | << "When media transport is used, SDES must be enabled. Set " |
| 1109 | "configuration.enable_dtls_srtp to false. use_media_transport=" |
| 1110 | << configuration.use_media_transport |
| 1111 | << ", use_media_transport_for_data_channels=" |
| 1112 | << configuration.use_media_transport_for_data_channels; |
| 1113 | return false; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 1117 | config.use_media_transport_for_media = configuration.use_media_transport; |
| 1118 | config.use_media_transport_for_data_channels = |
| 1119 | configuration.use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1120 | config.use_datagram_transport = use_datagram_transport_; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1121 | config.use_datagram_transport_for_data_channels = |
| 1122 | use_datagram_transport_for_data_channels_; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 1123 | config.use_datagram_transport_for_data_channels_receive_only = |
| 1124 | use_datagram_transport_for_data_channels_receive_only_; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1125 | config.media_transport_factory = factory_->media_transport_factory(); |
| 1126 | } |
| 1127 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1128 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1129 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1130 | if (!configuration.certificates.empty()) { |
| 1131 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1132 | // just picking the first one. The decision should be made based on the DTLS |
| 1133 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1134 | certificate = configuration.certificates[0]; |
| 1135 | } |
| 1136 | |
| 1137 | if (options.disable_encryption) { |
| 1138 | dtls_enabled_ = false; |
| 1139 | } else { |
| 1140 | // Enable DTLS by default if we have an identity store or a certificate. |
| 1141 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
| 1142 | // |configuration| can override the default |dtls_enabled_| value. |
| 1143 | if (configuration.enable_dtls_srtp) { |
| 1144 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
| 1149 | |
| 1150 | if (use_datagram_transport_for_data_channels_) { |
| 1151 | if (configuration.enable_rtp_data_channel) { |
| 1152 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1153 | "use_datagram_transport_for_data_channels are " |
| 1154 | "incompatible and cannot both be set to true"; |
| 1155 | return false; |
| 1156 | } |
| 1157 | if (configuration.enable_dtls_srtp && !*configuration.enable_dtls_srtp) { |
| 1158 | RTC_LOG(LS_INFO) << "Using data channel transport with no fallback"; |
| 1159 | data_channel_type_ = cricket::DCT_DATA_CHANNEL_TRANSPORT; |
| 1160 | } else { |
| 1161 | RTC_LOG(LS_INFO) << "Using data channel transport with fallback to SCTP"; |
| 1162 | data_channel_type_ = cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP; |
| 1163 | config.sctp_factory = sctp_factory_.get(); |
| 1164 | } |
| 1165 | } else if (configuration.use_media_transport_for_data_channels) { |
| 1166 | if (configuration.enable_rtp_data_channel) { |
| 1167 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1168 | "use_media_transport_for_data_channels are " |
| 1169 | "incompatible and cannot both be set to true"; |
| 1170 | return false; |
| 1171 | } |
| 1172 | data_channel_type_ = cricket::DCT_MEDIA_TRANSPORT; |
| 1173 | } else if (configuration.enable_rtp_data_channel) { |
| 1174 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1175 | // set. It takes precendence over the disable_sctp_data_channels |
| 1176 | // PeerConnectionFactoryInterface::Options. |
| 1177 | data_channel_type_ = cricket::DCT_RTP; |
| 1178 | } else { |
| 1179 | // DTLS has to be enabled to use SCTP. |
| 1180 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1181 | data_channel_type_ = cricket::DCT_SCTP; |
| 1182 | config.sctp_factory = sctp_factory_.get(); |
| 1183 | } |
| 1184 | } |
| 1185 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1186 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 1187 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1188 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1189 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1190 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1191 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1192 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1193 | transport_controller_->SignalConnectionState.connect( |
| 1194 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1195 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1196 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1197 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1198 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 1199 | transport_controller_->SignalIceCandidateError.connect( |
| 1200 | this, &PeerConnection::OnTransportControllerCandidateError); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1201 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1202 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1203 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1204 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 1205 | transport_controller_->SignalIceCandidatePairChanged.connect( |
| 1206 | this, &PeerConnection::OnTransportControllerCandidateChanged); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1207 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1208 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1209 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1210 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1211 | configuration_ = configuration; |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1212 | use_media_transport_ = configuration.use_media_transport; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1213 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1214 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1215 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1216 | video_options_.screencast_min_bitrate_kbps = |
| 1217 | configuration.screencast_min_bitrate; |
| 1218 | audio_options_.combined_audio_video_bwe = |
| 1219 | configuration.combined_audio_video_bwe; |
| 1220 | |
| 1221 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1222 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1223 | |
| 1224 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1225 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1226 | |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 1227 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1228 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1229 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 1230 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1231 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1232 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1233 | // Whether the certificate generator/certificate is null or not determines |
| 1234 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1235 | // the right instructions by clearing the variables if needed. |
| 1236 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1237 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1238 | certificate = nullptr; |
| 1239 | } else if (certificate) { |
| 1240 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1241 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1245 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1246 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1247 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1248 | this, &PeerConnection::OnCertificateReady); |
| 1249 | |
| 1250 | if (options.disable_encryption) { |
| 1251 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1252 | } |
| 1253 | |
| 1254 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1255 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1256 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1257 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1258 | // Add default audio/video transceivers for Plan B SDP. |
| 1259 | if (!IsUnifiedPlan()) { |
| 1260 | transceivers_.push_back( |
| 1261 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1262 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1263 | transceivers_.push_back( |
| 1264 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1265 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1266 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1267 | int delay_ms = |
| 1268 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1269 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1270 | MSG_REPORT_USAGE_PATTERN, nullptr); |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 1271 | |
| 1272 | if (dependencies.video_bitrate_allocator_factory) { |
| 1273 | video_bitrate_allocator_factory_ = |
| 1274 | std::move(dependencies.video_bitrate_allocator_factory); |
| 1275 | } else { |
| 1276 | video_bitrate_allocator_factory_ = |
| 1277 | CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1278 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
| 1281 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1282 | RTCError PeerConnection::ValidateConfiguration( |
| 1283 | const RTCConfiguration& config) const { |
| 1284 | if (config.ice_regather_interval_range && |
| 1285 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1286 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1287 | "ice_regather_interval_range specified but continual " |
| 1288 | "gathering policy is GATHER_ONCE"); |
| 1289 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1290 | auto result = |
| 1291 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1292 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1295 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1296 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1297 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1298 | "Plan SdpSemantics. Please use GetSenders " |
| 1299 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1300 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1303 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1304 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1305 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1306 | "Plan SdpSemantics. Please use GetReceivers " |
| 1307 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1308 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1311 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1312 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1313 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1314 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1315 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1316 | if (IsClosed()) { |
| 1317 | return false; |
| 1318 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1319 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1320 | return false; |
| 1321 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1322 | |
| 1323 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1324 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1325 | observer->SignalAudioTrackAdded.connect(this, |
| 1326 | &PeerConnection::OnAudioTrackAdded); |
| 1327 | observer->SignalAudioTrackRemoved.connect( |
| 1328 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1329 | observer->SignalVideoTrackAdded.connect(this, |
| 1330 | &PeerConnection::OnVideoTrackAdded); |
| 1331 | observer->SignalVideoTrackRemoved.connect( |
| 1332 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1333 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1334 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1335 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1336 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1337 | } |
| 1338 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1339 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1342 | stats_->AddStream(local_stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1343 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1344 | return true; |
| 1345 | } |
| 1346 | |
| 1347 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1348 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1349 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1350 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1351 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1352 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1353 | if (!IsClosed()) { |
| 1354 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1355 | RemoveAudioTrack(track.get(), local_stream); |
| 1356 | } |
| 1357 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1358 | RemoveVideoTrack(track.get(), local_stream); |
| 1359 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1360 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1361 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1362 | stream_observers_.erase( |
| 1363 | std::remove_if( |
| 1364 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1365 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1366 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1367 | }), |
| 1368 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1369 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1370 | if (IsClosed()) { |
| 1371 | return; |
| 1372 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1373 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1376 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1377 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1378 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1379 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1380 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1381 | if (!track) { |
| 1382 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1383 | } |
| 1384 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1385 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1386 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1387 | "Track has invalid kind: " + track->kind()); |
| 1388 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1389 | if (IsClosed()) { |
| 1390 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1391 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1392 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1393 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1394 | LOG_AND_RETURN_ERROR( |
| 1395 | RTCErrorType::INVALID_PARAMETER, |
| 1396 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1397 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1398 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1399 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1400 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1401 | if (sender_or_error.ok()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1402 | UpdateNegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1403 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1404 | } |
| 1405 | return sender_or_error; |
| 1406 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1407 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1408 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1409 | PeerConnection::AddTrackPlanB( |
| 1410 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1411 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1412 | if (stream_ids.size() > 1u) { |
| 1413 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1414 | "AddTrack with more than one stream is not " |
| 1415 | "supported with Plan B semantics."); |
| 1416 | } |
| 1417 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1418 | if (adjusted_stream_ids.empty()) { |
| 1419 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1420 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1421 | cricket::MediaType media_type = |
| 1422 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1423 | ? cricket::MEDIA_TYPE_AUDIO |
| 1424 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1425 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1426 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1427 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1428 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1429 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1430 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1431 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1432 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1433 | if (sender_info) { |
| 1434 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1435 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1436 | } else { |
| 1437 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1438 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1439 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1440 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1441 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1442 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1443 | if (sender_info) { |
| 1444 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1445 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1446 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1447 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1448 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1449 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1450 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1451 | PeerConnection::AddTrackUnifiedPlan( |
| 1452 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1453 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1454 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1455 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1456 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1457 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1458 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1459 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1460 | transceiver->internal()->set_direction( |
| 1461 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1462 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1463 | transceiver->internal()->set_direction( |
| 1464 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1465 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1466 | transceiver->sender()->SetTrack(track); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1467 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1468 | } else { |
| 1469 | cricket::MediaType media_type = |
| 1470 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1471 | ? cricket::MEDIA_TYPE_AUDIO |
| 1472 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1473 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1474 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1475 | std::string sender_id = track->id(); |
| 1476 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1477 | // This can happen if this is the second time AddTrack has created a sender |
| 1478 | // for this track. |
| 1479 | if (FindSenderById(sender_id)) { |
| 1480 | sender_id = rtc::CreateRandomUuid(); |
| 1481 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1482 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1483 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1484 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1485 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1486 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1487 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1488 | return transceiver->sender(); |
| 1489 | } |
| 1490 | |
| 1491 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1492 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1493 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1494 | RTC_DCHECK(track); |
| 1495 | for (auto transceiver : transceivers_) { |
| 1496 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1497 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1498 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1499 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1500 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1501 | return transceiver; |
| 1502 | } |
| 1503 | } |
| 1504 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1508 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1509 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1512 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1513 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1514 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1515 | if (!sender) { |
| 1516 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1517 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1518 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1519 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1520 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1521 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1522 | if (IsUnifiedPlan()) { |
| 1523 | auto transceiver = FindTransceiverBySender(sender); |
| 1524 | if (!transceiver || !sender->track()) { |
| 1525 | return RTCError::OK(); |
| 1526 | } |
| 1527 | sender->SetTrack(nullptr); |
| 1528 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1529 | transceiver->internal()->set_direction( |
| 1530 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1531 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1532 | transceiver->internal()->set_direction( |
| 1533 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1534 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1535 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1536 | bool removed; |
| 1537 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1538 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1539 | } else { |
| 1540 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1541 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1542 | } |
| 1543 | if (!removed) { |
| 1544 | LOG_AND_RETURN_ERROR( |
| 1545 | RTCErrorType::INVALID_PARAMETER, |
| 1546 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1547 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1548 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1549 | UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1550 | return RTCError::OK(); |
| 1551 | } |
| 1552 | |
| 1553 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1554 | PeerConnection::FindTransceiverBySender( |
| 1555 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1556 | for (auto transceiver : transceivers_) { |
| 1557 | if (transceiver->sender() == sender) { |
| 1558 | return transceiver; |
| 1559 | } |
| 1560 | } |
| 1561 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1564 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1565 | PeerConnection::AddTransceiver( |
| 1566 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1567 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1568 | } |
| 1569 | |
| 1570 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1571 | PeerConnection::AddTransceiver( |
| 1572 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1573 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1574 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1575 | RTC_CHECK(IsUnifiedPlan()) |
| 1576 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1577 | if (!track) { |
| 1578 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1579 | } |
| 1580 | cricket::MediaType media_type; |
| 1581 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1582 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1583 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1584 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1585 | } else { |
| 1586 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1587 | "Track kind is not audio or video"); |
| 1588 | } |
| 1589 | return AddTransceiver(media_type, track, init); |
| 1590 | } |
| 1591 | |
| 1592 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1593 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1594 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1595 | } |
| 1596 | |
| 1597 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1598 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1599 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1600 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1601 | RTC_CHECK(IsUnifiedPlan()) |
| 1602 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1603 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1604 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1605 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1606 | "media type is not audio or video"); |
| 1607 | } |
| 1608 | return AddTransceiver(media_type, nullptr, init); |
| 1609 | } |
| 1610 | |
| 1611 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1612 | PeerConnection::AddTransceiver( |
| 1613 | cricket::MediaType media_type, |
| 1614 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1615 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1616 | bool update_negotiation_needed) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1617 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1618 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1619 | if (track) { |
| 1620 | RTC_DCHECK_EQ(media_type, |
| 1621 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1622 | ? cricket::MEDIA_TYPE_AUDIO |
| 1623 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1624 | } |
| 1625 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 1626 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1627 | init.send_encodings.size(), 0, 7, 8); |
| 1628 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1629 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1630 | [](const RtpEncodingParameters& encoding) { |
| 1631 | return !encoding.rid.empty(); |
| 1632 | }); |
| 1633 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1634 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1635 | RTCErrorType::INVALID_PARAMETER, |
| 1636 | "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] | 1637 | } |
| 1638 | |
Amit Hilbuch | f477040 | 2019-04-08 14:11:57 -0700 | [diff] [blame] | 1639 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1640 | [](const RtpEncodingParameters& encoding) { |
| 1641 | return !IsLegalRsidName(encoding.rid); |
| 1642 | })) { |
| 1643 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1644 | "Invalid RID value provided."); |
| 1645 | } |
| 1646 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1647 | if (absl::c_any_of(init.send_encodings, |
| 1648 | [](const RtpEncodingParameters& encoding) { |
| 1649 | return encoding.ssrc.has_value(); |
| 1650 | })) { |
| 1651 | LOG_AND_RETURN_ERROR( |
| 1652 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1653 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | RtpParameters parameters; |
| 1657 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1658 | |
| 1659 | // Encodings are dropped from the tail if too many are provided. |
| 1660 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1661 | parameters.encodings.erase( |
| 1662 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1663 | parameters.encodings.end()); |
| 1664 | } |
| 1665 | |
| 1666 | // Single RID should be removed. |
| 1667 | if (parameters.encodings.size() == 1 && |
| 1668 | !parameters.encodings[0].rid.empty()) { |
| 1669 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1670 | parameters.encodings[0].rid.clear(); |
| 1671 | } |
| 1672 | |
| 1673 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1674 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1675 | rtc::UniqueStringGenerator rid_generator; |
| 1676 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1677 | encoding.rid = rid_generator(); |
| 1678 | } |
| 1679 | } |
| 1680 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1681 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1682 | LOG_AND_RETURN_ERROR( |
| 1683 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1684 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1685 | } |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1686 | |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 1687 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1688 | if (!result.ok()) { |
| 1689 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1690 | } |
| 1691 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1692 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1693 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1694 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1695 | // that sender ID is already in use. |
| 1696 | std::string sender_id = |
| 1697 | (track && !FindSenderById(track->id()) ? track->id() |
| 1698 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1699 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1700 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1701 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1702 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1703 | transceiver->internal()->set_direction(init.direction); |
| 1704 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1705 | if (update_negotiation_needed) { |
| 1706 | UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1707 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1708 | |
| 1709 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1710 | } |
| 1711 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1712 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1713 | PeerConnection::CreateSender( |
| 1714 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1715 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1716 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1717 | const std::vector<std::string>& stream_ids, |
| 1718 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1719 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1720 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1721 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1722 | RTC_DCHECK(!track || |
| 1723 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1724 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1725 | signaling_thread(), |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1726 | AudioRtpSender::Create(worker_thread(), id, stats_.get(), this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1727 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1728 | } else { |
| 1729 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1730 | RTC_DCHECK(!track || |
| 1731 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1732 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1733 | signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1734 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1735 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1736 | bool set_track_succeeded = sender->SetTrack(track); |
| 1737 | RTC_DCHECK(set_track_succeeded); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1738 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1739 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1740 | return sender; |
| 1741 | } |
| 1742 | |
| 1743 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1744 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1745 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1746 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1747 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1748 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1749 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1750 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1751 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1752 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1753 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1754 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1755 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1756 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1757 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1758 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1759 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1760 | return receiver; |
| 1761 | } |
| 1762 | |
| 1763 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1764 | PeerConnection::CreateAndAddTransceiver( |
| 1765 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1766 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1767 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1768 | // Ensure that the new sender does not have an ID that is already in use by |
| 1769 | // another sender. |
| 1770 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1771 | // could be invalid, but should not cause a crash). |
| 1772 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1773 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 1774 | signaling_thread(), |
| 1775 | new RtpTransceiver(sender, receiver, channel_manager())); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1776 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1777 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1778 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1779 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1782 | void PeerConnection::OnNegotiationNeeded() { |
| 1783 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1784 | RTC_DCHECK(!IsClosed()); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1785 | UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1786 | } |
| 1787 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1788 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1789 | const std::string& kind, |
| 1790 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1791 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1792 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1793 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1794 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1795 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1796 | if (IsClosed()) { |
| 1797 | return nullptr; |
| 1798 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1799 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1800 | // Internally we need to have one stream with Plan B semantics, so we |
| 1801 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1802 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1803 | if (stream_id.empty()) { |
| 1804 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1805 | RTC_LOG(LS_INFO) |
| 1806 | << "No stream_id specified for sender. Generated stream ID: " |
| 1807 | << stream_ids[0]; |
| 1808 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1809 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1812 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1813 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1814 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1815 | auto audio_sender = AudioRtpSender::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1816 | worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1817 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1818 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1819 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1820 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1821 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1822 | auto video_sender = |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1823 | VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1824 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1825 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1826 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1827 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1828 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1829 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1830 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1831 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1832 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1833 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1834 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1835 | } |
| 1836 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1837 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1838 | const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 1839 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1840 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1841 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1842 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1843 | } |
| 1844 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1847 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1848 | PeerConnection::GetSendersInternal() const { |
| 1849 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1850 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1851 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1852 | auto senders = transceiver->internal()->senders(); |
| 1853 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1854 | } |
| 1855 | return all_senders; |
| 1856 | } |
| 1857 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1858 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1859 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 1860 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1861 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1862 | for (const auto& receiver : GetReceiversInternal()) { |
| 1863 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1864 | } |
| 1865 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1868 | std::vector< |
| 1869 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1870 | PeerConnection::GetReceiversInternal() const { |
| 1871 | std::vector< |
| 1872 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1873 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1874 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1875 | auto receivers = transceiver->internal()->receivers(); |
| 1876 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1877 | receivers.end()); |
| 1878 | } |
| 1879 | return all_receivers; |
| 1880 | } |
| 1881 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1882 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1883 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1884 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1885 | RTC_CHECK(IsUnifiedPlan()) |
| 1886 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1887 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1888 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1889 | all_transceivers.push_back(transceiver); |
| 1890 | } |
| 1891 | return all_transceivers; |
| 1892 | } |
| 1893 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1894 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1895 | MediaStreamTrackInterface* track, |
| 1896 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1897 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1898 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1899 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1900 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1901 | return false; |
| 1902 | } |
| 1903 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1904 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1905 | // The StatsCollector is used to tell if a track is valid because it may |
| 1906 | // remember tracks that the PeerConnection previously removed. |
| 1907 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1908 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1909 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1910 | return false; |
| 1911 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1912 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1913 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1914 | return true; |
| 1915 | } |
| 1916 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1917 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1918 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1919 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1920 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1921 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1922 | stats_collector_->GetStatsReport(callback); |
| 1923 | } |
| 1924 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1925 | void PeerConnection::GetStats( |
| 1926 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1927 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1928 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1929 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1930 | RTC_DCHECK(callback); |
| 1931 | RTC_DCHECK(stats_collector_); |
| 1932 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1933 | if (selector) { |
| 1934 | for (const auto& proxy_transceiver : transceivers_) { |
| 1935 | for (const auto& proxy_sender : |
| 1936 | proxy_transceiver->internal()->senders()) { |
| 1937 | if (proxy_sender == selector) { |
| 1938 | internal_sender = proxy_sender->internal(); |
| 1939 | break; |
| 1940 | } |
| 1941 | } |
| 1942 | if (internal_sender) |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1946 | // 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] | 1947 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1948 | // PeerConnection). This means that "all the stats objects representing the |
| 1949 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1950 | // produces an empty stats report. |
| 1951 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1952 | } |
| 1953 | |
| 1954 | void PeerConnection::GetStats( |
| 1955 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1956 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1957 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1958 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1959 | RTC_DCHECK(callback); |
| 1960 | RTC_DCHECK(stats_collector_); |
| 1961 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1962 | if (selector) { |
| 1963 | for (const auto& proxy_transceiver : transceivers_) { |
| 1964 | for (const auto& proxy_receiver : |
| 1965 | proxy_transceiver->internal()->receivers()) { |
| 1966 | if (proxy_receiver == selector) { |
| 1967 | internal_receiver = proxy_receiver->internal(); |
| 1968 | break; |
| 1969 | } |
| 1970 | } |
| 1971 | if (internal_receiver) |
| 1972 | break; |
| 1973 | } |
| 1974 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1975 | // 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] | 1976 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1977 | // the PeerConnection). This means that "all the stats objects representing |
| 1978 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1979 | // selector produces an empty stats report. |
| 1980 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1981 | } |
| 1982 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1983 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1984 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1985 | return signaling_state_; |
| 1986 | } |
| 1987 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1988 | PeerConnectionInterface::IceConnectionState |
| 1989 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1990 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1991 | return ice_connection_state_; |
| 1992 | } |
| 1993 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1994 | PeerConnectionInterface::IceConnectionState |
| 1995 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1996 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1997 | return standardized_ice_connection_state_; |
| 1998 | } |
| 1999 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 2000 | PeerConnectionInterface::PeerConnectionState |
| 2001 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2002 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 2003 | return connection_state_; |
| 2004 | } |
| 2005 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2006 | PeerConnectionInterface::IceGatheringState |
| 2007 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2008 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2009 | return ice_gathering_state_; |
| 2010 | } |
| 2011 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2012 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2013 | const std::string& label, |
| 2014 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 10:17:47 +0100 | [diff] [blame] | 2015 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2016 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2017 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2018 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2019 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 2020 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 2021 | if (config) { |
| 2022 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 2023 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2024 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2025 | InternalCreateDataChannel(label, internal_config.get())); |
| 2026 | if (!channel.get()) { |
| 2027 | return nullptr; |
| 2028 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2029 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2030 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 2031 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2032 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2033 | UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2034 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2035 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2036 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 2037 | } |
| 2038 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 2039 | void PeerConnection::RestartIce() { |
| 2040 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2041 | local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions( |
| 2042 | current_local_description_.get(), pending_local_description_.get()); |
| 2043 | UpdateNegotiationNeeded(); |
| 2044 | } |
| 2045 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2046 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 2047 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2048 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2049 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2050 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2051 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2052 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 2053 | return; |
| 2054 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2055 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2056 | if (IsClosed()) { |
| 2057 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2058 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2059 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2060 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2061 | return; |
| 2062 | } |
| 2063 | |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 2064 | // If a session error has occurred the PeerConnection is in a possibly |
| 2065 | // inconsistent state so fail right away. |
| 2066 | if (session_error() != SessionError::kNone) { |
| 2067 | std::string error_message = GetSessionErrorMsg(); |
| 2068 | RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message; |
| 2069 | PostCreateSessionDescriptionFailure( |
| 2070 | observer, |
| 2071 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2072 | return; |
| 2073 | } |
| 2074 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2075 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2076 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2077 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2078 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2079 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2080 | return; |
| 2081 | } |
| 2082 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2083 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 2084 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 2085 | if (IsUnifiedPlan()) { |
| 2086 | RTCError error = HandleLegacyOfferOptions(options); |
| 2087 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2088 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2089 | return; |
| 2090 | } |
| 2091 | } |
| 2092 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2093 | cricket::MediaSessionOptions session_options; |
| 2094 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2095 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2098 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 2099 | const RTCOfferAnswerOptions& options) { |
| 2100 | RTC_DCHECK(IsUnifiedPlan()); |
| 2101 | |
| 2102 | if (options.offer_to_receive_audio == 0) { |
| 2103 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2104 | cricket::MEDIA_TYPE_AUDIO); |
| 2105 | } else if (options.offer_to_receive_audio == 1) { |
| 2106 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 2107 | } else if (options.offer_to_receive_audio > 1) { |
| 2108 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2109 | "offer_to_receive_audio > 1 is not supported."); |
| 2110 | } |
| 2111 | |
| 2112 | if (options.offer_to_receive_video == 0) { |
| 2113 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2114 | cricket::MEDIA_TYPE_VIDEO); |
| 2115 | } else if (options.offer_to_receive_video == 1) { |
| 2116 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 2117 | } else if (options.offer_to_receive_video > 1) { |
| 2118 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2119 | "offer_to_receive_video > 1 is not supported."); |
| 2120 | } |
| 2121 | |
| 2122 | return RTCError::OK(); |
| 2123 | } |
| 2124 | |
| 2125 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2126 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2127 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2128 | RtpTransceiverDirection new_direction = |
| 2129 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 2130 | if (new_direction != transceiver->direction()) { |
| 2131 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 2132 | << " transceiver (MID=" |
| 2133 | << transceiver->mid().value_or("<not set>") << ") from " |
| 2134 | << RtpTransceiverDirectionToString( |
| 2135 | transceiver->direction()) |
| 2136 | << " to " |
| 2137 | << RtpTransceiverDirectionToString(new_direction) |
| 2138 | << " since CreateOffer specified offer_to_receive=0"; |
| 2139 | transceiver->internal()->set_direction(new_direction); |
| 2140 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 2145 | cricket::MediaType media_type) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 2146 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2147 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2148 | RTC_LOG(LS_INFO) |
| 2149 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 2150 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2151 | RtpTransceiverInit init; |
| 2152 | init.direction = RtpTransceiverDirection::kRecvOnly; |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2153 | AddTransceiver(media_type, nullptr, init, |
| 2154 | /*update_negotiation_needed=*/false); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2159 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 2160 | std::vector< |
| 2161 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2162 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2163 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2164 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2165 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 2166 | receiving_transceivers.push_back(transceiver); |
| 2167 | } |
| 2168 | } |
| 2169 | return receiving_transceivers; |
| 2170 | } |
| 2171 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2172 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 2173 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2174 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2175 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2176 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2177 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 2181 | // If a session error has occurred the PeerConnection is in a possibly |
| 2182 | // inconsistent state so fail right away. |
| 2183 | if (session_error() != SessionError::kNone) { |
| 2184 | std::string error_message = GetSessionErrorMsg(); |
| 2185 | RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message; |
| 2186 | PostCreateSessionDescriptionFailure( |
| 2187 | observer, |
| 2188 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2189 | return; |
| 2190 | } |
| 2191 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2192 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 2193 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 2194 | std::string error = |
| 2195 | "PeerConnection cannot create an answer in a state other than " |
| 2196 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2197 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2198 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2199 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2200 | return; |
| 2201 | } |
| 2202 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2203 | // The remote description should be set if we're in the right state. |
| 2204 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2205 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2206 | if (IsUnifiedPlan()) { |
| 2207 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2208 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2209 | "supported with Unified Plan semantics. Use the " |
| 2210 | "RtpTransceiver API instead."; |
| 2211 | } |
| 2212 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2213 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2214 | "supported with Unified Plan semantics. Use the " |
| 2215 | "RtpTransceiver API instead."; |
| 2216 | } |
| 2217 | } |
| 2218 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2219 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2220 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2221 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2222 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | void PeerConnection::SetLocalDescription( |
| 2226 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2227 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2228 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2229 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2230 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2231 | // The SetLocalDescription contract is that we take ownership of the session |
| 2232 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 2233 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 2234 | // description as a unique_ptr argument to formalize this agreement. |
| 2235 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 2236 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2237 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2238 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2239 | return; |
| 2240 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2241 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2242 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2243 | PostSetSessionDescriptionFailure( |
| 2244 | observer, |
| 2245 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2246 | return; |
| 2247 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2248 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2249 | // If a session error has occurred the PeerConnection is in a possibly |
| 2250 | // inconsistent state so fail right away. |
| 2251 | if (session_error() != SessionError::kNone) { |
| 2252 | std::string error_message = GetSessionErrorMsg(); |
| 2253 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2254 | PostSetSessionDescriptionFailure( |
| 2255 | observer, |
| 2256 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2257 | return; |
| 2258 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2259 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2260 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2261 | if (!error.ok()) { |
| 2262 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2263 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2264 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2265 | PostSetSessionDescriptionFailure( |
| 2266 | observer, |
| 2267 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2268 | return; |
| 2269 | } |
| 2270 | |
| 2271 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2272 | // which may destroy it before returning. |
| 2273 | const SdpType type = desc->GetType(); |
| 2274 | |
| 2275 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2276 | // |desc| may be destroyed at this point. |
| 2277 | |
| 2278 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2279 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2280 | // inconsistent state, so act conservatively here and set the session error |
| 2281 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2282 | SetSessionError(SessionError::kContent, error.message()); |
| 2283 | std::string error_message = |
| 2284 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2285 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2286 | PostSetSessionDescriptionFailure( |
| 2287 | observer, |
| 2288 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2289 | return; |
| 2290 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2291 | RTC_DCHECK(local_description()); |
| 2292 | |
| 2293 | PostSetSessionDescriptionSuccess(observer); |
| 2294 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 2295 | // MaybeStartGathering needs to be called after posting |
| 2296 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2297 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2298 | transport_controller_->MaybeStartGathering(); |
| 2299 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2300 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2301 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2302 | // MaybeStartGathering... |
| 2303 | network_thread()->Invoke<void>( |
| 2304 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2305 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2306 | // Make UMA notes about what was agreed to. |
| 2307 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2308 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2309 | |
| 2310 | if (IsUnifiedPlan()) { |
| 2311 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2312 | UpdateNegotiationNeeded(); |
| 2313 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2314 | is_negotiation_needed_) { |
| 2315 | Observer()->OnRenegotiationNeeded(); |
| 2316 | } |
| 2317 | } |
| 2318 | |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 2319 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | RTCError PeerConnection::ApplyLocalDescription( |
| 2323 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2324 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2325 | RTC_DCHECK(desc); |
| 2326 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2327 | // Update stats here so that we have the most recent stats for tracks and |
| 2328 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2329 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2330 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2331 | // Take a reference to the old local description since it's used below to |
| 2332 | // compare against the new local description. When setting the new local |
| 2333 | // description, grab ownership of the replaced session description in case it |
| 2334 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2335 | // of the method. |
| 2336 | const SessionDescriptionInterface* old_local_description = |
| 2337 | local_description(); |
| 2338 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2339 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2340 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2341 | replaced_local_description = pending_local_description_ |
| 2342 | ? std::move(pending_local_description_) |
| 2343 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2344 | current_local_description_ = std::move(desc); |
| 2345 | pending_local_description_ = nullptr; |
| 2346 | current_remote_description_ = std::move(pending_remote_description_); |
| 2347 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2348 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2349 | pending_local_description_ = std::move(desc); |
| 2350 | } |
| 2351 | // The session description to apply now must be accessed by |
| 2352 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2353 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2354 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 2355 | // Report statistics about any use of simulcast. |
| 2356 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 2357 | *local_description()->description()); |
| 2358 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2359 | if (!is_caller_) { |
| 2360 | if (remote_description()) { |
| 2361 | // Remote description was applied first, so this PC is the callee. |
| 2362 | is_caller_ = false; |
| 2363 | } else { |
| 2364 | // Local description is applied first, so this PC is the caller. |
| 2365 | is_caller_ = true; |
| 2366 | } |
| 2367 | } |
| 2368 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2369 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2370 | if (!error.ok()) { |
| 2371 | return error; |
| 2372 | } |
| 2373 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2374 | if (IsUnifiedPlan()) { |
| 2375 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2376 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2377 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2378 | if (!error.ok()) { |
| 2379 | return error; |
| 2380 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2381 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2382 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2383 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2384 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2385 | // Note that code paths that don't set MID won't be able to use |
| 2386 | // information about DTLS transports. |
| 2387 | if (transceiver->mid()) { |
| 2388 | auto dtls_transport = |
| 2389 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2390 | transceiver->internal()->sender_internal()->set_transport( |
| 2391 | dtls_transport); |
| 2392 | transceiver->internal()->receiver_internal()->set_transport( |
| 2393 | dtls_transport); |
| 2394 | } |
| 2395 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2396 | const ContentInfo* content = |
| 2397 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2398 | if (!content) { |
| 2399 | continue; |
| 2400 | } |
| 2401 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2402 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2403 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2404 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2405 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2406 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2407 | // "recvonly", process the removal of a remote track for the media |
| 2408 | // description, given transceiver, removeList, and muteTracks. |
| 2409 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2410 | (transceiver->internal()->fired_direction() && |
| 2411 | RtpTransceiverDirectionHasRecv( |
| 2412 | *transceiver->internal()->fired_direction()))) { |
| 2413 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2414 | &removed_streams); |
| 2415 | } |
| 2416 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2417 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2418 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2419 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2420 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2421 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2422 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2423 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2424 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2425 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2426 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2427 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2428 | } |
| 2429 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2430 | // Media channels will be created only when offer is set. These may use new |
| 2431 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2432 | if (type == SdpType::kOffer) { |
| 2433 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2434 | // description is applied. Restore back to old description. |
| 2435 | RTCError error = CreateChannels(*local_description()->description()); |
| 2436 | if (!error.ok()) { |
| 2437 | return error; |
| 2438 | } |
| 2439 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2440 | // Remove unused channels if MediaContentDescription is rejected. |
| 2441 | RemoveUnusedChannels(local_description()->description()); |
| 2442 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2443 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2444 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2445 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2446 | if (!error.ok()) { |
| 2447 | return error; |
| 2448 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2449 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2450 | if (remote_description()) { |
| 2451 | // Now that we have a local description, we can push down remote candidates. |
| 2452 | UseCandidatesInSessionDescription(remote_description()); |
| 2453 | } |
| 2454 | |
| 2455 | pending_ice_restarts_.clear(); |
| 2456 | if (session_error() != SessionError::kNone) { |
| 2457 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2458 | } |
| 2459 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2460 | // If setting the description decided our SSL role, allocate any necessary |
| 2461 | // SCTP sids. |
| 2462 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2463 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2464 | AllocateSctpSids(role); |
| 2465 | } |
| 2466 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2467 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2468 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2469 | const ContentInfo* content = |
| 2470 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2471 | if (!content) { |
| 2472 | continue; |
| 2473 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2474 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2475 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2476 | // 0 is a special value meaning "this sender has no associated send |
| 2477 | // stream". Need to call this so the sender won't attempt to configure |
| 2478 | // a no longer existing stream and run into DCHECKs in the lower |
| 2479 | // layers. |
| 2480 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2481 | } else { |
| 2482 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2483 | const std::vector<StreamParams>& streams = channel->local_streams(); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 2484 | transceiver->internal()->sender_internal()->set_stream_ids( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2485 | streams[0].stream_ids()); |
| 2486 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2487 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2488 | } |
| 2489 | } |
| 2490 | } else { |
| 2491 | // Plan B semantics. |
| 2492 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2493 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2494 | // local session description. |
| 2495 | const cricket::ContentInfo* audio_content = |
| 2496 | GetFirstAudioContent(local_description()->description()); |
| 2497 | if (audio_content) { |
| 2498 | if (audio_content->rejected) { |
| 2499 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2500 | } else { |
| 2501 | const cricket::AudioContentDescription* audio_desc = |
| 2502 | audio_content->media_description()->as_audio(); |
| 2503 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2504 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2505 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2506 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2507 | const cricket::ContentInfo* video_content = |
| 2508 | GetFirstVideoContent(local_description()->description()); |
| 2509 | if (video_content) { |
| 2510 | if (video_content->rejected) { |
| 2511 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2512 | } else { |
| 2513 | const cricket::VideoContentDescription* video_desc = |
| 2514 | video_content->media_description()->as_video(); |
| 2515 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2516 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2517 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2521 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2522 | if (data_content) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 2523 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 2524 | data_content->media_description()->as_rtp_data(); |
| 2525 | // rtp_data_desc will be null if this is an SCTP description. |
| 2526 | if (rtp_data_desc) { |
| 2527 | UpdateLocalRtpDataChannels(rtp_data_desc->streams()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 2531 | if (type == SdpType::kAnswer && |
| 2532 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 2533 | *current_local_description_)) { |
| 2534 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 2535 | } |
| 2536 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2537 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2540 | // The SDP parser used to populate these values by default for the 'content |
| 2541 | // name' if an a=mid line was absent. |
| 2542 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2543 | switch (media_type) { |
| 2544 | case cricket::MEDIA_TYPE_AUDIO: |
| 2545 | return cricket::CN_AUDIO; |
| 2546 | case cricket::MEDIA_TYPE_VIDEO: |
| 2547 | return cricket::CN_VIDEO; |
| 2548 | case cricket::MEDIA_TYPE_DATA: |
| 2549 | return cricket::CN_DATA; |
| 2550 | } |
| 2551 | RTC_NOTREACHED(); |
| 2552 | return ""; |
| 2553 | } |
| 2554 | |
| 2555 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2556 | cricket::SessionDescription* new_remote_description) { |
| 2557 | RTC_DCHECK(new_remote_description); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2558 | const cricket::ContentInfos no_infos; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2559 | const cricket::ContentInfos& local_contents = |
| 2560 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2561 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2562 | const cricket::ContentInfos& remote_contents = |
| 2563 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2564 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2565 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2566 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2567 | if (!content.name.empty()) { |
| 2568 | continue; |
| 2569 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2570 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2571 | absl::string_view source_explanation; |
| 2572 | if (IsUnifiedPlan()) { |
| 2573 | if (i < local_contents.size()) { |
| 2574 | new_mid = local_contents[i].name; |
| 2575 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2576 | } else if (i < remote_contents.size()) { |
| 2577 | new_mid = remote_contents[i].name; |
| 2578 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2579 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2580 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2581 | source_explanation = "generated just now"; |
| 2582 | } |
| 2583 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2584 | new_mid = std::string( |
| 2585 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2586 | source_explanation = "to match pre-existing behavior"; |
| 2587 | } |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2588 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2589 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2590 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2591 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2592 | << " is missing an a=mid line. Filling in the value '" |
| 2593 | << new_mid << "' " << source_explanation << "."; |
| 2594 | } |
| 2595 | } |
| 2596 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2597 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2598 | SetSessionDescriptionObserver* observer, |
| 2599 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2600 | SetRemoteDescription( |
| 2601 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2602 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2603 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2604 | } |
| 2605 | |
| 2606 | void PeerConnection::SetRemoteDescription( |
| 2607 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2608 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2609 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2610 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2611 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2612 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2613 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2614 | return; |
| 2615 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2616 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2617 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2618 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2619 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2620 | return; |
| 2621 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2622 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2623 | // If a session error has occurred the PeerConnection is in a possibly |
| 2624 | // inconsistent state so fail right away. |
| 2625 | if (session_error() != SessionError::kNone) { |
| 2626 | std::string error_message = GetSessionErrorMsg(); |
| 2627 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2628 | observer->OnSetRemoteDescriptionComplete( |
| 2629 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2630 | return; |
| 2631 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2632 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2633 | if (desc->GetType() == SdpType::kOffer) { |
| 2634 | // Report to UMA the format of the received offer. |
| 2635 | ReportSdpFormatReceived(*desc); |
| 2636 | } |
| 2637 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2638 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 2639 | // points. |
| 2640 | FillInMissingRemoteMids(desc->description()); |
| 2641 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2642 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2643 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2644 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2645 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2646 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2647 | observer->OnSetRemoteDescriptionComplete( |
| 2648 | RTCError(error.type(), std::move(error_message))); |
| 2649 | return; |
| 2650 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2651 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2652 | // Grab the description type before moving ownership to |
| 2653 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2654 | const SdpType type = desc->GetType(); |
| 2655 | |
| 2656 | error = ApplyRemoteDescription(std::move(desc)); |
| 2657 | // |desc| may be destroyed at this point. |
| 2658 | |
| 2659 | if (!error.ok()) { |
| 2660 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2661 | // inconsistent state, so act conservatively here and set the session error |
| 2662 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2663 | SetSessionError(SessionError::kContent, error.message()); |
| 2664 | std::string error_message = |
| 2665 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2666 | RTC_LOG(LS_ERROR) << error_message; |
| 2667 | observer->OnSetRemoteDescriptionComplete( |
| 2668 | RTCError(error.type(), std::move(error_message))); |
| 2669 | return; |
| 2670 | } |
| 2671 | RTC_DCHECK(remote_description()); |
| 2672 | |
| 2673 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2674 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2675 | // MaybeStartGathering... |
| 2676 | network_thread()->Invoke<void>( |
| 2677 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2678 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2679 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2680 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2681 | } |
| 2682 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2683 | if (IsUnifiedPlan()) { |
| 2684 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2685 | UpdateNegotiationNeeded(); |
| 2686 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2687 | is_negotiation_needed_) { |
| 2688 | Observer()->OnRenegotiationNeeded(); |
| 2689 | } |
| 2690 | } |
| 2691 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2692 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 2693 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2697 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2698 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2699 | RTC_DCHECK(desc); |
| 2700 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2701 | // Update stats here so that we have the most recent stats for tracks and |
| 2702 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2703 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2704 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2705 | // Take a reference to the old remote description since it's used below to |
| 2706 | // compare against the new remote description. When setting the new remote |
| 2707 | // description, grab ownership of the replaced session description in case it |
| 2708 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2709 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2710 | const SessionDescriptionInterface* old_remote_description = |
| 2711 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2712 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2713 | SdpType type = desc->GetType(); |
| 2714 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2715 | replaced_remote_description = pending_remote_description_ |
| 2716 | ? std::move(pending_remote_description_) |
| 2717 | : std::move(current_remote_description_); |
| 2718 | current_remote_description_ = std::move(desc); |
| 2719 | pending_remote_description_ = nullptr; |
| 2720 | current_local_description_ = std::move(pending_local_description_); |
| 2721 | } else { |
| 2722 | replaced_remote_description = std::move(pending_remote_description_); |
| 2723 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2724 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2725 | // The session description to apply now must be accessed by |
| 2726 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2727 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2728 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 2729 | // Report statistics about any use of simulcast. |
| 2730 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 2731 | *remote_description()->description()); |
| 2732 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2733 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2734 | if (!error.ok()) { |
| 2735 | return error; |
| 2736 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2737 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2738 | if (IsUnifiedPlan()) { |
| 2739 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2740 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2741 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2742 | if (!error.ok()) { |
| 2743 | return error; |
| 2744 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2745 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2746 | // Media channels will be created only when offer is set. These may use new |
| 2747 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2748 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2749 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2750 | // description is applied. Restore back to old description. |
| 2751 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2752 | if (!error.ok()) { |
| 2753 | return error; |
| 2754 | } |
| 2755 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2756 | // Remove unused channels if MediaContentDescription is rejected. |
| 2757 | RemoveUnusedChannels(remote_description()->description()); |
| 2758 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2759 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2760 | // NOTE: Candidates allocation will be initiated only when |
| 2761 | // SetLocalDescription is called. |
| 2762 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2763 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2764 | if (!error.ok()) { |
| 2765 | return error; |
| 2766 | } |
| 2767 | |
| 2768 | if (local_description() && |
| 2769 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2770 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2771 | } |
| 2772 | |
| 2773 | if (old_remote_description) { |
| 2774 | for (const cricket::ContentInfo& content : |
| 2775 | old_remote_description->description()->contents()) { |
| 2776 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2777 | // password that indicates the remote peer requests an ICE restart. |
| 2778 | // TODO(deadbeef): When we start storing both the current and pending |
| 2779 | // remote description, this should reset pending_ice_restarts and compare |
| 2780 | // against the current description. |
| 2781 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2782 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2783 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2784 | pending_ice_restarts_.insert(content.name); |
| 2785 | } |
| 2786 | } else { |
| 2787 | // We retain all received candidates only if ICE is not restarted. |
| 2788 | // When ICE is restarted, all previous candidates belong to an old |
| 2789 | // generation and should not be kept. |
| 2790 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2791 | // description should only contain candidates from the last set remote |
| 2792 | // description plus any candidates added since then. We should remove |
| 2793 | // this once we're sure it won't break anything. |
| 2794 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2795 | old_remote_description, content.name, mutable_remote_description()); |
| 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | if (session_error() != SessionError::kNone) { |
| 2801 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2802 | } |
| 2803 | |
| 2804 | // Set the the ICE connection state to connecting since the connection may |
| 2805 | // become writable with peer reflexive candidates before any remote candidate |
| 2806 | // is signaled. |
| 2807 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2808 | // is to have a new signal the indicates a change in checking state from the |
| 2809 | // transport and expose a new checking() member from transport that can be |
| 2810 | // read to determine the current checking state. The existing SignalConnecting |
| 2811 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2812 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2813 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2814 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2815 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2816 | } |
| 2817 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2818 | // If setting the description decided our SSL role, allocate any necessary |
| 2819 | // SCTP sids. |
| 2820 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2821 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2822 | AllocateSctpSids(role); |
| 2823 | } |
| 2824 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2825 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2826 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2827 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2828 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2829 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2830 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2831 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2832 | const ContentInfo* content = |
| 2833 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2834 | if (!content) { |
| 2835 | continue; |
| 2836 | } |
| 2837 | const MediaContentDescription* media_desc = content->media_description(); |
| 2838 | RtpTransceiverDirection local_direction = |
| 2839 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2840 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 2841 | // RTCSessionDescription: Set the associated remote streams given |
| 2842 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 2843 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 2844 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2845 | std::vector<std::string> stream_ids; |
| 2846 | if (!media_desc->streams().empty()) { |
| 2847 | // The remote description has signaled the stream IDs. |
| 2848 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2849 | } |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2850 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 2851 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 2852 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 2853 | stream_ids, &added_streams, |
| 2854 | &removed_streams); |
| 2855 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 2856 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 2857 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 2858 | // process the addition of a remote track for the media description. |
| 2859 | if (!transceiver->fired_direction() || |
| 2860 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 2861 | RTC_LOG(LS_INFO) |
| 2862 | << "Processing the addition of a remote track for MID=" |
| 2863 | << content->name << "."; |
| 2864 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 11:25:05 +0100 | [diff] [blame] | 2865 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2866 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2867 | // 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] | 2868 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2869 | // removal of a remote track for the media description, given transceiver, |
| 2870 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2871 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2872 | (transceiver->fired_direction() && |
| 2873 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2874 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2875 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2876 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2877 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2878 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2879 | // 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] | 2880 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2881 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2882 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2883 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2884 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2885 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 2886 | if (transceiver->mid()) { |
| 2887 | auto dtls_transport = |
| 2888 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2889 | transceiver->internal()->sender_internal()->set_transport( |
| 2890 | dtls_transport); |
| 2891 | transceiver->internal()->receiver_internal()->set_transport( |
| 2892 | dtls_transport); |
| 2893 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2894 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2895 | // 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] | 2896 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2897 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2898 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2899 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2900 | transceiver->Stop(); |
| 2901 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2902 | if (!content->rejected && |
| 2903 | RtpTransceiverDirectionHasRecv(local_direction)) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2904 | if (!media_desc->streams().empty() && |
| 2905 | media_desc->streams()[0].has_ssrcs()) { |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 2906 | uint32_t ssrc = media_desc->streams()[0].first_ssrc(); |
| 2907 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
| 2908 | } else { |
| 2909 | transceiver->internal() |
| 2910 | ->receiver_internal() |
| 2911 | ->SetupUnsignaledMediaChannel(); |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2912 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2913 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2914 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2915 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2916 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2917 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2918 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2919 | observer->OnTrack(transceiver); |
| 2920 | observer->OnAddTrack(transceiver->receiver(), |
| 2921 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2922 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2923 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2924 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2925 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2926 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2927 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2928 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2929 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2930 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2931 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2932 | } |
| 2933 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2934 | const cricket::ContentInfo* audio_content = |
| 2935 | GetFirstAudioContent(remote_description()->description()); |
| 2936 | const cricket::ContentInfo* video_content = |
| 2937 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2938 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2939 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2940 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2941 | GetFirstVideoContentDescription(remote_description()->description()); |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 2942 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 2943 | GetFirstRtpDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2944 | |
| 2945 | // Check if the descriptions include streams, just in case the peer supports |
| 2946 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2947 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2948 | (audio_desc && !audio_desc->streams().empty()) || |
| 2949 | (video_desc && !video_desc->streams().empty())) { |
| 2950 | remote_peer_supports_msid_ = true; |
| 2951 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2952 | |
| 2953 | // We wait to signal new streams until we finish processing the description, |
| 2954 | // since only at that point will new streams have all their tracks. |
| 2955 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2956 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2957 | if (!IsUnifiedPlan()) { |
| 2958 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2959 | // rejected media section, there is some cleanup logic that expects the |
| 2960 | // voice/ video channel to still be set. But in this method the voice/video |
| 2961 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2962 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2963 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2964 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2965 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2966 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2967 | // and MediaStreams. |
| 2968 | if (audio_content) { |
| 2969 | if (audio_content->rejected) { |
| 2970 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2971 | } else { |
| 2972 | bool default_audio_track_needed = |
| 2973 | !remote_peer_supports_msid_ && |
| 2974 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2975 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2976 | default_audio_track_needed, audio_desc->type(), |
| 2977 | new_streams); |
| 2978 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2979 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2980 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2981 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2982 | // and MediaStreams. |
| 2983 | if (video_content) { |
| 2984 | if (video_content->rejected) { |
| 2985 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2986 | } else { |
| 2987 | bool default_video_track_needed = |
| 2988 | !remote_peer_supports_msid_ && |
| 2989 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2990 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2991 | default_video_track_needed, video_desc->type(), |
| 2992 | new_streams); |
| 2993 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2994 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2995 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 2996 | // If this is an RTP data transport, update the DataChannels with the |
| 2997 | // information from the remote peer. |
| 2998 | if (rtp_data_desc) { |
| 2999 | UpdateRemoteRtpDataChannels(GetActiveStreams(rtp_data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3000 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3001 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3002 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3003 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3004 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 3005 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 3006 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3007 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3008 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 3009 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3010 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3011 | UpdateEndedRemoteMediaStreams(); |
| 3012 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3013 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 3014 | if (type == SdpType::kAnswer && |
| 3015 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 3016 | *current_local_description_)) { |
| 3017 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 3018 | } |
| 3019 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3020 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3023 | void PeerConnection::SetAssociatedRemoteStreams( |
| 3024 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 3025 | const std::vector<std::string>& stream_ids, |
| 3026 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 3027 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3028 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 3029 | for (const std::string& stream_id : stream_ids) { |
| 3030 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3031 | remote_streams_->find(stream_id); |
| 3032 | if (!stream) { |
| 3033 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3034 | MediaStream::Create(stream_id)); |
| 3035 | remote_streams_->AddStream(stream); |
| 3036 | added_streams->push_back(stream); |
| 3037 | } |
| 3038 | media_streams.push_back(stream); |
| 3039 | } |
| 3040 | // Special case: "a=msid" missing, use random stream ID. |
| 3041 | if (media_streams.empty() && |
| 3042 | !(remote_description()->description()->msid_signaling() & |
| 3043 | cricket::kMsidSignalingMediaSection)) { |
| 3044 | if (!missing_msid_default_stream_) { |
| 3045 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 3046 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 3047 | added_streams->push_back(missing_msid_default_stream_); |
| 3048 | } |
| 3049 | media_streams.push_back(missing_msid_default_stream_); |
| 3050 | } |
| 3051 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 3052 | receiver->streams(); |
| 3053 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 3054 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 3055 | // update the stream-track relationships in a later step. We do this earlier, |
| 3056 | // changing the order of things, but the end-result is the same. |
| 3057 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 3058 | // instead. https://crbug.com/webrtc/9480 |
| 3059 | receiver->SetStreams(media_streams); |
| 3060 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3061 | } |
| 3062 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3063 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 3064 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3065 | transceiver, |
| 3066 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 3067 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3068 | RTC_DCHECK(transceiver->mid()); |
| 3069 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 3070 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3071 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3072 | transceiver->internal()->receiver_internal()->streams(); |
| 3073 | // This will remove the remote track from the streams. |
| 3074 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 3075 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3076 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3077 | } |
| 3078 | |
| 3079 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 3080 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 3081 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3082 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 3083 | // streams, see if the stream was removed by checking if this was the last |
| 3084 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3085 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3086 | if (remote_stream->GetAudioTracks().empty() && |
| 3087 | remote_stream->GetVideoTracks().empty()) { |
| 3088 | remote_streams_->RemoveStream(remote_stream); |
| 3089 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | } |
| 3093 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3094 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 3095 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3096 | const SessionDescriptionInterface& new_session, |
| 3097 | const SessionDescriptionInterface* old_local_description, |
| 3098 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3099 | RTC_DCHECK(IsUnifiedPlan()); |
| 3100 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 3101 | const cricket::ContentGroup* bundle_group = nullptr; |
| 3102 | if (new_session.GetType() == SdpType::kOffer) { |
| 3103 | auto bundle_group_or_error = |
| 3104 | GetEarlyBundleGroup(*new_session.description()); |
| 3105 | if (!bundle_group_or_error.ok()) { |
| 3106 | return bundle_group_or_error.MoveError(); |
| 3107 | } |
| 3108 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3109 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3110 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3111 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3112 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 3113 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3114 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 3115 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3116 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3117 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3118 | const cricket::ContentInfo* old_local_content = nullptr; |
| 3119 | if (old_local_description && |
| 3120 | i < old_local_description->description()->contents().size()) { |
| 3121 | old_local_content = |
| 3122 | &old_local_description->description()->contents()[i]; |
| 3123 | } |
| 3124 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 3125 | if (old_remote_description && |
| 3126 | i < old_remote_description->description()->contents().size()) { |
| 3127 | old_remote_content = |
| 3128 | &old_remote_description->description()->contents()[i]; |
| 3129 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3130 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3131 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 3132 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3133 | if (!transceiver_or_error.ok()) { |
| 3134 | return transceiver_or_error.MoveError(); |
| 3135 | } |
| 3136 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3137 | RTCError error = |
| 3138 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 3139 | if (!error.ok()) { |
| 3140 | return error; |
| 3141 | } |
| 3142 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3143 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 3144 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3145 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 3146 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3147 | continue; |
| 3148 | } |
| 3149 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 3150 | if (!error.ok()) { |
| 3151 | return error; |
| 3152 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3153 | } else { |
| 3154 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3155 | "Unknown section type."); |
| 3156 | } |
| 3157 | } |
| 3158 | |
| 3159 | return RTCError::OK(); |
| 3160 | } |
| 3161 | |
| 3162 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 3163 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3164 | transceiver, |
| 3165 | const cricket::ContentInfo& content, |
| 3166 | const cricket::ContentGroup* bundle_group) { |
| 3167 | RTC_DCHECK(IsUnifiedPlan()); |
| 3168 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3169 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3170 | if (content.rejected) { |
| 3171 | if (channel) { |
| 3172 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3173 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3174 | } |
| 3175 | } else { |
| 3176 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3177 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3178 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3179 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3180 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3181 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3182 | } |
| 3183 | if (!channel) { |
| 3184 | LOG_AND_RETURN_ERROR( |
| 3185 | RTCErrorType::INTERNAL_ERROR, |
| 3186 | "Failed to create channel for mid=" + content.name); |
| 3187 | } |
| 3188 | transceiver->internal()->SetChannel(channel); |
| 3189 | } |
| 3190 | } |
| 3191 | return RTCError::OK(); |
| 3192 | } |
| 3193 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3194 | RTCError PeerConnection::UpdateDataChannel( |
| 3195 | cricket::ContentSource source, |
| 3196 | const cricket::ContentInfo& content, |
| 3197 | const cricket::ContentGroup* bundle_group) { |
| 3198 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3199 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3200 | // will take care of rejecting it. |
| 3201 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3202 | } |
| 3203 | if (content.rejected) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3204 | RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3205 | DestroyDataChannel(); |
| 3206 | } else { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 3207 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3208 | RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3209 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3210 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3211 | "Failed to create data channel."); |
| 3212 | } |
| 3213 | } |
| 3214 | if (source == cricket::CS_REMOTE) { |
| 3215 | const MediaContentDescription* data_desc = content.media_description(); |
| 3216 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 3217 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 3218 | } |
| 3219 | } |
| 3220 | } |
| 3221 | return RTCError::OK(); |
| 3222 | } |
| 3223 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3224 | // This method will extract any send encodings that were sent by the remote |
| 3225 | // connection. This is currently only relevant for Simulcast scenario (where |
| 3226 | // the number of layers may be communicated by the server). |
| 3227 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 3228 | const MediaContentDescription& desc) { |
| 3229 | if (!desc.HasSimulcast()) { |
| 3230 | return {}; |
| 3231 | } |
| 3232 | std::vector<RtpEncodingParameters> result; |
| 3233 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 3234 | |
| 3235 | // This is a remote description, the parameters we are after should appear |
| 3236 | // as receive streams. |
| 3237 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 3238 | RTC_DCHECK(!alternatives.empty()); |
| 3239 | // There is currently no way to specify or choose from alternatives. |
| 3240 | // We will always use the first alternative, which is the most preferred. |
| 3241 | const SimulcastLayer& layer = alternatives[0]; |
| 3242 | RtpEncodingParameters parameters; |
| 3243 | parameters.rid = layer.rid; |
| 3244 | parameters.active = !layer.is_paused; |
| 3245 | result.push_back(parameters); |
| 3246 | } |
| 3247 | |
| 3248 | return result; |
| 3249 | } |
| 3250 | |
| 3251 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 3252 | const std::vector<SimulcastLayer>& layers, |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3253 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3254 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3255 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3256 | std::vector<std::string> disabled_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3257 | |
| 3258 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 3259 | // So we will iterate over the send encodings rather than the layers. |
| 3260 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3261 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 3262 | [&encoding](const SimulcastLayer& layer) { |
| 3263 | return layer.rid == encoding.rid; |
| 3264 | }); |
| 3265 | // 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] | 3266 | if (iter == layers.end()) { |
| 3267 | disabled_layers.push_back(encoding.rid); |
| 3268 | continue; |
| 3269 | } |
| 3270 | |
| 3271 | encoding.active = !iter->is_paused; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3272 | } |
| 3273 | |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3274 | RTCError result = sender->SetParametersInternal(parameters); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3275 | if (result.ok()) { |
| 3276 | result = sender->DisableEncodingLayers(disabled_layers); |
| 3277 | } |
| 3278 | |
| 3279 | return result; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3280 | } |
| 3281 | |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3282 | static bool SimulcastIsRejected( |
| 3283 | const ContentInfo* local_content, |
| 3284 | const MediaContentDescription& answer_media_desc) { |
| 3285 | bool simulcast_offered = local_content && |
| 3286 | local_content->media_description() && |
| 3287 | local_content->media_description()->HasSimulcast(); |
| 3288 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 3289 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 3290 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 3291 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 3292 | } |
| 3293 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3294 | static RTCError DisableSimulcastInSender( |
| 3295 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3296 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3297 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3298 | if (parameters.encodings.size() <= 1) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3299 | return RTCError::OK(); |
| 3300 | } |
| 3301 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3302 | std::vector<std::string> disabled_layers; |
| 3303 | std::transform( |
| 3304 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 3305 | std::back_inserter(disabled_layers), |
| 3306 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 3307 | return sender->DisableEncodingLayers(disabled_layers); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3308 | } |
| 3309 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3310 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3311 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3312 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3313 | size_t mline_index, |
| 3314 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3315 | const ContentInfo* old_local_content, |
| 3316 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3317 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3318 | // If this is an offer then the m= section might be recycled. If the m= |
| 3319 | // section is being recycled (defined as: rejected in the current local or |
| 3320 | // remote description and not rejected in new description), dissociate the |
| 3321 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3322 | // and discard the mapping between the transceiver and its m= section index. |
| 3323 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3324 | old_remote_content)) { |
| 3325 | // We want to dissociate the transceiver that has the rejected mid. |
| 3326 | const std::string& old_mid = |
| 3327 | (old_local_content && old_local_content->rejected) |
| 3328 | ? old_local_content->name |
| 3329 | : old_remote_content->name; |
| 3330 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3331 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3332 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3333 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3334 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3335 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3336 | } |
| 3337 | } |
| 3338 | const MediaContentDescription* media_desc = content.media_description(); |
| 3339 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3340 | if (source == cricket::CS_LOCAL) { |
| 3341 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3342 | // mapping between transceivers and m= section indices established when |
| 3343 | // creating the offer. |
| 3344 | if (!transceiver) { |
| 3345 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3346 | } |
| 3347 | if (!transceiver) { |
| 3348 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3349 | "Unknown transceiver"); |
| 3350 | } |
| 3351 | } else { |
| 3352 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3353 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3354 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3355 | // When simulcast is requested, a transceiver cannot be associated because |
| 3356 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3357 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3358 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3359 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3360 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3361 | } |
| 3362 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3363 | // recvonly direction. |
| 3364 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3365 | RTC_LOG(LS_INFO) << "Adding " |
| 3366 | << cricket::MediaTypeToString(media_desc->type()) |
| 3367 | << " transceiver for MID=" << content.name |
| 3368 | << " at i=" << mline_index |
| 3369 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3370 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3371 | std::vector<RtpEncodingParameters> send_encodings = |
| 3372 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3373 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3374 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3375 | std::string receiver_id; |
| 3376 | if (!media_desc->streams().empty()) { |
| 3377 | receiver_id = media_desc->streams()[0].id; |
| 3378 | } else { |
| 3379 | receiver_id = rtc::CreateRandomUuid(); |
| 3380 | } |
| 3381 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3382 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3383 | transceiver->internal()->set_direction( |
| 3384 | RtpTransceiverDirection::kRecvOnly); |
| 3385 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3386 | |
| 3387 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3388 | // This can happen when simulcast is not supported on the remote party. |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3389 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 3390 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3391 | RTCError error = |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3392 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3393 | if (!error.ok()) { |
| 3394 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3395 | return std::move(error); |
| 3396 | } |
| 3397 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3398 | } |
| 3399 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3400 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3401 | LOG_AND_RETURN_ERROR( |
| 3402 | RTCErrorType::INVALID_PARAMETER, |
| 3403 | "Transceiver type does not match media description type."); |
| 3404 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3405 | if (media_desc->HasSimulcast()) { |
| 3406 | std::vector<SimulcastLayer> layers = |
| 3407 | source == cricket::CS_LOCAL |
| 3408 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3409 | : media_desc->simulcast_description() |
| 3410 | .receive_layers() |
| 3411 | .GetAllLayers(); |
| 3412 | RTCError error = UpdateSimulcastLayerStatusInSender( |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3413 | layers, transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3414 | if (!error.ok()) { |
| 3415 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3416 | return std::move(error); |
| 3417 | } |
| 3418 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3419 | // Associate the found or created RtpTransceiver with the m= section by |
| 3420 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3421 | // section, and establish a mapping between the transceiver and the index of |
| 3422 | // the m= section. |
| 3423 | transceiver->internal()->set_mid(content.name); |
| 3424 | transceiver->internal()->set_mline_index(mline_index); |
| 3425 | return std::move(transceiver); |
| 3426 | } |
| 3427 | |
| 3428 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3429 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3430 | RTC_DCHECK(IsUnifiedPlan()); |
| 3431 | for (auto transceiver : transceivers_) { |
| 3432 | if (transceiver->mid() == mid) { |
| 3433 | return transceiver; |
| 3434 | } |
| 3435 | } |
| 3436 | return nullptr; |
| 3437 | } |
| 3438 | |
| 3439 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3440 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3441 | RTC_DCHECK(IsUnifiedPlan()); |
| 3442 | for (auto transceiver : transceivers_) { |
| 3443 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3444 | return transceiver; |
| 3445 | } |
| 3446 | } |
| 3447 | return nullptr; |
| 3448 | } |
| 3449 | |
| 3450 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3451 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3452 | cricket::MediaType media_type) const { |
| 3453 | RTC_DCHECK(IsUnifiedPlan()); |
| 3454 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3455 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3456 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3457 | // associated with any m= section and are not stopped, find the first such |
| 3458 | // RtpTransceiver. |
| 3459 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3460 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3461 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3462 | !transceiver->stopped()) { |
| 3463 | return transceiver; |
| 3464 | } |
| 3465 | } |
| 3466 | return nullptr; |
| 3467 | } |
| 3468 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3469 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3470 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3471 | transceiver, |
| 3472 | const SessionDescriptionInterface* sdesc) const { |
| 3473 | RTC_DCHECK(transceiver); |
| 3474 | RTC_DCHECK(sdesc); |
| 3475 | if (IsUnifiedPlan()) { |
| 3476 | if (!transceiver->internal()->mid()) { |
| 3477 | // This transceiver is not associated with a media section yet. |
| 3478 | return nullptr; |
| 3479 | } |
| 3480 | return sdesc->description()->GetContentByName( |
| 3481 | *transceiver->internal()->mid()); |
| 3482 | } else { |
| 3483 | // Plan B only allows at most one audio and one video section, so use the |
| 3484 | // first media section of that type. |
| 3485 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3486 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3487 | } |
| 3488 | } |
| 3489 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3490 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3491 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3492 | return configuration_; |
| 3493 | } |
| 3494 | |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3495 | RTCError PeerConnection::SetConfiguration( |
| 3496 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3497 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3498 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3499 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3500 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3501 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 3502 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3503 | } |
| 3504 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3505 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3506 | // size is not allowed, and changing the set of ICE servers will not result |
| 3507 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3508 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3509 | configuration_.ice_candidate_pool_size) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3510 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3511 | "Can't change candidate pool size after calling " |
| 3512 | "SetLocalDescription."); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3513 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3514 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3515 | if (local_description() && |
| 3516 | configuration.use_media_transport != configuration_.use_media_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3517 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3518 | "Can't change media_transport after calling " |
| 3519 | "SetLocalDescription."); |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | if (remote_description() && |
| 3523 | configuration.use_media_transport != configuration_.use_media_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3524 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3525 | "Can't change media_transport after calling " |
| 3526 | "SetRemoteDescription."); |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3527 | } |
| 3528 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3529 | if (local_description() && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3530 | configuration.use_media_transport_for_data_channels != |
| 3531 | configuration_.use_media_transport_for_data_channels) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3532 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3533 | "Can't change media_transport_for_data_channels " |
| 3534 | "after calling SetLocalDescription."); |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | if (remote_description() && |
| 3538 | configuration.use_media_transport_for_data_channels != |
| 3539 | configuration_.use_media_transport_for_data_channels) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3540 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3541 | "Can't change media_transport_for_data_channels " |
| 3542 | "after calling SetRemoteDescription."); |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3546 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3547 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3548 | "Can't change crypto_options after calling " |
| 3549 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3550 | } |
| 3551 | |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3552 | if (local_description() && configuration.use_datagram_transport != |
| 3553 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3554 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3555 | "Can't change use_datagram_transport " |
| 3556 | "after calling SetLocalDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | if (remote_description() && configuration.use_datagram_transport != |
| 3560 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3561 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3562 | "Can't change use_datagram_transport " |
| 3563 | "after calling SetRemoteDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3564 | } |
| 3565 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3566 | if (local_description() && |
| 3567 | configuration.use_datagram_transport_for_data_channels != |
| 3568 | configuration_.use_datagram_transport_for_data_channels) { |
| 3569 | LOG_AND_RETURN_ERROR( |
| 3570 | RTCErrorType::INVALID_MODIFICATION, |
| 3571 | "Can't change use_datagram_transport_for_data_channels " |
| 3572 | "after calling SetLocalDescription."); |
| 3573 | } |
| 3574 | |
| 3575 | if (remote_description() && |
| 3576 | configuration.use_datagram_transport_for_data_channels != |
| 3577 | configuration_.use_datagram_transport_for_data_channels) { |
| 3578 | LOG_AND_RETURN_ERROR( |
| 3579 | RTCErrorType::INVALID_MODIFICATION, |
| 3580 | "Can't change use_datagram_transport_for_data_channels " |
| 3581 | "after calling SetRemoteDescription."); |
| 3582 | } |
| 3583 | |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 3584 | if (local_description() && |
| 3585 | configuration.use_datagram_transport_for_data_channels_receive_only != |
| 3586 | configuration_ |
| 3587 | .use_datagram_transport_for_data_channels_receive_only) { |
| 3588 | LOG_AND_RETURN_ERROR( |
| 3589 | RTCErrorType::INVALID_MODIFICATION, |
| 3590 | "Can't change use_datagram_transport_for_data_channels_receive_only " |
| 3591 | "after calling SetLocalDescription."); |
| 3592 | } |
| 3593 | |
| 3594 | if (remote_description() && |
| 3595 | configuration.use_datagram_transport_for_data_channels_receive_only != |
| 3596 | configuration_ |
| 3597 | .use_datagram_transport_for_data_channels_receive_only) { |
| 3598 | LOG_AND_RETURN_ERROR( |
| 3599 | RTCErrorType::INVALID_MODIFICATION, |
| 3600 | "Can't change use_datagram_transport_for_data_channels_receive_only " |
| 3601 | "after calling SetRemoteDescription."); |
| 3602 | } |
| 3603 | |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3604 | if (configuration.use_media_transport_for_data_channels || |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3605 | configuration.use_media_transport || |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 3606 | (configuration.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3607 | *configuration.use_datagram_transport) || |
| 3608 | (configuration.use_datagram_transport_for_data_channels && |
| 3609 | *configuration.use_datagram_transport_for_data_channels)) { |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3610 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 3611 | << "Media transport requires MaxBundle policy."; |
| 3612 | } |
| 3613 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3614 | // The simplest (and most future-compatible) way to tell if the config was |
| 3615 | // modified in an invalid way is to copy each property we do support |
| 3616 | // modifying, then use operator==. There are far more properties we don't |
| 3617 | // support modifying than those we do, and more could be added. |
| 3618 | RTCConfiguration modified_config = configuration_; |
| 3619 | modified_config.servers = configuration.servers; |
| 3620 | modified_config.type = configuration.type; |
| 3621 | modified_config.ice_candidate_pool_size = |
| 3622 | configuration.ice_candidate_pool_size; |
| 3623 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
Qingsi Wang | bca1485 | 2019-06-26 14:56:02 -0700 | [diff] [blame] | 3624 | modified_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 3625 | configuration.surface_ice_candidates_on_ice_transport_type_changed; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3626 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 3627 | modified_config.ice_check_interval_strong_connectivity = |
| 3628 | configuration.ice_check_interval_strong_connectivity; |
| 3629 | modified_config.ice_check_interval_weak_connectivity = |
| 3630 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 3631 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 3632 | modified_config.ice_unwritable_min_checks = |
| 3633 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 3634 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3635 | modified_config.stun_candidate_keepalive_interval = |
| 3636 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3637 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 3638 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3639 | modified_config.active_reset_srtp_params = |
| 3640 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3641 | modified_config.use_media_transport = configuration.use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3642 | modified_config.use_media_transport_for_data_channels = |
| 3643 | configuration.use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 3644 | modified_config.use_datagram_transport = configuration.use_datagram_transport; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3645 | modified_config.use_datagram_transport_for_data_channels = |
| 3646 | configuration.use_datagram_transport_for_data_channels; |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 3647 | modified_config.use_datagram_transport_for_data_channels_receive_only = |
| 3648 | configuration.use_datagram_transport_for_data_channels_receive_only; |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 3649 | modified_config.turn_logging_id = configuration.turn_logging_id; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3650 | if (configuration != modified_config) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3651 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3652 | "Modifying the configuration in an unsupported way."); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3653 | } |
| 3654 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 3655 | // Validate the modified configuration. |
| 3656 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 3657 | if (!validate_error.ok()) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3658 | return validate_error; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 3659 | } |
| 3660 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3661 | // Note that this isn't possible through chromium, since it's an unsigned |
| 3662 | // short in WebIDL. |
| 3663 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 3664 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3665 | return RTCError(RTCErrorType::INVALID_RANGE); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | // Parse ICE servers before hopping to network thread. |
| 3669 | cricket::ServerAddresses stun_servers; |
| 3670 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 3671 | RTCErrorType parse_error = |
| 3672 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 3673 | if (parse_error != RTCErrorType::NONE) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3674 | return RTCError(parse_error); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3675 | } |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 3676 | // Add the turn logging id to all turn servers |
| 3677 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 3678 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 3679 | } |
| 3680 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3681 | // Note if STUN or TURN servers were supplied. |
| 3682 | if (!stun_servers.empty()) { |
| 3683 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 3684 | } |
| 3685 | if (!turn_servers.empty()) { |
| 3686 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 3687 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3688 | |
| 3689 | // In theory this shouldn't fail. |
| 3690 | if (!network_thread()->Invoke<bool>( |
| 3691 | RTC_FROM_HERE, |
| 3692 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 3693 | stun_servers, turn_servers, modified_config.type, |
| 3694 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3695 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3696 | modified_config.turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3697 | modified_config.stun_candidate_keepalive_interval, |
| 3698 | static_cast<bool>(local_description())))) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3699 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3700 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3701 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3702 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3703 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 3704 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 3705 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3706 | if (modified_config.servers != configuration_.servers || |
| 3707 | modified_config.type != configuration_.type || |
| 3708 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3709 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3710 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3711 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 3712 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 3713 | |
| 3714 | use_datagram_transport_ = datagram_transport_config_.enabled && |
| 3715 | modified_config.use_datagram_transport.value_or( |
| 3716 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3717 | use_datagram_transport_for_data_channels_ = |
| 3718 | datagram_transport_data_channel_config_.enabled && |
| 3719 | modified_config.use_datagram_transport_for_data_channels.value_or( |
| 3720 | datagram_transport_data_channel_config_.default_value); |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 3721 | use_datagram_transport_for_data_channels_receive_only_ = |
| 3722 | modified_config.use_datagram_transport_for_data_channels_receive_only |
| 3723 | .value_or(datagram_transport_data_channel_config_.receive_only); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 3724 | transport_controller_->SetMediaTransportSettings( |
| 3725 | modified_config.use_media_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3726 | modified_config.use_media_transport_for_data_channels, |
Bjorn A Mellem | 7da4e56 | 2019-09-26 11:02:11 -0700 | [diff] [blame] | 3727 | use_datagram_transport_, use_datagram_transport_for_data_channels_, |
| 3728 | use_datagram_transport_for_data_channels_receive_only_); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3729 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3730 | if (configuration_.active_reset_srtp_params != |
| 3731 | modified_config.active_reset_srtp_params) { |
| 3732 | transport_controller_->SetActiveResetSrtpParams( |
| 3733 | modified_config.active_reset_srtp_params); |
| 3734 | } |
| 3735 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3736 | configuration_ = modified_config; |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3737 | use_media_transport_ = configuration.use_media_transport; |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3738 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3741 | bool PeerConnection::AddIceCandidate( |
| 3742 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 3743 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3744 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3745 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3746 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3747 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3748 | return false; |
| 3749 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3750 | |
| 3751 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3752 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3753 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3754 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3755 | return false; |
| 3756 | } |
| 3757 | |
| 3758 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3759 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3760 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3761 | return false; |
| 3762 | } |
| 3763 | |
| 3764 | bool valid = false; |
| 3765 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 3766 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3767 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3768 | return false; |
| 3769 | } |
| 3770 | |
| 3771 | // Add this candidate to the remote session description. |
| 3772 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3773 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3774 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3775 | return false; |
| 3776 | } |
| 3777 | |
| 3778 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3779 | bool result = UseCandidate(ice_candidate); |
| 3780 | if (result) { |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 3781 | NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED); |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3782 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 3783 | } else { |
| 3784 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 3785 | } |
| 3786 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3787 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3788 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3789 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3790 | return true; |
| 3791 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3794 | bool PeerConnection::RemoveIceCandidates( |
| 3795 | const std::vector<cricket::Candidate>& candidates) { |
| 3796 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3797 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3798 | if (IsClosed()) { |
| 3799 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 3800 | return false; |
| 3801 | } |
| 3802 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3803 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3804 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 3805 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3806 | return false; |
| 3807 | } |
| 3808 | |
| 3809 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3810 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3811 | return false; |
| 3812 | } |
| 3813 | |
| 3814 | size_t number_removed = |
| 3815 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3816 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3817 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3818 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3819 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3820 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3824 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3825 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3826 | RTC_LOG(LS_ERROR) |
| 3827 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3828 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3829 | } |
| 3830 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3831 | } |
| 3832 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3833 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3834 | if (!worker_thread()->IsCurrent()) { |
| 3835 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3836 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3837 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 3838 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3839 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3840 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3841 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3842 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3843 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3844 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3845 | "min_bitrate_bps <= 0"); |
| 3846 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3847 | if (has_start) { |
| 3848 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3849 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3850 | "start_bitrate_bps < min_bitrate_bps"); |
| 3851 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3852 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3853 | "curent_bitrate_bps < 0"); |
| 3854 | } |
| 3855 | } |
| 3856 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3857 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3858 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3859 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3860 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3861 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3862 | "max_bitrate_bps < min_bitrate_bps"); |
| 3863 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3864 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3865 | "max_bitrate_bps < 0"); |
| 3866 | } |
| 3867 | } |
| 3868 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3869 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 10:31:54 -0700 | [diff] [blame] | 3870 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3871 | |
| 3872 | return RTCError::OK(); |
| 3873 | } |
| 3874 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3875 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3876 | if (!worker_thread()->IsCurrent()) { |
| 3877 | worker_thread()->Invoke<void>( |
| 3878 | RTC_FROM_HERE, |
| 3879 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3880 | return; |
| 3881 | } |
| 3882 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3883 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3884 | audio_state->SetPlayout(playout); |
| 3885 | } |
| 3886 | |
| 3887 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3888 | if (!worker_thread()->IsCurrent()) { |
| 3889 | worker_thread()->Invoke<void>( |
| 3890 | RTC_FROM_HERE, |
| 3891 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3892 | return; |
| 3893 | } |
| 3894 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3895 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3896 | audio_state->SetRecording(recording); |
| 3897 | } |
| 3898 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3899 | std::unique_ptr<rtc::SSLCertificate> |
| 3900 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3901 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3902 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3903 | return nullptr; |
| 3904 | } |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 3905 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3906 | } |
| 3907 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3908 | std::unique_ptr<rtc::SSLCertChain> |
| 3909 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 3910 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3911 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3912 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3913 | return nullptr; |
| 3914 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3915 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3916 | audio_transceiver->internal()->channel()->transport_name()); |
| 3917 | } |
| 3918 | |
| 3919 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3920 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3921 | for (auto transceiver : transceivers_) { |
| 3922 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3923 | return transceiver; |
| 3924 | } |
| 3925 | } |
| 3926 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3929 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3930 | int64_t output_period_ms) { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3931 | return worker_thread()->Invoke<bool>( |
Danil Chapovalov | 116ffe7 | 2019-09-05 10:21:11 +0200 | [diff] [blame] | 3932 | RTC_FROM_HERE, |
| 3933 | [this, output = std::move(output), output_period_ms]() mutable { |
| 3934 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3935 | }); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3936 | } |
| 3937 | |
Niels Möller | f00ca1a | 2019-05-10 11:33:12 +0200 | [diff] [blame] | 3938 | bool PeerConnection::StartRtcEventLog( |
| 3939 | std::unique_ptr<RtcEventLogOutput> output) { |
Niels Möller | 695cf6a | 2019-05-13 12:27:23 +0200 | [diff] [blame] | 3940 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 3941 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 3942 | output_period_ms = 5000; |
| 3943 | } |
| 3944 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 11:33:12 +0200 | [diff] [blame] | 3945 | } |
| 3946 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3947 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3948 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3949 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3950 | } |
| 3951 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 3952 | rtc::scoped_refptr<DtlsTransportInterface> |
| 3953 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3954 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 3955 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3956 | } |
| 3957 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3958 | rtc::scoped_refptr<DtlsTransport> |
| 3959 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3960 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3961 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3962 | } |
| 3963 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 3964 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 3965 | const { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3966 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 3967 | if (!sctp_mid_) { |
| 3968 | return nullptr; |
| 3969 | } |
| 3970 | return transport_controller_->GetSctpTransport(*sctp_mid_); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 3971 | } |
| 3972 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3973 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3974 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3975 | return pending_local_description_ ? pending_local_description_.get() |
| 3976 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3980 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3981 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3982 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3985 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3986 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3987 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3988 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3992 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3993 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3994 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3998 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3999 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4000 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 4004 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 4005 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4006 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 4007 | } |
| 4008 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4009 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 4010 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 4011 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4012 | // Update stats here so that we have the most recent stats for tracks and |
| 4013 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 4014 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4015 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4016 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4017 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 4018 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 4019 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 4020 | transceiver->Stop(); |
| 4021 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 4022 | |
| 4023 | // Ensure that all asynchronous stats requests are completed before destroying |
| 4024 | // the transport controller below. |
| 4025 | if (stats_collector_) { |
| 4026 | stats_collector_->WaitForPendingRequest(); |
| 4027 | } |
| 4028 | |
| 4029 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 4030 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 4031 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4032 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 4033 | // The event log is used in the transport controller, which must be outlived |
| 4034 | // by the former. CreateOffer by the peer connection is implemented |
| 4035 | // asynchronously and if the peer connection is closed without resetting the |
| 4036 | // WebRTC session description factory, the session description factory would |
| 4037 | // call the transport controller. |
| 4038 | webrtc_session_desc_factory_.reset(); |
| 4039 | transport_controller_.reset(); |
| 4040 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 4041 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4042 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 4043 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4044 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 4045 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 4046 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 4047 | call_.reset(); |
| 4048 | // The event log must outlive call (and any other object that uses it). |
| 4049 | event_log_.reset(); |
| 4050 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4051 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4052 | // The .h file says that observer can be discarded after close() returns. |
| 4053 | // Make sure this is true. |
| 4054 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4057 | void PeerConnection::OnMessage(rtc::Message* msg) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 4058 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4059 | switch (msg->message_id) { |
| 4060 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 4061 | SetSessionDescriptionMsg* param = |
| 4062 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4063 | param->observer->OnSuccess(); |
| 4064 | delete param; |
| 4065 | break; |
| 4066 | } |
| 4067 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 4068 | SetSessionDescriptionMsg* param = |
| 4069 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4070 | param->observer->OnFailure(std::move(param->error)); |
| 4071 | delete param; |
| 4072 | break; |
| 4073 | } |
| 4074 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 4075 | CreateSessionDescriptionMsg* param = |
| 4076 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 4077 | param->observer->OnFailure(std::move(param->error)); |
| 4078 | delete param; |
| 4079 | break; |
| 4080 | } |
| 4081 | case MSG_GETSTATS: { |
| 4082 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 4083 | StatsReports reports; |
| 4084 | stats_->GetStats(param->track, &reports); |
| 4085 | param->observer->OnComplete(reports); |
| 4086 | delete param; |
| 4087 | break; |
| 4088 | } |
| 4089 | case MSG_FREE_DATACHANNELS: { |
| 4090 | sctp_data_channels_to_free_.clear(); |
| 4091 | break; |
| 4092 | } |
| 4093 | case MSG_REPORT_USAGE_PATTERN: { |
| 4094 | ReportUsagePattern(); |
| 4095 | break; |
| 4096 | } |
| 4097 | default: |
| 4098 | RTC_NOTREACHED() << "Not implemented"; |
| 4099 | break; |
| 4100 | } |
| 4101 | } |
| 4102 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 4103 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 4104 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4105 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 4106 | GetAudioTransceiver()->internal()->channel()); |
| 4107 | if (voice_channel) { |
| 4108 | return voice_channel->media_channel(); |
| 4109 | } else { |
| 4110 | return nullptr; |
| 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 4115 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4116 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 4117 | GetVideoTransceiver()->internal()->channel()); |
| 4118 | if (video_channel) { |
| 4119 | return video_channel->media_channel(); |
| 4120 | } else { |
| 4121 | return nullptr; |
| 4122 | } |
| 4123 | } |
| 4124 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4125 | void PeerConnection::CreateAudioReceiver( |
| 4126 | MediaStreamInterface* stream, |
| 4127 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 4128 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4129 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 4130 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4131 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4132 | auto* audio_receiver = new AudioRtpReceiver( |
| 4133 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4134 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 4135 | if (remote_sender_info.sender_id == kDefaultAudioSenderId) { |
| 4136 | audio_receiver->SetupUnsignaledMediaChannel(); |
| 4137 | } else { |
| 4138 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4139 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4140 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4141 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4142 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 4143 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4144 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4147 | void PeerConnection::CreateVideoReceiver( |
| 4148 | MediaStreamInterface* stream, |
| 4149 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 4150 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4151 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 4152 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4153 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4154 | auto* video_receiver = new VideoRtpReceiver( |
| 4155 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4156 | video_receiver->SetMediaChannel(video_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 4157 | if (remote_sender_info.sender_id == kDefaultVideoSenderId) { |
| 4158 | video_receiver->SetupUnsignaledMediaChannel(); |
| 4159 | } else { |
| 4160 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4161 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4162 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4163 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4164 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 4165 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4166 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4169 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 4170 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4171 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4172 | const RtpSenderInfo& remote_sender_info) { |
| 4173 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 4174 | if (!receiver) { |
| 4175 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 4176 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4177 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4178 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4179 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4180 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 4181 | } else { |
| 4182 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 4183 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4184 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4187 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 4188 | MediaStreamInterface* stream) { |
| 4189 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4190 | RTC_DCHECK(track); |
| 4191 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4192 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4193 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4194 | // We already have a sender for this track, so just change the stream_id |
| 4195 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 4196 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4197 | return; |
| 4198 | } |
| 4199 | |
| 4200 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4201 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4202 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4203 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4204 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4205 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 4206 | // which will connect the sender to the underlying transport. This can |
| 4207 | // occur if a local session description that contains the ID of the sender |
| 4208 | // is set before AddStream is called. It can also occur if the local |
| 4209 | // session description is not changed and RemoveStream is called, and |
| 4210 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4211 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4212 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4213 | if (sender_info) { |
| 4214 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 4219 | // indefinitely, when we have unified plan SDP. |
| 4220 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 4221 | MediaStreamInterface* stream) { |
| 4222 | RTC_DCHECK(!IsClosed()); |
| 4223 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4224 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4225 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4226 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4227 | return; |
| 4228 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4229 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 4233 | MediaStreamInterface* stream) { |
| 4234 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4235 | RTC_DCHECK(track); |
| 4236 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4237 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4238 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4239 | // We already have a sender for this track, so just change the stream_id |
| 4240 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 4241 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4242 | return; |
| 4243 | } |
| 4244 | |
| 4245 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4246 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4247 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4248 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4249 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 4250 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4251 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4252 | if (sender_info) { |
| 4253 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 4258 | MediaStreamInterface* stream) { |
| 4259 | RTC_DCHECK(!IsClosed()); |
| 4260 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4261 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4262 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4263 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4264 | return; |
| 4265 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4266 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4267 | } |
| 4268 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4269 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4270 | if (ice_connection_state_ == new_state) { |
| 4271 | return; |
| 4272 | } |
| 4273 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4274 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4275 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4276 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4277 | return; |
| 4278 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4279 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4280 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 4281 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4282 | RTC_DCHECK(ice_connection_state_ != |
| 4283 | PeerConnectionInterface::kIceConnectionClosed); |
| 4284 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4285 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4286 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4289 | void PeerConnection::SetStandardizedIceConnectionState( |
| 4290 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4291 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4292 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4296 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 4300 | << standardized_ice_connection_state_ << " => " << new_state; |
| 4301 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4302 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 11:25:14 +0100 | [diff] [blame] | 4303 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4306 | void PeerConnection::SetConnectionState( |
| 4307 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4308 | if (connection_state_ == new_state) |
| 4309 | return; |
| 4310 | if (IsClosed()) |
| 4311 | return; |
| 4312 | connection_state_ = new_state; |
| 4313 | Observer()->OnConnectionChange(new_state); |
| 4314 | } |
| 4315 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4316 | void PeerConnection::OnIceGatheringChange( |
| 4317 | PeerConnectionInterface::IceGatheringState new_state) { |
| 4318 | if (IsClosed()) { |
| 4319 | return; |
| 4320 | } |
| 4321 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4322 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 4325 | void PeerConnection::OnIceCandidate( |
| 4326 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4327 | if (IsClosed()) { |
| 4328 | return; |
| 4329 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 4330 | ReportIceCandidateCollected(candidate->candidate()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4331 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 4334 | void PeerConnection::OnIceCandidateError(const std::string& host_candidate, |
| 4335 | const std::string& url, |
| 4336 | int error_code, |
| 4337 | const std::string& error_text) { |
| 4338 | if (IsClosed()) { |
| 4339 | return; |
| 4340 | } |
| 4341 | Observer()->OnIceCandidateError(host_candidate, url, error_code, error_text); |
| 4342 | } |
| 4343 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4344 | void PeerConnection::OnIceCandidatesRemoved( |
| 4345 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4346 | if (IsClosed()) { |
| 4347 | return; |
| 4348 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4349 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4350 | } |
| 4351 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 4352 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 4353 | const cricket::CandidatePairChangeEvent& event) { |
| 4354 | if (IsClosed()) { |
| 4355 | return; |
| 4356 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 4357 | |
Qingsi Wang | cc46b10 | 2019-09-12 11:19:01 -0700 | [diff] [blame] | 4358 | if (event.selected_candidate_pair.local_candidate().type() == |
| 4359 | LOCAL_PORT_TYPE && |
| 4360 | event.selected_candidate_pair.remote_candidate().type() == |
| 4361 | LOCAL_PORT_TYPE) { |
| 4362 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 4363 | } |
| 4364 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 4365 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 4366 | } |
| 4367 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4368 | void PeerConnection::ChangeSignalingState( |
| 4369 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4370 | if (signaling_state_ == signaling_state) { |
| 4371 | return; |
| 4372 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4373 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 4374 | << GetSignalingStateString(signaling_state_) |
| 4375 | << " New state: " |
| 4376 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4377 | signaling_state_ = signaling_state; |
| 4378 | if (signaling_state == kClosed) { |
| 4379 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4380 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4381 | standardized_ice_connection_state_ = |
| 4382 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4383 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 4384 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4385 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 4386 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4387 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4388 | } |
| 4389 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4390 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4393 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4394 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4395 | if (IsClosed()) { |
| 4396 | return; |
| 4397 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4398 | AddAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4399 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4400 | } |
| 4401 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4402 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4403 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4404 | if (IsClosed()) { |
| 4405 | return; |
| 4406 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4407 | RemoveAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4408 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4412 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4413 | if (IsClosed()) { |
| 4414 | return; |
| 4415 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4416 | AddVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4417 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4421 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4422 | if (IsClosed()) { |
| 4423 | return; |
| 4424 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4425 | RemoveVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4426 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4427 | } |
| 4428 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4429 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4430 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4431 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4432 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4433 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4434 | } |
| 4435 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4436 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4437 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4438 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4439 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4440 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4441 | msg->error = std::move(error); |
| 4442 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4443 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4447 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4448 | RTCError error) { |
| 4449 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4450 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4451 | msg->error = std::move(error); |
| 4452 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4453 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4454 | } |
| 4455 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4456 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4457 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4458 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4459 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4460 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4461 | if (IsUnifiedPlan()) { |
| 4462 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4463 | } else { |
| 4464 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 4465 | } |
| 4466 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4467 | // Intentionally unset the data channel type for RTP data channel with the |
| 4468 | // second condition. Otherwise the RTP data channels would be successfully |
| 4469 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4470 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4471 | // people won't try to use them. |
| 4472 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4473 | session_options->data_channel_type = data_channel_type(); |
| 4474 | } |
| 4475 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4476 | // Apply ICE restart flag and renomination flag. |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 4477 | bool ice_restart = offer_answer_options.ice_restart || |
| 4478 | local_ice_credentials_to_replace_->HasIceCredentials(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4479 | for (auto& options : session_options->media_description_options) { |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 4480 | options.transport_options.ice_restart = ice_restart; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4481 | options.transport_options.enable_ice_renomination = |
| 4482 | configuration_.enable_ice_renomination; |
| 4483 | } |
| 4484 | |
| 4485 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4486 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4487 | session_options->pooled_ice_credentials = |
| 4488 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4489 | RTC_FROM_HERE, |
| 4490 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4491 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 4492 | session_options->offer_extmap_allow_mixed = |
| 4493 | configuration_.offer_extmap_allow_mixed; |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 4494 | |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4495 | if (configuration_.use_media_transport || |
| 4496 | configuration_.use_media_transport_for_data_channels) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 4497 | session_options->media_transport_settings = |
| 4498 | transport_controller_->GenerateOrGetLastMediaTransportOffer(); |
| 4499 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4500 | |
| 4501 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4502 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4503 | for (auto& options : session_options->media_description_options) { |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame^] | 4504 | absl::optional<cricket::OpaqueTransportParameters> params = |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4505 | transport_controller_->GetTransportParameters(options.mid); |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame^] | 4506 | if (!params) { |
| 4507 | continue; |
| 4508 | } |
| 4509 | options.transport_options.opaque_parameters = params; |
| 4510 | if ((use_datagram_transport_ && |
| 4511 | (options.type == cricket::MEDIA_TYPE_AUDIO || |
| 4512 | options.type == cricket::MEDIA_TYPE_VIDEO)) || |
| 4513 | (use_datagram_transport_for_data_channels_ && |
| 4514 | options.type == cricket::MEDIA_TYPE_DATA)) { |
| 4515 | options.alt_protocol = params->protocol; |
| 4516 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | |
Harald Alvestrand | 4aa1192 | 2019-05-14 22:00:01 +0200 | [diff] [blame] | 4520 | // Allow fallback for using obsolete SCTP syntax. |
| 4521 | // Note that the default in |session_options| is true, while |
| 4522 | // the default in |options| is false. |
| 4523 | session_options->use_obsolete_sctp_sdp = |
| 4524 | offer_answer_options.use_obsolete_sctp_sdp; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4528 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4529 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4530 | // Figure out transceiver directional preferences. |
| 4531 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4532 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4533 | |
| 4534 | // By default, generate sendrecv/recvonly m= sections. |
| 4535 | bool recv_audio = true; |
| 4536 | bool recv_video = true; |
| 4537 | |
| 4538 | // By default, only offer a new m= section if we have media to send with it. |
| 4539 | bool offer_new_audio_description = send_audio; |
| 4540 | bool offer_new_video_description = send_video; |
| 4541 | bool offer_new_data_description = HasDataChannels(); |
| 4542 | |
| 4543 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4544 | if (offer_answer_options.offer_to_receive_audio != |
| 4545 | RTCOfferAnswerOptions::kUndefined) { |
| 4546 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4547 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4548 | offer_new_audio_description || |
| 4549 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4550 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4551 | if (offer_answer_options.offer_to_receive_video != |
| 4552 | RTCOfferAnswerOptions::kUndefined) { |
| 4553 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4554 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4555 | offer_new_video_description || |
| 4556 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4559 | absl::optional<size_t> audio_index; |
| 4560 | absl::optional<size_t> video_index; |
| 4561 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4562 | // If a current description exists, generate m= sections in the same order, |
| 4563 | // using the first audio/video/data section that appears and rejecting |
| 4564 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4565 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4566 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4567 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4568 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4569 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4570 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4571 | } |
| 4572 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4573 | // Add audio/video/data m= sections to the end if needed. |
| 4574 | if (!audio_index && offer_new_audio_description) { |
| 4575 | session_options->media_description_options.push_back( |
| 4576 | cricket::MediaDescriptionOptions( |
| 4577 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4578 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4579 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4580 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4581 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4582 | if (!video_index && offer_new_video_description) { |
| 4583 | session_options->media_description_options.push_back( |
| 4584 | cricket::MediaDescriptionOptions( |
| 4585 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4586 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4587 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4588 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4589 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4590 | if (!data_index && offer_new_data_description) { |
| 4591 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4592 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4593 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4597 | !audio_index ? nullptr |
| 4598 | : &session_options->media_description_options[*audio_index]; |
| 4599 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4600 | !video_index ? nullptr |
| 4601 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4602 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4603 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4604 | audio_media_description_options, |
| 4605 | video_media_description_options, |
| 4606 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4607 | } |
| 4608 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4609 | static cricket::MediaDescriptionOptions |
| 4610 | GetMediaDescriptionOptionsForTransceiver( |
| 4611 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4612 | transceiver, |
| 4613 | const std::string& mid) { |
| 4614 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4615 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4616 | transceiver->stopped()); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 4617 | media_description_options.codec_preferences = |
| 4618 | transceiver->codec_preferences(); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4619 | // This behavior is specified in JSEP. The gist is that: |
| 4620 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 4621 | // sendrecv. |
| 4622 | // 2. If the MSID is included, then it must be included in any subsequent |
| 4623 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4624 | if (transceiver->stopped() || |
| 4625 | (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 4626 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 4627 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4628 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4629 | |
| 4630 | cricket::SenderOptions sender_options; |
| 4631 | sender_options.track_id = transceiver->sender()->id(); |
| 4632 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 4633 | |
| 4634 | // The following sets up RIDs and Simulcast. |
| 4635 | // 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] | 4636 | RtpParameters send_parameters = |
| 4637 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4638 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 4639 | send_parameters.encodings.end(), |
| 4640 | [](const RtpEncodingParameters& encoding) { |
| 4641 | return !encoding.rid.empty(); |
| 4642 | }); |
| 4643 | |
Amit Hilbuch | b7446ed | 2019-01-28 12:25:25 -0800 | [diff] [blame] | 4644 | std::vector<RidDescription> send_rids; |
| 4645 | SimulcastLayerList send_layers; |
| 4646 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 4647 | if (encoding.rid.empty()) { |
| 4648 | continue; |
| 4649 | } |
| 4650 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 4651 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 4652 | } |
| 4653 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4654 | if (has_rids) { |
| 4655 | sender_options.rids = send_rids; |
| 4656 | } |
| 4657 | |
| 4658 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4659 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 4660 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 4661 | // simulcast, or simulcast is acheived by munging the SDP. |
| 4662 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 4663 | media_description_options.sender_options.push_back(sender_options); |
| 4664 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4665 | return media_description_options; |
| 4666 | } |
| 4667 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4668 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 4669 | static const ContentInfo* GetContentByIndex( |
| 4670 | const SessionDescriptionInterface* sdesc, |
| 4671 | size_t i) { |
| 4672 | if (!sdesc) { |
| 4673 | return nullptr; |
| 4674 | } |
| 4675 | const ContentInfos& contents = sdesc->description()->contents(); |
| 4676 | return (i < contents.size() ? &contents[i] : nullptr); |
| 4677 | } |
| 4678 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4679 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 4680 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4681 | cricket::MediaSessionOptions* session_options) { |
| 4682 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 4683 | // Offers) and 5.2.2 (Subsequent Offers). |
| 4684 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 4685 | const ContentInfos no_infos; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4686 | const ContentInfos& local_contents = |
| 4687 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 4688 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4689 | const ContentInfos& remote_contents = |
| 4690 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 4691 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4692 | // The mline indices that can be recycled. New transceivers should reuse these |
| 4693 | // slots first. |
| 4694 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4695 | // First, go through each media section that exists in either the local or |
| 4696 | // remote description and generate a media section in this offer for the |
| 4697 | // associated transceiver. If a media section can be recycled, generate a |
| 4698 | // default, rejected media section here that can be later overwritten. |
| 4699 | for (size_t i = 0; |
| 4700 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 4701 | // Either |local_content| or |remote_content| is non-null. |
| 4702 | const ContentInfo* local_content = |
| 4703 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4704 | const ContentInfo* current_local_content = |
| 4705 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4706 | const ContentInfo* remote_content = |
| 4707 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4708 | const ContentInfo* current_remote_content = |
| 4709 | GetContentByIndex(current_remote_description(), i); |
| 4710 | bool had_been_rejected = |
| 4711 | (current_local_content && current_local_content->rejected) || |
| 4712 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4713 | const std::string& mid = |
| 4714 | (local_content ? local_content->name : remote_content->name); |
| 4715 | cricket::MediaType media_type = |
| 4716 | (local_content ? local_content->media_description()->type() |
| 4717 | : remote_content->media_description()->type()); |
| 4718 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4719 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4720 | auto transceiver = GetAssociatedTransceiver(mid); |
| 4721 | RTC_CHECK(transceiver); |
| 4722 | // 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] | 4723 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 4724 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4725 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4726 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 4727 | RtpTransceiverDirection::kInactive, |
| 4728 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4729 | recycleable_mline_indices.push(i); |
| 4730 | } else { |
| 4731 | session_options->media_description_options.push_back( |
| 4732 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 4733 | // CreateOffer shouldn't really cause any state changes in |
| 4734 | // PeerConnection, but we need a way to match new transceivers to new |
| 4735 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 4736 | // by recording the index of the media section generated for the |
| 4737 | // transceiver in the offer. |
| 4738 | transceiver->internal()->set_mline_index(i); |
| 4739 | } |
| 4740 | } else { |
| 4741 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4742 | RTC_CHECK(GetDataMid()); |
| 4743 | if (had_been_rejected || mid != *GetDataMid()) { |
| 4744 | session_options->media_description_options.push_back( |
| 4745 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 4746 | } else { |
| 4747 | session_options->media_description_options.push_back( |
| 4748 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 4749 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4750 | } |
| 4751 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4752 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4753 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 4754 | // and not associated). Reuse media sections marked as recyclable first, |
| 4755 | // otherwise append to the end of the offer. New media sections should be |
| 4756 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 4757 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4758 | if (transceiver->mid() || transceiver->stopped()) { |
| 4759 | continue; |
| 4760 | } |
| 4761 | size_t mline_index; |
| 4762 | if (!recycleable_mline_indices.empty()) { |
| 4763 | mline_index = recycleable_mline_indices.front(); |
| 4764 | recycleable_mline_indices.pop(); |
| 4765 | session_options->media_description_options[mline_index] = |
| 4766 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4767 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4768 | } else { |
| 4769 | mline_index = session_options->media_description_options.size(); |
| 4770 | session_options->media_description_options.push_back( |
| 4771 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4772 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4773 | } |
| 4774 | // See comment above for why CreateOffer changes the transceiver's state. |
| 4775 | transceiver->internal()->set_mline_index(mline_index); |
| 4776 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4777 | // Lastly, add a m-section if we have local data channels and an m section |
| 4778 | // does not already exist. |
| 4779 | if (!GetDataMid() && HasDataChannels()) { |
| 4780 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4781 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4782 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | void PeerConnection::GetOptionsForAnswer( |
| 4786 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4787 | cricket::MediaSessionOptions* session_options) { |
| 4788 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 4789 | |
| 4790 | if (IsUnifiedPlan()) { |
| 4791 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 4792 | } else { |
| 4793 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 4794 | } |
| 4795 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4796 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 4797 | // the RTP data channels would be successfully negotiated by default and the |
| 4798 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 4799 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 4800 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4801 | session_options->data_channel_type = data_channel_type(); |
| 4802 | } |
| 4803 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4804 | // Apply ICE renomination flag. |
| 4805 | for (auto& options : session_options->media_description_options) { |
| 4806 | options.transport_options.enable_ice_renomination = |
| 4807 | configuration_.enable_ice_renomination; |
| 4808 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 4809 | |
| 4810 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4811 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4812 | session_options->pooled_ice_credentials = |
| 4813 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4814 | RTC_FROM_HERE, |
| 4815 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4816 | port_allocator_.get())); |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4817 | |
| 4818 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4819 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4820 | for (auto& options : session_options->media_description_options) { |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame^] | 4821 | absl::optional<cricket::OpaqueTransportParameters> params = |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4822 | transport_controller_->GetTransportParameters(options.mid); |
Bjorn A Mellem | 8e1343a | 2019-09-30 15:12:47 -0700 | [diff] [blame^] | 4823 | if (!params) { |
| 4824 | continue; |
| 4825 | } |
| 4826 | options.transport_options.opaque_parameters = params; |
| 4827 | if ((use_datagram_transport_ && |
| 4828 | (options.type == cricket::MEDIA_TYPE_AUDIO || |
| 4829 | options.type == cricket::MEDIA_TYPE_VIDEO)) || |
| 4830 | (use_datagram_transport_for_data_channels_ && |
| 4831 | options.type == cricket::MEDIA_TYPE_DATA)) { |
| 4832 | options.alt_protocol = params->protocol; |
| 4833 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4834 | } |
| 4835 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4836 | } |
| 4837 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4838 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 4839 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4840 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4841 | // Figure out transceiver directional preferences. |
| 4842 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4843 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4844 | |
| 4845 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 4846 | // restricted by the direction in the offer. |
| 4847 | bool recv_audio = true; |
| 4848 | bool recv_video = true; |
| 4849 | |
| 4850 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4851 | if (offer_answer_options.offer_to_receive_audio != |
| 4852 | RTCOfferAnswerOptions::kUndefined) { |
| 4853 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 4854 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4855 | if (offer_answer_options.offer_to_receive_video != |
| 4856 | RTCOfferAnswerOptions::kUndefined) { |
| 4857 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4858 | } |
| 4859 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4860 | absl::optional<size_t> audio_index; |
| 4861 | absl::optional<size_t> video_index; |
| 4862 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 4863 | |
| 4864 | // Generate m= sections that match those in the offer. |
| 4865 | // Note that mediasession.cc will handle intersection our preferred |
| 4866 | // direction with the offered direction. |
| 4867 | GenerateMediaDescriptionOptions( |
| 4868 | remote_description(), |
| 4869 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4870 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 4871 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4872 | |
| 4873 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4874 | !audio_index ? nullptr |
| 4875 | : &session_options->media_description_options[*audio_index]; |
| 4876 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4877 | !video_index ? nullptr |
| 4878 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4879 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4880 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4881 | audio_media_description_options, |
| 4882 | video_media_description_options, |
| 4883 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4884 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4885 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4886 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 4887 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4888 | cricket::MediaSessionOptions* session_options) { |
| 4889 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 4890 | // Answers) and 5.3.2 (Subsequent Answers). |
| 4891 | RTC_DCHECK(remote_description()); |
| 4892 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 4893 | for (const ContentInfo& content : |
| 4894 | remote_description()->description()->contents()) { |
| 4895 | cricket::MediaType media_type = content.media_description()->type(); |
| 4896 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4897 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4898 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 4899 | RTC_CHECK(transceiver); |
| 4900 | session_options->media_description_options.push_back( |
| 4901 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 4902 | } else { |
| 4903 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 4904 | // Reject all data sections if data channels are disabled. |
| 4905 | // Reject a data section if it has already been rejected. |
| 4906 | // Reject all data sections except for the first one. |
| 4907 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 4908 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4909 | session_options->media_description_options.push_back( |
| 4910 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4911 | } else { |
| 4912 | session_options->media_description_options.push_back( |
| 4913 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4914 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4915 | } |
| 4916 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4917 | } |
| 4918 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4919 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 4920 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4921 | RtpTransceiverDirection audio_direction, |
| 4922 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4923 | absl::optional<size_t>* audio_index, |
| 4924 | absl::optional<size_t>* video_index, |
| 4925 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4926 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4927 | for (const cricket::ContentInfo& content : |
| 4928 | session_desc->description()->contents()) { |
| 4929 | if (IsAudioContent(&content)) { |
| 4930 | // If we already have an audio m= section, reject this extra one. |
| 4931 | if (*audio_index) { |
| 4932 | session_options->media_description_options.push_back( |
| 4933 | cricket::MediaDescriptionOptions( |
| 4934 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4935 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4936 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4937 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4938 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4939 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 4940 | content.name, audio_direction, |
| 4941 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4942 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4943 | } |
| 4944 | } else if (IsVideoContent(&content)) { |
| 4945 | // If we already have an video m= section, reject this extra one. |
| 4946 | if (*video_index) { |
| 4947 | session_options->media_description_options.push_back( |
| 4948 | cricket::MediaDescriptionOptions( |
| 4949 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4950 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4951 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4952 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4953 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4954 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 4955 | content.name, video_direction, |
| 4956 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4957 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4958 | } |
| 4959 | } else { |
| 4960 | RTC_DCHECK(IsDataContent(&content)); |
| 4961 | // If we already have an data m= section, reject this extra one. |
| 4962 | if (*data_index) { |
| 4963 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4964 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4965 | } else { |
| 4966 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4967 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4968 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4969 | } |
| 4970 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4971 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4972 | } |
| 4973 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4974 | cricket::MediaDescriptionOptions |
| 4975 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4976 | const std::string& mid) const { |
| 4977 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4978 | // expect sendrecv. |
| 4979 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4980 | RtpTransceiverDirection::kSendRecv, |
| 4981 | /*stopped=*/false); |
| 4982 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4983 | return options; |
| 4984 | } |
| 4985 | |
| 4986 | cricket::MediaDescriptionOptions |
| 4987 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4988 | const std::string& mid) const { |
| 4989 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4990 | RtpTransceiverDirection::kInactive, |
| 4991 | /*stopped=*/true); |
| 4992 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4993 | return options; |
| 4994 | } |
| 4995 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4996 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4997 | switch (data_channel_type_) { |
| 4998 | case cricket::DCT_RTP: |
| 4999 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5000 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5001 | } |
| 5002 | return rtp_data_channel_->content_name(); |
| 5003 | case cricket::DCT_SCTP: |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5004 | case cricket::DCT_MEDIA_TRANSPORT: |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 5005 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
| 5006 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
| 5007 | return sctp_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5008 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5009 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 5010 | } |
| 5011 | } |
| 5012 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5013 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 5014 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 5015 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5016 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 5017 | } |
| 5018 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5019 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5020 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5021 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5022 | cricket::MediaType media_type, |
| 5023 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5024 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5025 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5026 | std::vector<RtpSenderInfo>* current_senders = |
| 5027 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5028 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5029 | // 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] | 5030 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5031 | for (auto sender_it = current_senders->begin(); |
| 5032 | sender_it != current_senders->end(); |
| 5033 | /* incremented manually */) { |
| 5034 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5035 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5036 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5037 | std::string params_stream_id; |
| 5038 | if (params) { |
| 5039 | params_stream_id = |
| 5040 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 5041 | : kDefaultStreamId); |
| 5042 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5043 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5044 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5045 | // 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] | 5046 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5047 | sender_exists) { |
| 5048 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5049 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5050 | OnRemoteSenderRemoved(info, media_type); |
| 5051 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5052 | } |
| 5053 | } |
| 5054 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5055 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5056 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 5057 | if (!params.has_ssrcs()) { |
| 5058 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 5059 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 5060 | // create a default. |
| 5061 | default_sender_needed = true; |
| 5062 | break; |
| 5063 | } |
| 5064 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5065 | // |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] | 5066 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 5067 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 5068 | // not supported in Plan B, we just take the first here and create the |
| 5069 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5070 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5071 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 5072 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5073 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5074 | uint32_t ssrc = params.first_ssrc(); |
| 5075 | |
| 5076 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5077 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5078 | if (!stream) { |
| 5079 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5080 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5081 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5082 | remote_streams_->AddStream(stream); |
| 5083 | new_streams->AddStream(stream); |
| 5084 | } |
| 5085 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5086 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5087 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5088 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5089 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5090 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5091 | } |
| 5092 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5093 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5094 | // Add default sender if necessary. |
| 5095 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5096 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5097 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5098 | if (!default_stream) { |
| 5099 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5100 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5101 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5102 | remote_streams_->AddStream(default_stream); |
| 5103 | new_streams->AddStream(default_stream); |
| 5104 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5105 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5106 | ? kDefaultAudioSenderId |
| 5107 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5108 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5109 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5110 | if (!default_sender_info) { |
| 5111 | current_senders->push_back( |
Ruslan Burakov | 7ea4605 | 2019-02-16 02:07:05 +0100 | [diff] [blame] | 5112 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5113 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5114 | } |
| 5115 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5116 | } |
| 5117 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5118 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 5119 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 5120 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 5121 | << " receiver for track_id=" << sender_info.sender_id |
| 5122 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5123 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 5124 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5125 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5126 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5127 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5128 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5129 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 5130 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5131 | } |
| 5132 | } |
| 5133 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5134 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 5135 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5136 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 5137 | << " receiver for track_id=" << sender_info.sender_id |
| 5138 | << " and stream_id=" << sender_info.stream_id; |
| 5139 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5140 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5141 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5142 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5143 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5144 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 5145 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5146 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5147 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5148 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5149 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5150 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5151 | } |
| 5152 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5153 | // Stopping or destroying a VideoRtpReceiver will end the |
| 5154 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5155 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5156 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5157 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5158 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5159 | // There's no guarantee the track is still available, e.g. the track may |
| 5160 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5161 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5162 | } |
| 5163 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 5164 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5165 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5166 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5167 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5168 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 5172 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 5173 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 5174 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 5175 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 5176 | streams_to_remove.push_back(stream); |
| 5177 | } |
| 5178 | } |
| 5179 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 5180 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5181 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5182 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5183 | } |
| 5184 | } |
| 5185 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5186 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5187 | const std::vector<cricket::StreamParams>& streams, |
| 5188 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5189 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5190 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5191 | // 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] | 5192 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5193 | for (auto sender_it = current_senders->begin(); |
| 5194 | sender_it != current_senders->end(); |
| 5195 | /* incremented manually */) { |
| 5196 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5197 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5198 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 5199 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5200 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5201 | OnLocalSenderRemoved(info, media_type); |
| 5202 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5203 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5204 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5205 | } |
| 5206 | } |
| 5207 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5208 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5209 | for (const cricket::StreamParams& params : streams) { |
| 5210 | // 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] | 5211 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5212 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5213 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5214 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5215 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5216 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5217 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5218 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5219 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5220 | } |
| 5221 | } |
| 5222 | } |
| 5223 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5224 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 5225 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5226 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5227 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5228 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5229 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 5230 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5231 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5232 | return; |
| 5233 | } |
| 5234 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5235 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5236 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5237 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5238 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5239 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5240 | |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 5241 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5242 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5245 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 5246 | cricket::MediaType media_type) { |
| 5247 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5248 | if (!sender) { |
| 5249 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5250 | // SessionDescriptions has been renegotiated. |
| 5251 | return; |
| 5252 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5253 | |
| 5254 | // A sender has been removed from the SessionDescription but it's still |
| 5255 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 5256 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 5257 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5258 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5259 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5260 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5261 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5262 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5263 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 5267 | const cricket::StreamParamsVec& streams) { |
| 5268 | std::vector<std::string> existing_channels; |
| 5269 | |
| 5270 | // Find new and active data channels. |
| 5271 | for (const cricket::StreamParams& params : streams) { |
| 5272 | // |it->sync_label| is actually the data channel label. The reason is that |
| 5273 | // we use the same naming of data channels as we do for |
| 5274 | // MediaStreams and Tracks. |
| 5275 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 5276 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5277 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5278 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 5279 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5280 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5281 | continue; |
| 5282 | } |
| 5283 | // Set the SSRC the data channel should use for sending. |
| 5284 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 5285 | existing_channels.push_back(data_channel_it->first); |
| 5286 | } |
| 5287 | |
| 5288 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 5289 | } |
| 5290 | |
| 5291 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 5292 | const cricket::StreamParamsVec& streams) { |
| 5293 | std::vector<std::string> existing_channels; |
| 5294 | |
| 5295 | // Find new and active data channels. |
| 5296 | for (const cricket::StreamParams& params : streams) { |
| 5297 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 5298 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5299 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5300 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5301 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5302 | auto data_channel_it = rtp_data_channels_.find(label); |
| 5303 | if (data_channel_it == rtp_data_channels_.end()) { |
| 5304 | // This is a new data channel. |
| 5305 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 5306 | } else { |
| 5307 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 5308 | } |
| 5309 | existing_channels.push_back(label); |
| 5310 | } |
| 5311 | |
| 5312 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 5313 | } |
| 5314 | |
| 5315 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 5316 | const std::vector<std::string>& active_channels, |
| 5317 | bool is_local_update) { |
| 5318 | auto it = rtp_data_channels_.begin(); |
| 5319 | while (it != rtp_data_channels_.end()) { |
| 5320 | DataChannel* data_channel = it->second; |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 5321 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5322 | ++it; |
| 5323 | continue; |
| 5324 | } |
| 5325 | |
| 5326 | if (is_local_update) { |
| 5327 | data_channel->SetSendSsrc(0); |
| 5328 | } else { |
| 5329 | data_channel->RemotePeerRequestClose(); |
| 5330 | } |
| 5331 | |
| 5332 | if (data_channel->state() == DataChannel::kClosed) { |
| 5333 | rtp_data_channels_.erase(it); |
| 5334 | it = rtp_data_channels_.begin(); |
| 5335 | } else { |
| 5336 | ++it; |
| 5337 | } |
| 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 5342 | uint32_t remote_ssrc) { |
| 5343 | rtc::scoped_refptr<DataChannel> channel( |
| 5344 | InternalCreateDataChannel(label, nullptr)); |
| 5345 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5346 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5347 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5348 | return; |
| 5349 | } |
| 5350 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 5351 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5352 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5353 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5354 | } |
| 5355 | |
| 5356 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 5357 | const std::string& label, |
| 5358 | const InternalDataChannelInit* config) { |
| 5359 | if (IsClosed()) { |
| 5360 | return nullptr; |
| 5361 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5362 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5363 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5364 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 5365 | return nullptr; |
| 5366 | } |
| 5367 | InternalDataChannelInit new_config = |
| 5368 | config ? (*config) : InternalDataChannelInit(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5369 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5370 | if (new_config.id < 0) { |
| 5371 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5372 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5373 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5374 | RTC_LOG(LS_ERROR) |
| 5375 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5376 | return nullptr; |
| 5377 | } |
| 5378 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5379 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5380 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5381 | return nullptr; |
| 5382 | } |
| 5383 | } |
| 5384 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5385 | rtc::scoped_refptr<DataChannel> channel( |
| 5386 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5387 | if (!channel) { |
| 5388 | sid_allocator_.ReleaseSid(new_config.id); |
| 5389 | return nullptr; |
| 5390 | } |
| 5391 | |
| 5392 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 5393 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5394 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 5395 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5396 | return nullptr; |
| 5397 | } |
| 5398 | rtp_data_channels_[channel->label()] = channel; |
| 5399 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5400 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5401 | sctp_data_channels_.push_back(channel); |
| 5402 | channel->SignalClosed.connect(this, |
| 5403 | &PeerConnection::OnSctpDataChannelClosed); |
| 5404 | } |
| 5405 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 5406 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5407 | return channel; |
| 5408 | } |
| 5409 | |
| 5410 | bool PeerConnection::HasDataChannels() const { |
| 5411 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 5412 | } |
| 5413 | |
| 5414 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 11:29:38 +0100 | [diff] [blame] | 5415 | std::vector<rtc::scoped_refptr<DataChannel>> channels_to_close; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5416 | for (const auto& channel : sctp_data_channels_) { |
| 5417 | if (channel->id() < 0) { |
| 5418 | int sid; |
| 5419 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 11:29:38 +0100 | [diff] [blame] | 5420 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid, closing channel."; |
| 5421 | channels_to_close.push_back(channel); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5422 | continue; |
| 5423 | } |
| 5424 | channel->SetSctpSid(sid); |
| 5425 | } |
| 5426 | } |
Harald Alvestrand | 1f928d3 | 2019-03-28 11:29:38 +0100 | [diff] [blame] | 5427 | // Since closing modifies the list of channels, we have to do the actual |
| 5428 | // closing outside the loop. |
| 5429 | for (const auto& channel : channels_to_close) { |
| 5430 | channel->CloseAbruptly(); |
| 5431 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 5435 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5436 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 5437 | ++it) { |
| 5438 | if (it->get() == channel) { |
| 5439 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5440 | // After the closing procedure is done, it's safe to use this ID for |
| 5441 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5442 | sid_allocator_.ReleaseSid(channel->id()); |
| 5443 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 5444 | // Since this method is triggered by a signal from the DataChannel, |
| 5445 | // we can't free it directly here; we need to free it asynchronously. |
| 5446 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5447 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 5448 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 5449 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5450 | return; |
| 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5455 | void PeerConnection::OnDataChannelDestroyed() { |
| 5456 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 5457 | // DataChannel may callback to us and try to modify the list. |
| 5458 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 5459 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 5460 | for (const auto& kv : temp_rtp_dcs) { |
| 5461 | kv.second->OnTransportChannelDestroyed(); |
| 5462 | } |
| 5463 | |
| 5464 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 5465 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 5466 | for (const auto& channel : temp_sctp_dcs) { |
| 5467 | channel->OnTransportChannelDestroyed(); |
| 5468 | } |
| 5469 | } |
| 5470 | |
| 5471 | void PeerConnection::OnDataChannelOpenMessage( |
| 5472 | const std::string& label, |
| 5473 | const InternalDataChannelInit& config) { |
| 5474 | rtc::scoped_refptr<DataChannel> channel( |
| 5475 | InternalCreateDataChannel(label, &config)); |
| 5476 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5477 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5478 | return; |
| 5479 | } |
| 5480 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 5481 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5482 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5483 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 5484 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5485 | } |
| 5486 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5487 | bool PeerConnection::HandleOpenMessage_s( |
| 5488 | const cricket::ReceiveDataParams& params, |
| 5489 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5490 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 5491 | // Received OPEN message; parse and signal that a new data channel should |
| 5492 | // be created. |
| 5493 | std::string label; |
| 5494 | InternalDataChannelInit config; |
| 5495 | config.id = params.ssrc; |
| 5496 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 5497 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 5498 | << params.ssrc; |
| 5499 | return true; |
| 5500 | } |
| 5501 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5502 | OnDataChannelOpenMessage(label, config); |
| 5503 | return true; |
| 5504 | } |
| 5505 | return false; |
| 5506 | } |
| 5507 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5508 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5509 | PeerConnection::GetAudioTransceiver() const { |
| 5510 | // This method only works with Plan B SDP, where there is a single |
| 5511 | // audio/video transceiver. |
| 5512 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5513 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5514 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5515 | return transceiver; |
| 5516 | } |
| 5517 | } |
| 5518 | RTC_NOTREACHED(); |
| 5519 | return nullptr; |
| 5520 | } |
| 5521 | |
| 5522 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5523 | PeerConnection::GetVideoTransceiver() const { |
| 5524 | // This method only works with Plan B SDP, where there is a single |
| 5525 | // audio/video transceiver. |
| 5526 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5527 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5528 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5529 | return transceiver; |
| 5530 | } |
| 5531 | } |
| 5532 | RTC_NOTREACHED(); |
| 5533 | return nullptr; |
| 5534 | } |
| 5535 | |
| 5536 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 5537 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5538 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5539 | switch (type) { |
| 5540 | case cricket::MEDIA_TYPE_AUDIO: |
| 5541 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 5542 | case cricket::MEDIA_TYPE_VIDEO: |
| 5543 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 5544 | case cricket::MEDIA_TYPE_DATA: |
| 5545 | return false; |
| 5546 | } |
| 5547 | RTC_NOTREACHED(); |
| 5548 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5549 | } |
| 5550 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5551 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5552 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5553 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5554 | for (auto sender : transceiver->internal()->senders()) { |
| 5555 | if (sender->track() == track) { |
| 5556 | return sender; |
| 5557 | } |
| 5558 | } |
| 5559 | } |
| 5560 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5561 | } |
| 5562 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5563 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5564 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5565 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5566 | for (auto sender : transceiver->internal()->senders()) { |
| 5567 | if (sender->id() == sender_id) { |
| 5568 | return sender; |
| 5569 | } |
| 5570 | } |
| 5571 | } |
| 5572 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5573 | } |
| 5574 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5575 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 5576 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5577 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5578 | for (auto receiver : transceiver->internal()->receivers()) { |
| 5579 | if (receiver->id() == receiver_id) { |
| 5580 | return receiver; |
| 5581 | } |
| 5582 | } |
| 5583 | } |
| 5584 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5585 | } |
| 5586 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5587 | std::vector<PeerConnection::RtpSenderInfo>* |
| 5588 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 5589 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5590 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 5591 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5592 | ? &remote_audio_sender_infos_ |
| 5593 | : &remote_video_sender_infos_; |
| 5594 | } |
| 5595 | |
| 5596 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5597 | cricket::MediaType media_type) { |
| 5598 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5599 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5600 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 5601 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5602 | } |
| 5603 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5604 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 5605 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5606 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5607 | const std::string sender_id) const { |
| 5608 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5609 | if (sender_info.stream_id == stream_id && |
| 5610 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5611 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5612 | } |
| 5613 | } |
| 5614 | return nullptr; |
| 5615 | } |
| 5616 | |
| 5617 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 5618 | for (const auto& channel : sctp_data_channels_) { |
| 5619 | if (channel->id() == sid) { |
| 5620 | return channel; |
| 5621 | } |
| 5622 | } |
| 5623 | return nullptr; |
| 5624 | } |
| 5625 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5626 | PeerConnection::InitializePortAllocatorResult |
| 5627 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5628 | const cricket::ServerAddresses& stun_servers, |
| 5629 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5630 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5631 | RTC_DCHECK_RUN_ON(network_thread()); |
| 5632 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 5633 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5634 | // To handle both internal and externally created port allocator, we will |
| 5635 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5636 | int port_allocator_flags = port_allocator_->flags(); |
| 5637 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5638 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5639 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5640 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5641 | // by experiment. |
| 5642 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5643 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 5644 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 5645 | .find("Disabled") == 0) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5646 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5647 | } |
| 5648 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5649 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5650 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5651 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5652 | } |
| 5653 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5654 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5655 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5656 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5657 | } |
| 5658 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5659 | if (configuration.candidate_network_policy == |
| 5660 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5661 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5662 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5663 | } |
| 5664 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5665 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5666 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5667 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5668 | } |
| 5669 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5670 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5671 | // No step delay is used while allocating ports. |
| 5672 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5673 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5674 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 5675 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5676 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5677 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5678 | for (auto& turn_server : turn_servers_copy) { |
| 5679 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 5680 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5681 | // Call this last since it may create pooled allocator sessions using the |
| 5682 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5683 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5684 | stun_servers, std::move(turn_servers_copy), |
| 5685 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 5686 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5687 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5688 | |
| 5689 | InitializePortAllocatorResult res; |
| 5690 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 5691 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5692 | } |
| 5693 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 5694 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5695 | const cricket::ServerAddresses& stun_servers, |
| 5696 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5697 | IceTransportsType type, |
| 5698 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 5699 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5700 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 5701 | absl::optional<int> stun_candidate_keepalive_interval, |
| 5702 | bool have_local_description) { |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5703 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5704 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5705 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5706 | // size is not allowed, and changing the set of ICE servers will not result |
| 5707 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 5708 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5709 | port_allocator_->FreezeCandidatePool(); |
| 5710 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5711 | // Add the custom tls turn servers if they exist. |
| 5712 | auto turn_servers_copy = turn_servers; |
| 5713 | for (auto& turn_server : turn_servers_copy) { |
| 5714 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5715 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5716 | // Call this last since it may create pooled allocator sessions using the |
| 5717 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 5718 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5719 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 5720 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5721 | } |
| 5722 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 5723 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5724 | return factory_->channel_manager(); |
| 5725 | } |
| 5726 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 5727 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5728 | std::unique_ptr<RtcEventLogOutput> output, |
| 5729 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5730 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5731 | if (!event_log_) { |
| 5732 | return false; |
| 5733 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5734 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5738 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5739 | if (event_log_) { |
| 5740 | event_log_->StopLogging(); |
| 5741 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5742 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 5743 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5744 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5745 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5746 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5747 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5748 | if (channel && channel->content_name() == content_name) { |
| 5749 | return channel; |
| 5750 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5751 | } |
| 5752 | if (rtp_data_channel() && |
| 5753 | rtp_data_channel()->content_name() == content_name) { |
| 5754 | return rtp_data_channel(); |
| 5755 | } |
| 5756 | return nullptr; |
| 5757 | } |
| 5758 | |
| 5759 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5760 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5761 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5762 | RTC_LOG(LS_INFO) |
| 5763 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5764 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5765 | return false; |
| 5766 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5767 | if (!data_channel_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5768 | 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] | 5769 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5770 | return false; |
| 5771 | } |
| 5772 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5773 | absl::optional<rtc::SSLRole> dtls_role; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5774 | if (sctp_mid_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5775 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5776 | if (!dtls_role && is_caller_.has_value()) { |
| 5777 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5778 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5779 | *role = *dtls_role; |
| 5780 | return true; |
| 5781 | } |
| 5782 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5786 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 5787 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5788 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5789 | RTC_LOG(LS_INFO) |
| 5790 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5791 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5792 | return false; |
| 5793 | } |
| 5794 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5795 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5796 | if (dtls_role) { |
| 5797 | *role = *dtls_role; |
| 5798 | return true; |
| 5799 | } |
| 5800 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5801 | } |
| 5802 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5803 | void PeerConnection::SetSessionError(SessionError error, |
| 5804 | const std::string& error_desc) { |
| 5805 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5806 | if (error != session_error_) { |
| 5807 | session_error_ = error; |
| 5808 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5809 | } |
| 5810 | } |
| 5811 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5812 | RTCError PeerConnection::UpdateSessionState( |
| 5813 | SdpType type, |
| 5814 | cricket::ContentSource source, |
| 5815 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5816 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5817 | |
| 5818 | // If there's already a pending error then no state transition should happen. |
| 5819 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5820 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5821 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5822 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5823 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5824 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | // Update the signaling state according to the specified state machine (see |
| 5828 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5829 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5830 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5831 | ? PeerConnectionInterface::kHaveLocalOffer |
| 5832 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5833 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5834 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5835 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 5836 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5837 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5838 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5839 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 5840 | } |
| 5841 | |
| 5842 | // Update internal objects according to the session description's media |
| 5843 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5844 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5845 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 5846 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5847 | } |
| 5848 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5849 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5850 | } |
| 5851 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5852 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5853 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5854 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5855 | const SessionDescriptionInterface* sdesc = |
| 5856 | (source == cricket::CS_LOCAL ? local_description() |
| 5857 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5858 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5859 | |
| 5860 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5861 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5862 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5863 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5864 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5865 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5866 | continue; |
| 5867 | } |
| 5868 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5869 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5870 | if (!content_desc) { |
| 5871 | continue; |
| 5872 | } |
| 5873 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5874 | bool success = (source == cricket::CS_LOCAL) |
| 5875 | ? channel->SetLocalContent(content_desc, type, &error) |
| 5876 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5877 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5878 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5879 | } |
| 5880 | } |
| 5881 | |
| 5882 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 5883 | if (rtp_data_channel_) { |
| 5884 | const ContentInfo* data_content = |
| 5885 | cricket::GetFirstDataContent(sdesc->description()); |
| 5886 | if (data_content && !data_content->rejected) { |
| 5887 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5888 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5889 | if (data_desc) { |
| 5890 | std::string error; |
| 5891 | bool success = |
| 5892 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5893 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5894 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5895 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5896 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5897 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5898 | } |
| 5899 | } |
| 5900 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5901 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5902 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 5903 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5904 | if (sctp_mid_ && local_description() && remote_description()) { |
| 5905 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
| 5906 | transport_controller_->GetSctpTransport(*sctp_mid_); |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 5907 | auto local_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 5908 | local_description()->description()); |
| 5909 | auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 5910 | remote_description()->description()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5911 | if (sctp_transport && local_sctp_description && remote_sctp_description) { |
Harald Alvestrand | 8d3d6cf | 2019-05-16 11:49:17 +0200 | [diff] [blame] | 5912 | int max_message_size; |
| 5913 | // A remote max message size of zero means "any size supported". |
| 5914 | // We configure the connection with our own max message size. |
| 5915 | if (remote_sctp_description->max_message_size() == 0) { |
| 5916 | max_message_size = local_sctp_description->max_message_size(); |
| 5917 | } else { |
| 5918 | max_message_size = |
| 5919 | std::min(local_sctp_description->max_message_size(), |
| 5920 | remote_sctp_description->max_message_size()); |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 5921 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5922 | sctp_transport->Start(local_sctp_description->port(), |
| 5923 | remote_sctp_description->port(), max_message_size); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5924 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5925 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5926 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5927 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5928 | } |
| 5929 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5930 | RTCError PeerConnection::PushdownTransportDescription( |
| 5931 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5932 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5933 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5934 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5935 | if (source == cricket::CS_LOCAL) { |
| 5936 | const SessionDescriptionInterface* sdesc = local_description(); |
| 5937 | RTC_DCHECK(sdesc); |
| 5938 | return transport_controller_->SetLocalDescription(type, |
| 5939 | sdesc->description()); |
| 5940 | } else { |
| 5941 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 5942 | RTC_DCHECK(sdesc); |
| 5943 | return transport_controller_->SetRemoteDescription(type, |
| 5944 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5945 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | bool PeerConnection::GetTransportDescription( |
| 5949 | const SessionDescription* description, |
| 5950 | const std::string& content_name, |
| 5951 | cricket::TransportDescription* tdesc) { |
| 5952 | if (!description || !tdesc) { |
| 5953 | return false; |
| 5954 | } |
| 5955 | const TransportInfo* transport_info = |
| 5956 | description->GetTransportInfoByName(content_name); |
| 5957 | if (!transport_info) { |
| 5958 | return false; |
| 5959 | } |
| 5960 | *tdesc = transport_info->description; |
| 5961 | return true; |
| 5962 | } |
| 5963 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5964 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 5965 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 5966 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5967 | switch (config.continual_gathering_policy) { |
| 5968 | case PeerConnectionInterface::GATHER_ONCE: |
| 5969 | gathering_policy = cricket::GATHER_ONCE; |
| 5970 | break; |
| 5971 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 5972 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 5973 | break; |
| 5974 | default: |
| 5975 | RTC_NOTREACHED(); |
| 5976 | gathering_policy = cricket::GATHER_ONCE; |
| 5977 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5978 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5979 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5980 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 5981 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5982 | ice_config.prioritize_most_likely_candidate_pairs = |
| 5983 | config.prioritize_most_likely_ice_candidate_pairs; |
| 5984 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5985 | RTCConfigurationToIceConfigOptionalInt( |
| 5986 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5987 | ice_config.continual_gathering_policy = gathering_policy; |
| 5988 | ice_config.presume_writable_when_fully_relayed = |
| 5989 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 5990 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 5991 | config.surface_ice_candidates_on_ice_transport_type_changed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 5992 | ice_config.ice_check_interval_strong_connectivity = |
| 5993 | config.ice_check_interval_strong_connectivity; |
| 5994 | ice_config.ice_check_interval_weak_connectivity = |
| 5995 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5996 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-29 23:08:51 -0800 | [diff] [blame] | 5997 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 5998 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 5999 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 6000 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6001 | ice_config.regather_all_networks_interval_range = |
| 6002 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 6003 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6004 | return ice_config; |
| 6005 | } |
| 6006 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6007 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 6008 | const rtc::CopyOnWriteBuffer& payload, |
| 6009 | cricket::SendDataResult* result) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 6010 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6011 | if (data_channel_transport_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6012 | SendDataParams send_params; |
| 6013 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 6014 | send_params.ordered = params.ordered; |
| 6015 | if (params.max_rtx_count >= 0) { |
| 6016 | send_params.max_rtx_count = params.max_rtx_count; |
| 6017 | } else if (params.max_rtx_ms >= 0) { |
| 6018 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 6019 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6020 | |
| 6021 | RTCError error = network_thread()->Invoke<RTCError>( |
| 6022 | RTC_FROM_HERE, [this, params, send_params, payload] { |
| 6023 | return data_channel_transport_->SendData(params.sid, send_params, |
| 6024 | payload); |
| 6025 | }); |
| 6026 | |
| 6027 | if (error.ok()) { |
| 6028 | *result = cricket::SendDataResult::SDR_SUCCESS; |
| 6029 | return true; |
| 6030 | } else if (error.type() == RTCErrorType::RESOURCE_EXHAUSTED) { |
| 6031 | // SCTP transport uses RESOURCE_EXHAUSTED when it's blocked. |
| 6032 | // TODO(mellem): Stop using RTCError here and get rid of the mapping. |
| 6033 | *result = cricket::SendDataResult::SDR_BLOCK; |
| 6034 | return false; |
| 6035 | } |
| 6036 | *result = cricket::SendDataResult::SDR_ERROR; |
| 6037 | return false; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6038 | } else if (rtp_data_channel_) { |
| 6039 | return rtp_data_channel_->SendData(params, payload, result); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6040 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6041 | RTC_LOG(LS_ERROR) << "SendData called before transport is ready"; |
| 6042 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6046 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6047 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6048 | // Don't log an error here, because DataChannels are expected to call |
| 6049 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 6050 | // whether or not the underlying transport is ready. |
| 6051 | return false; |
| 6052 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6053 | if (data_channel_transport_) { |
| 6054 | SignalDataChannelTransportWritable_s.connect(webrtc_data_channel, |
| 6055 | &DataChannel::OnChannelReady); |
| 6056 | SignalDataChannelTransportReceivedData_s.connect( |
| 6057 | webrtc_data_channel, &DataChannel::OnDataReceived); |
| 6058 | SignalDataChannelTransportChannelClosing_s.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6059 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6060 | SignalDataChannelTransportChannelClosed_s.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6061 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6062 | } |
| 6063 | if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6064 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 6065 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 6066 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 6067 | &DataChannel::OnDataReceived); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6068 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6069 | return true; |
| 6070 | } |
| 6071 | |
| 6072 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6073 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6074 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6075 | RTC_LOG(LS_ERROR) |
| 6076 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 6077 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6078 | return; |
| 6079 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6080 | if (data_channel_transport_) { |
| 6081 | SignalDataChannelTransportWritable_s.disconnect(webrtc_data_channel); |
| 6082 | SignalDataChannelTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 6083 | SignalDataChannelTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 6084 | SignalDataChannelTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 6085 | } |
| 6086 | if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6087 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 6088 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6089 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6090 | } |
| 6091 | |
| 6092 | void PeerConnection::AddSctpDataStream(int sid) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6093 | if (data_channel_transport_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6094 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 6095 | if (data_channel_transport_) { |
| 6096 | data_channel_transport_->OpenChannel(sid); |
| 6097 | } |
| 6098 | }); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6099 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6100 | } |
| 6101 | |
| 6102 | void PeerConnection::RemoveSctpDataStream(int sid) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6103 | if (data_channel_transport_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6104 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 6105 | if (data_channel_transport_) { |
| 6106 | data_channel_transport_->CloseChannel(sid); |
| 6107 | } |
| 6108 | }); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6109 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6110 | } |
| 6111 | |
| 6112 | bool PeerConnection::ReadyToSendData() const { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6113 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6114 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6115 | (data_channel_transport_ && data_channel_transport_ready_to_send_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6116 | } |
| 6117 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6118 | void PeerConnection::OnDataReceived(int channel_id, |
| 6119 | DataMessageType type, |
| 6120 | const rtc::CopyOnWriteBuffer& buffer) { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 6121 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6122 | cricket::ReceiveDataParams params; |
| 6123 | params.sid = channel_id; |
| 6124 | params.type = ToCricketDataMessageType(type); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6125 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6126 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 6127 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6128 | if (!HandleOpenMessage_s(params, buffer)) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6129 | SignalDataChannelTransportReceivedData_s(params, buffer); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6130 | } |
| 6131 | }); |
| 6132 | } |
| 6133 | |
| 6134 | void PeerConnection::OnChannelClosing(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 6135 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6136 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6137 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 6138 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6139 | SignalDataChannelTransportChannelClosing_s(channel_id); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6140 | }); |
| 6141 | } |
| 6142 | |
| 6143 | void PeerConnection::OnChannelClosed(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 6144 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6145 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6146 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 6147 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6148 | SignalDataChannelTransportChannelClosed_s(channel_id); |
| 6149 | }); |
| 6150 | } |
| 6151 | |
| 6152 | void PeerConnection::OnReadyToSend() { |
| 6153 | RTC_DCHECK_RUN_ON(network_thread()); |
| 6154 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 6155 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 6156 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6157 | data_channel_transport_ready_to_send_ = true; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6158 | SignalDataChannelTransportWritable_s( |
| 6159 | data_channel_transport_ready_to_send_); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6160 | }); |
| 6161 | } |
| 6162 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6163 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 6164 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6165 | if (sctp_mid_ && transport_controller_) { |
| 6166 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 6167 | if (dtls_transport) { |
| 6168 | return dtls_transport->transport_name(); |
| 6169 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6170 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6171 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6172 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6173 | } |
| 6174 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 6175 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 6176 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 6177 | network_thread()->Invoke<void>( |
| 6178 | RTC_FROM_HERE, |
| 6179 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 6180 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 6181 | return candidate_states_list; |
| 6182 | } |
| 6183 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6184 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 6185 | const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 6186 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6187 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6188 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6189 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6190 | if (channel) { |
| 6191 | transport_names_by_mid[channel->content_name()] = |
| 6192 | channel->transport_name(); |
| 6193 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6194 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6195 | if (rtp_data_channel_) { |
| 6196 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 6197 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6198 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6199 | if (data_channel_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6200 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6201 | RTC_DCHECK(transport_name); |
| 6202 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6203 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6204 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6205 | } |
| 6206 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6207 | std::map<std::string, cricket::TransportStats> |
| 6208 | PeerConnection::GetTransportStatsByNames( |
| 6209 | const std::set<std::string>& transport_names) { |
| 6210 | if (!network_thread()->IsCurrent()) { |
| 6211 | return network_thread() |
| 6212 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 6213 | RTC_FROM_HERE, |
| 6214 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6215 | } |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 6216 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6217 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 6218 | for (const std::string& transport_name : transport_names) { |
| 6219 | cricket::TransportStats transport_stats; |
| 6220 | bool success = |
| 6221 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 6222 | if (success) { |
| 6223 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 6224 | } else { |
| 6225 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 6226 | << transport_name; |
| 6227 | } |
| 6228 | } |
| 6229 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | bool PeerConnection::GetLocalCertificate( |
| 6233 | const std::string& transport_name, |
| 6234 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6235 | if (!certificate) { |
| 6236 | return false; |
| 6237 | } |
| 6238 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 6239 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6240 | } |
| 6241 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 6242 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6243 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 6244 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 6248 | return data_channel_type_; |
| 6249 | } |
| 6250 | |
| 6251 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 15:36:53 +0200 | [diff] [blame] | 6252 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6253 | return pending_ice_restarts_.find(content_name) != |
| 6254 | pending_ice_restarts_.end(); |
| 6255 | } |
| 6256 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6257 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 6258 | return transport_controller_->NeedsIceRestart(content_name); |
| 6259 | } |
| 6260 | |
| 6261 | void PeerConnection::OnCertificateReady( |
| 6262 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 6263 | transport_controller_->SetLocalCertificate(certificate); |
| 6264 | } |
| 6265 | |
| 6266 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6267 | SetSessionError(SessionError::kTransport, |
| 6268 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6269 | } |
| 6270 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 6271 | void PeerConnection::OnTransportControllerConnectionState( |
| 6272 | cricket::IceConnectionState state) { |
| 6273 | switch (state) { |
| 6274 | case cricket::kIceConnectionConnecting: |
| 6275 | // If the current state is Connected or Completed, then there were |
| 6276 | // writable channels but now there are not, so the next state must |
| 6277 | // be Disconnected. |
| 6278 | // kIceConnectionConnecting is currently used as the default, |
| 6279 | // un-connected state by the TransportController, so its only use is |
| 6280 | // detecting disconnections. |
| 6281 | if (ice_connection_state_ == |
| 6282 | PeerConnectionInterface::kIceConnectionConnected || |
| 6283 | ice_connection_state_ == |
| 6284 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 6285 | SetIceConnectionState( |
| 6286 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 6287 | } |
| 6288 | break; |
| 6289 | case cricket::kIceConnectionFailed: |
| 6290 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 6291 | break; |
| 6292 | case cricket::kIceConnectionConnected: |
| 6293 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 6294 | "all transports are writable."; |
| 6295 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6296 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6297 | break; |
| 6298 | case cricket::kIceConnectionCompleted: |
| 6299 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 6300 | "all transports are complete."; |
| 6301 | if (ice_connection_state_ != |
| 6302 | PeerConnectionInterface::kIceConnectionConnected) { |
| 6303 | // If jumping directly from "checking" to "connected", |
| 6304 | // signal "connected" first. |
| 6305 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6306 | } |
| 6307 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 6308 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6309 | ReportTransportStats(); |
| 6310 | break; |
| 6311 | default: |
| 6312 | RTC_NOTREACHED(); |
| 6313 | } |
| 6314 | } |
| 6315 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6316 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 6317 | const std::string& transport_name, |
| 6318 | const cricket::Candidates& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6319 | int sdp_mline_index; |
| 6320 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6321 | RTC_LOG(LS_ERROR) |
| 6322 | << "OnTransportControllerCandidatesGathered: content name " |
| 6323 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6324 | return; |
| 6325 | } |
| 6326 | |
| 6327 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 6328 | citer != candidates.end(); ++citer) { |
| 6329 | // Use transport_name as the candidate media id. |
| 6330 | std::unique_ptr<JsepIceCandidate> candidate( |
| 6331 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 6332 | if (local_description()) { |
| 6333 | mutable_local_description()->AddCandidate(candidate.get()); |
| 6334 | } |
| 6335 | OnIceCandidate(std::move(candidate)); |
| 6336 | } |
| 6337 | } |
| 6338 | |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 6339 | void PeerConnection::OnTransportControllerCandidateError( |
| 6340 | const cricket::IceCandidateErrorEvent& event) { |
| 6341 | OnIceCandidateError(event.host_candidate, event.url, event.error_code, |
| 6342 | event.error_text); |
| 6343 | } |
| 6344 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6345 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 6346 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6347 | // Sanity check. |
| 6348 | for (const cricket::Candidate& candidate : candidates) { |
| 6349 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6350 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6351 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6352 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6353 | return; |
| 6354 | } |
| 6355 | } |
| 6356 | |
| 6357 | if (local_description()) { |
| 6358 | mutable_local_description()->RemoveCandidates(candidates); |
| 6359 | } |
| 6360 | OnIceCandidatesRemoved(candidates); |
| 6361 | } |
| 6362 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 6363 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 6364 | const cricket::CandidatePairChangeEvent& event) { |
| 6365 | OnSelectedCandidatePairChanged(event); |
| 6366 | } |
| 6367 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6368 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 6369 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6370 | RTC_HISTOGRAM_ENUMERATION( |
| 6371 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 6372 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6373 | } |
| 6374 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6375 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6376 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6377 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6378 | if (channel && !channel->enabled()) { |
| 6379 | channel->Enable(true); |
| 6380 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6381 | } |
| 6382 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6383 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6384 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6385 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | // Returns the media index for a local ice candidate given the content name. |
| 6389 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 6390 | const std::string& content_name, |
| 6391 | int* sdp_mline_index) { |
| 6392 | if (!local_description() || !sdp_mline_index) { |
| 6393 | return false; |
| 6394 | } |
| 6395 | |
| 6396 | bool content_found = false; |
| 6397 | const ContentInfos& contents = local_description()->description()->contents(); |
| 6398 | for (size_t index = 0; index < contents.size(); ++index) { |
| 6399 | if (contents[index].name == content_name) { |
| 6400 | *sdp_mline_index = static_cast<int>(index); |
| 6401 | content_found = true; |
| 6402 | break; |
| 6403 | } |
| 6404 | } |
| 6405 | return content_found; |
| 6406 | } |
| 6407 | |
| 6408 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 6409 | const SessionDescriptionInterface* remote_desc) { |
| 6410 | if (!remote_desc) { |
| 6411 | return true; |
| 6412 | } |
| 6413 | bool ret = true; |
| 6414 | |
| 6415 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 6416 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 6417 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 6418 | const IceCandidateInterface* candidate = candidates->at(n); |
| 6419 | bool valid = false; |
| 6420 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 6421 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6422 | RTC_LOG(LS_INFO) |
| 6423 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6424 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6425 | } |
| 6426 | continue; |
| 6427 | } |
| 6428 | ret = UseCandidate(candidate); |
| 6429 | if (!ret) { |
| 6430 | break; |
| 6431 | } |
| 6432 | } |
| 6433 | } |
| 6434 | return ret; |
| 6435 | } |
| 6436 | |
| 6437 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6438 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 6439 | FindContentInfo(remote_description(), candidate); |
| 6440 | if (!result.ok()) { |
| 6441 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. " |
| 6442 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6443 | return false; |
| 6444 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6445 | std::vector<cricket::Candidate> candidates; |
| 6446 | candidates.push_back(candidate->candidate()); |
| 6447 | // Invoking BaseSession method to handle remote candidates. |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6448 | RTCError error = transport_controller_->AddRemoteCandidates( |
| 6449 | result.value()->name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6450 | if (error.ok()) { |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6451 | ReportRemoteIceCandidateAdded(candidate->candidate()); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6452 | // Candidates successfully submitted for checking. |
| 6453 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6454 | ice_connection_state_ == |
| 6455 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6456 | // If state is New, then the session has just gotten its first remote ICE |
| 6457 | // candidates, so go to Checking. |
| 6458 | // If state is Disconnected, the session is re-using old candidates or |
| 6459 | // receiving additional ones, so go to Checking. |
| 6460 | // If state is Connected, stay Connected. |
| 6461 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6462 | // newly added transport, then the state actually _should_ move to |
| 6463 | // checking. Add a way to distinguish that case. |
| 6464 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6465 | } |
| 6466 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 6467 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6468 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6469 | } |
| 6470 | return true; |
| 6471 | } |
| 6472 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6473 | RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo( |
| 6474 | const SessionDescriptionInterface* description, |
| 6475 | const IceCandidateInterface* candidate) { |
| 6476 | if (candidate->sdp_mline_index() >= 0) { |
| 6477 | size_t mediacontent_index = |
| 6478 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 6479 | size_t content_size = description->description()->contents().size(); |
| 6480 | if (mediacontent_index < content_size) { |
| 6481 | return &description->description()->contents()[mediacontent_index]; |
| 6482 | } else { |
| 6483 | return RTCError(RTCErrorType::INVALID_RANGE, |
| 6484 | "Media line index (" + |
| 6485 | rtc::ToString(candidate->sdp_mline_index()) + |
| 6486 | ") out of range (number of mlines: " + |
| 6487 | rtc::ToString(content_size) + ")."); |
| 6488 | } |
| 6489 | } else if (!candidate->sdp_mid().empty()) { |
| 6490 | auto& contents = description->description()->contents(); |
| 6491 | auto it = absl::c_find_if( |
| 6492 | contents, [candidate](const cricket::ContentInfo& content_info) { |
| 6493 | return content_info.mid() == candidate->sdp_mid(); |
| 6494 | }); |
| 6495 | if (it == contents.end()) { |
| 6496 | return RTCError( |
| 6497 | RTCErrorType::INVALID_PARAMETER, |
| 6498 | "Mid " + candidate->sdp_mid() + |
| 6499 | " specified but no media section with that mid found."); |
| 6500 | } else { |
| 6501 | return &*it; |
| 6502 | } |
| 6503 | } |
| 6504 | |
| 6505 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 6506 | "Neither sdp_mline_index nor sdp_mid specified."); |
| 6507 | } |
| 6508 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6509 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6510 | // Destroy video channel first since it may have a pointer to the |
| 6511 | // voice channel. |
| 6512 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6513 | if (!video_info || video_info->rejected) { |
| 6514 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6515 | } |
| 6516 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6517 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6518 | if (!audio_info || audio_info->rejected) { |
| 6519 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6523 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6524 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6525 | } |
| 6526 | } |
| 6527 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6528 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6529 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6530 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6531 | if (configuration_.bundle_policy == |
| 6532 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6533 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6534 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6535 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6536 | "max-bundle configured but session description " |
| 6537 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6538 | } |
| 6539 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 13:47:42 +0100 | [diff] [blame] | 6540 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6541 | } |
| 6542 | |
| 6543 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6544 | // Creating the media channels. Transports should already have been created |
| 6545 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6546 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6547 | if (voice && !voice->rejected && |
| 6548 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6549 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6550 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6551 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6552 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6553 | } |
| 6554 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 6555 | } |
| 6556 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6557 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6558 | if (video && !video->rejected && |
| 6559 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6560 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6561 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6562 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6563 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6564 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6565 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6566 | } |
| 6567 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6568 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6569 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6570 | !rtp_data_channel_ && !data_channel_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6571 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6572 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6573 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6574 | } |
| 6575 | } |
| 6576 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6577 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6578 | } |
| 6579 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6580 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6581 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6582 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6583 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6584 | MediaTransportConfig media_transport_config = |
| 6585 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6586 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6587 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6588 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6589 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 6590 | GetCryptoOptions(), &ssrc_generator_, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6591 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6592 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6593 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6594 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6595 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6596 | voice_channel->SignalSentPacket.connect(this, |
| 6597 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6598 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6599 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6600 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6601 | } |
| 6602 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6603 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6604 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6605 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6606 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6607 | MediaTransportConfig media_transport_config = |
| 6608 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6609 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6610 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6611 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6612 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 6613 | GetCryptoOptions(), &ssrc_generator_, video_options_, |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6614 | video_bitrate_allocator_factory_.get()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6615 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6616 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6617 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6618 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6619 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6620 | video_channel->SignalSentPacket.connect(this, |
| 6621 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6622 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6623 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6624 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6625 | } |
| 6626 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6627 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6628 | switch (data_channel_type_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6629 | case cricket::DCT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6630 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6631 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
| 6632 | case cricket::DCT_MEDIA_TRANSPORT: |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6633 | if (!network_thread()->Invoke<bool>( |
| 6634 | RTC_FROM_HERE, |
| 6635 | rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this, |
| 6636 | mid))) { |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6637 | return false; |
| 6638 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6639 | |
| 6640 | // All non-RTP data channels must initialize |sctp_data_channels_|. |
| 6641 | for (const auto& channel : sctp_data_channels_) { |
| 6642 | channel->OnTransportChannelCreated(); |
| 6643 | } |
| 6644 | return true; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6645 | case cricket::DCT_RTP: |
| 6646 | default: |
| 6647 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 6648 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 6649 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6650 | SrtpRequired(), GetCryptoOptions(), &ssrc_generator_); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6651 | if (!rtp_data_channel_) { |
| 6652 | return false; |
| 6653 | } |
| 6654 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 6655 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6656 | rtp_data_channel_->SignalSentPacket.connect( |
| 6657 | this, &PeerConnection::OnSentPacket_w); |
| 6658 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 6659 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6660 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6661 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | Call::Stats PeerConnection::GetCallStats() { |
| 6665 | if (!worker_thread()->IsCurrent()) { |
| 6666 | return worker_thread()->Invoke<Call::Stats>( |
| 6667 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6668 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 6669 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6670 | if (call_) { |
| 6671 | return call_->GetStats(); |
| 6672 | } else { |
| 6673 | return Call::Stats(); |
| 6674 | } |
| 6675 | } |
| 6676 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6677 | bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6678 | DataChannelTransportInterface* transport = |
| 6679 | transport_controller_->GetDataChannelTransport(mid); |
| 6680 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 6681 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6682 | << "Data channel transport is not available for data channels, mid=" |
| 6683 | << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6684 | return false; |
| 6685 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6686 | RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6687 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6688 | data_channel_transport_ = transport; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 6689 | data_channel_transport_invoker_ = std::make_unique<rtc::AsyncInvoker>(); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6690 | sctp_mid_ = mid; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6691 | |
| 6692 | // Note: setting the data sink and checking initial state must be done last, |
| 6693 | // after setting up the data channel. Setting the data sink may trigger |
| 6694 | // callbacks to PeerConnection which require the transport to be completely |
| 6695 | // set up (eg. OnReadyToSend()). |
| 6696 | transport->SetDataSink(this); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6697 | return true; |
| 6698 | } |
| 6699 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6700 | void PeerConnection::TeardownDataChannelTransport_n() { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6701 | if (!sctp_mid_ && !data_channel_transport_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6702 | return; |
| 6703 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6704 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
| 6705 | << *sctp_mid_; |
| 6706 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6707 | // |sctp_mid_| may still be active through an SCTP transport. If not, unset |
| 6708 | // it. |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6709 | sctp_mid_.reset(); |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6710 | data_channel_transport_invoker_ = nullptr; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6711 | if (data_channel_transport_) { |
| 6712 | data_channel_transport_->SetDataSink(nullptr); |
| 6713 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6714 | data_channel_transport_ = nullptr; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6715 | } |
| 6716 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6717 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6718 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6719 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6720 | if (!bundle_enabled) |
| 6721 | return true; |
| 6722 | |
| 6723 | const cricket::ContentGroup* bundle_group = |
| 6724 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6725 | RTC_DCHECK(bundle_group != NULL); |
| 6726 | |
| 6727 | const cricket::ContentInfos& contents = desc->contents(); |
| 6728 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6729 | citer != contents.end(); ++citer) { |
| 6730 | const cricket::ContentInfo* content = (&*citer); |
| 6731 | RTC_DCHECK(content != NULL); |
| 6732 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 6733 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6734 | if (!HasRtcpMuxEnabled(content)) |
| 6735 | return false; |
| 6736 | } |
| 6737 | } |
| 6738 | // RTCP-MUX is enabled in all the contents. |
| 6739 | return true; |
| 6740 | } |
| 6741 | |
| 6742 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6743 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6744 | } |
| 6745 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6746 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6747 | std::set<std::string> mids; |
| 6748 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 11:32:20 -0800 | [diff] [blame] | 6749 | if (content.name.empty()) { |
| 6750 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6751 | "A media section is missing a MID attribute."); |
| 6752 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6753 | if (!mids.insert(content.name).second) { |
| 6754 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6755 | "Duplicate a=mid value '" + content.name + "'."); |
| 6756 | } |
| 6757 | } |
| 6758 | return RTCError::OK(); |
| 6759 | } |
| 6760 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6761 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6762 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6763 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6764 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6765 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6769 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6770 | } |
| 6771 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6772 | SdpType type = sdesc->GetType(); |
| 6773 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6774 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6775 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 6776 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6777 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6778 | } |
| 6779 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6780 | RTCError error = ValidateMids(*sdesc->description()); |
| 6781 | if (!error.ok()) { |
| 6782 | return error; |
| 6783 | } |
| 6784 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6785 | // Verify crypto settings. |
| 6786 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6787 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6788 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6789 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6790 | if (!crypto_error.ok()) { |
| 6791 | return crypto_error; |
| 6792 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6793 | } |
| 6794 | |
| 6795 | // Verify ice-ufrag and ice-pwd. |
| 6796 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6797 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6798 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6802 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6803 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6804 | } |
| 6805 | |
| 6806 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6807 | // m-lines that do not rtcp-mux enabled. |
| 6808 | |
| 6809 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6810 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6811 | // With an answer we want to compare the new answer session description with |
| 6812 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6813 | const cricket::SessionDescription* offer_desc = |
| 6814 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6815 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6816 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6817 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6818 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6819 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6820 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6821 | } |
| 6822 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6823 | // The re-offers should respect the order of m= sections in current |
| 6824 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6825 | // With a re-offer, either the current local or current remote descriptions |
| 6826 | // could be the most up to date, so we would like to check against both of |
| 6827 | // them if they exist. It could be the case that one of them has a 0 port |
| 6828 | // for a media section, but the other does not. This is important to check |
| 6829 | // against in the case that we are recycling an m= section. |
| 6830 | const cricket::SessionDescription* current_desc = nullptr; |
| 6831 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6832 | if (local_description()) { |
| 6833 | current_desc = local_description()->description(); |
| 6834 | if (remote_description()) { |
| 6835 | secondary_current_desc = remote_description()->description(); |
| 6836 | } |
| 6837 | } else if (remote_description()) { |
| 6838 | current_desc = remote_description()->description(); |
| 6839 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6840 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6841 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6842 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6843 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6844 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6845 | } |
| 6846 | } |
| 6847 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6848 | if (IsUnifiedPlan()) { |
| 6849 | // Ensure that each audio and video media section has at most one |
| 6850 | // "StreamParams". This will return an error if receiving a session |
| 6851 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6852 | // same type. With Unified Plan, there can only be at most one track per |
| 6853 | // media section. |
| 6854 | for (const ContentInfo& content : sdesc->description()->contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 6855 | const MediaContentDescription& desc = *content.media_description(); |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6856 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6857 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6858 | desc.streams().size() > 1u) { |
| 6859 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6860 | "Media section has more than one track specified " |
| 6861 | "with a=ssrc lines which is not supported with " |
| 6862 | "Unified Plan."); |
| 6863 | } |
| 6864 | } |
| 6865 | } |
| 6866 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6867 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6868 | } |
| 6869 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6870 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6871 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6872 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6873 | return (state == PeerConnectionInterface::kStable) || |
| 6874 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6875 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6876 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6877 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6878 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6879 | } |
| 6880 | } |
| 6881 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6882 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6883 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6884 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6885 | return (state == PeerConnectionInterface::kStable) || |
| 6886 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6887 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6888 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6889 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6890 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6891 | } |
| 6892 | } |
| 6893 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6894 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6895 | switch (error) { |
| 6896 | case SessionError::kNone: |
| 6897 | return "ERROR_NONE"; |
| 6898 | case SessionError::kContent: |
| 6899 | return "ERROR_CONTENT"; |
| 6900 | case SessionError::kTransport: |
| 6901 | return "ERROR_TRANSPORT"; |
| 6902 | } |
| 6903 | RTC_NOTREACHED(); |
| 6904 | return ""; |
| 6905 | } |
| 6906 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6907 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 6908 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6909 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6910 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 6911 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6912 | } |
| 6913 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6914 | void PeerConnection::ReportSdpFormatReceived( |
| 6915 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6916 | int num_audio_mlines = 0; |
| 6917 | int num_video_mlines = 0; |
| 6918 | int num_audio_tracks = 0; |
| 6919 | int num_video_tracks = 0; |
| 6920 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6921 | cricket::MediaType media_type = content.media_description()->type(); |
| 6922 | int num_tracks = std::max( |
| 6923 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6924 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6925 | num_audio_mlines += 1; |
| 6926 | num_audio_tracks += num_tracks; |
| 6927 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6928 | num_video_mlines += 1; |
| 6929 | num_video_tracks += num_tracks; |
| 6930 | } |
| 6931 | } |
| 6932 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6933 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6934 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6935 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6936 | format = kSdpFormatReceivedComplexPlanB; |
| 6937 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6938 | format = kSdpFormatReceivedSimple; |
| 6939 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6940 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6941 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6942 | } |
| 6943 | |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6944 | void PeerConnection::ReportIceCandidateCollected( |
| 6945 | const cricket::Candidate& candidate) { |
| 6946 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 6947 | if (candidate.address().IsPrivateIP()) { |
| 6948 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 6949 | } |
| 6950 | if (candidate.address().IsUnresolvedIP()) { |
| 6951 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 6952 | } |
| 6953 | if (candidate.address().family() == AF_INET6) { |
| 6954 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 6955 | } |
| 6956 | } |
| 6957 | |
| 6958 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 6959 | const cricket::Candidate& candidate) { |
| 6960 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 6961 | if (candidate.address().IsPrivateIP()) { |
| 6962 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 6963 | } |
| 6964 | if (candidate.address().IsUnresolvedIP()) { |
| 6965 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 6966 | } |
| 6967 | if (candidate.address().family() == AF_INET6) { |
| 6968 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 6969 | } |
| 6970 | } |
| 6971 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6972 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6973 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6974 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6975 | } |
| 6976 | |
| 6977 | void PeerConnection::ReportUsagePattern() const { |
| 6978 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6979 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6980 | usage_event_accumulator_, |
| 6981 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6982 | const int bad_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6983 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6984 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6985 | const int good_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6986 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6987 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6988 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6989 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6990 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6991 | // If called after close(), we can't report, because observer may have |
| 6992 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6993 | if (observer_) { |
| 6994 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6995 | } else { |
| 6996 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6997 | << usage_event_accumulator_ |
| 6998 | << " observed after observer shutdown"; |
| 6999 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 7000 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 7001 | } |
| 7002 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7003 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 7004 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7005 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7006 | switch (answer.description()->msid_signaling()) { |
| 7007 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7008 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7009 | break; |
| 7010 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7011 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7012 | break; |
| 7013 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7014 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7015 | break; |
| 7016 | case cricket::kMsidSignalingMediaSection | |
| 7017 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7018 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7019 | break; |
| 7020 | default: |
| 7021 | RTC_NOTREACHED(); |
| 7022 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7023 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 7024 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 7025 | } |
| 7026 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7027 | // We need to check the local/remote description for the Transport instead of |
| 7028 | // the session, because a new Transport added during renegotiation may have |
| 7029 | // them unset while the session has them set from the previous negotiation. |
| 7030 | // Not doing so may trigger the auto generation of transport description and |
| 7031 | // mess up DTLS identity information, ICE credential, etc. |
| 7032 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 7033 | const IceCandidateInterface* candidate, |
| 7034 | const SessionDescriptionInterface* remote_desc, |
| 7035 | bool* valid) { |
| 7036 | *valid = true; |
| 7037 | |
| 7038 | const SessionDescriptionInterface* current_remote_desc = |
| 7039 | remote_desc ? remote_desc : remote_description(); |
| 7040 | |
| 7041 | if (!current_remote_desc) { |
| 7042 | return false; |
| 7043 | } |
| 7044 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 7045 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 7046 | FindContentInfo(current_remote_desc, candidate); |
| 7047 | if (!result.ok()) { |
| 7048 | RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. " |
| 7049 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7050 | |
| 7051 | *valid = false; |
| 7052 | return false; |
| 7053 | } |
| 7054 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 7055 | std::string transport_name = GetTransportName(result.value()->name); |
| 7056 | return !transport_name.empty(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7057 | } |
| 7058 | |
| 7059 | bool PeerConnection::SrtpRequired() const { |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 7060 | return !use_datagram_transport_ && |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 7061 | (dtls_enabled_ || |
| 7062 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7063 | } |
| 7064 | |
| 7065 | void PeerConnection::OnTransportControllerGatheringState( |
| 7066 | cricket::IceGatheringState state) { |
| 7067 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 7068 | if (state == cricket::kIceGatheringGathering) { |
| 7069 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 7070 | } else if (state == cricket::kIceGatheringComplete) { |
| 7071 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 7072 | } |
| 7073 | } |
| 7074 | |
| 7075 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7076 | std::map<std::string, std::set<cricket::MediaType>> |
| 7077 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 7078 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7079 | if (transceiver->internal()->channel()) { |
| 7080 | const std::string& transport_name = |
| 7081 | transceiver->internal()->channel()->transport_name(); |
| 7082 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 7083 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7084 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7085 | } |
| 7086 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7087 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 7088 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7089 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7090 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 7091 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7092 | if (transport_name) { |
| 7093 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7094 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7095 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7096 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7097 | for (const auto& entry : media_types_by_transport_name) { |
| 7098 | const std::string& transport_name = entry.first; |
| 7099 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7100 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7101 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7102 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7103 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7104 | } |
| 7105 | } |
| 7106 | } |
| 7107 | // Walk through the ConnectionInfos to gather best connection usage |
| 7108 | // for IPv4 and IPv6. |
| 7109 | void PeerConnection::ReportBestConnectionState( |
| 7110 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7111 | for (const cricket::TransportChannelStats& channel_stats : |
| 7112 | stats.channel_stats) { |
| 7113 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 7114 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7115 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7116 | continue; |
| 7117 | } |
| 7118 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7119 | const cricket::Candidate& local = connection_info.local_candidate; |
| 7120 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7121 | |
| 7122 | // Increment the counter for IceCandidatePairType. |
| 7123 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 7124 | (local.type() == RELAY_PORT_TYPE && |
| 7125 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7126 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 7127 | GetIceCandidatePairCounter(local, remote), |
| 7128 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7129 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7130 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 7131 | GetIceCandidatePairCounter(local, remote), |
| 7132 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7133 | } else { |
| 7134 | RTC_CHECK(0); |
| 7135 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7136 | |
| 7137 | // Increment the counter for IP type. |
| 7138 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7139 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7140 | kBestConnections_IPv4, |
| 7141 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7142 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7143 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7144 | kBestConnections_IPv6, |
| 7145 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7146 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 7147 | RTC_CHECK(!local.address().hostname().empty() && |
| 7148 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7149 | } |
| 7150 | |
| 7151 | return; |
| 7152 | } |
| 7153 | } |
| 7154 | } |
| 7155 | |
| 7156 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7157 | const cricket::TransportStats& stats, |
| 7158 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7159 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 7160 | return; |
| 7161 | } |
| 7162 | |
| 7163 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 7164 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 7165 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 7166 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7167 | return; |
| 7168 | } |
| 7169 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7170 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 7171 | for (cricket::MediaType media_type : media_types) { |
| 7172 | switch (media_type) { |
| 7173 | case cricket::MEDIA_TYPE_AUDIO: |
| 7174 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7175 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 7176 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7177 | break; |
| 7178 | case cricket::MEDIA_TYPE_VIDEO: |
| 7179 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7180 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 7181 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7182 | break; |
| 7183 | case cricket::MEDIA_TYPE_DATA: |
| 7184 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7185 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 7186 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7187 | break; |
| 7188 | default: |
| 7189 | RTC_NOTREACHED(); |
| 7190 | continue; |
| 7191 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7192 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7193 | } |
| 7194 | |
| 7195 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7196 | for (cricket::MediaType media_type : media_types) { |
| 7197 | switch (media_type) { |
| 7198 | case cricket::MEDIA_TYPE_AUDIO: |
| 7199 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7200 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 7201 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7202 | break; |
| 7203 | case cricket::MEDIA_TYPE_VIDEO: |
| 7204 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7205 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 7206 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7207 | break; |
| 7208 | case cricket::MEDIA_TYPE_DATA: |
| 7209 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7210 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 7211 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7212 | break; |
| 7213 | default: |
| 7214 | RTC_NOTREACHED(); |
| 7215 | continue; |
| 7216 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7217 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7218 | } |
| 7219 | } |
| 7220 | |
| 7221 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 7222 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7223 | RTC_DCHECK(call_); |
| 7224 | call_->OnSentPacket(sent_packet); |
| 7225 | } |
| 7226 | |
| 7227 | const std::string PeerConnection::GetTransportName( |
| 7228 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7229 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7230 | if (channel) { |
| 7231 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7232 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7233 | if (data_channel_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7234 | RTC_DCHECK(sctp_mid_); |
| 7235 | if (content_name == *sctp_mid_) { |
| 7236 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7237 | } |
| 7238 | } |
| 7239 | // Return an empty string if failed to retrieve the transport name. |
| 7240 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7241 | } |
| 7242 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7243 | void PeerConnection::DestroyTransceiverChannel( |
| 7244 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 7245 | transceiver) { |
| 7246 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7247 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7248 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7249 | if (channel) { |
| 7250 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7251 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7252 | } |
| 7253 | } |
| 7254 | |
| 7255 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7256 | if (rtp_data_channel_) { |
| 7257 | OnDataChannelDestroyed(); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7258 | DestroyChannelInterface(rtp_data_channel_); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7259 | rtp_data_channel_ = nullptr; |
| 7260 | } |
| 7261 | |
| 7262 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 7263 | // grab a reference to this PeerConnection. If this is called from the |
| 7264 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 7265 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 7266 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 7267 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7268 | if (sctp_mid_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7269 | OnDataChannelDestroyed(); |
| 7270 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 7271 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7272 | TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7273 | }); |
| 7274 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7275 | } |
| 7276 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7277 | void PeerConnection::DestroyChannelInterface( |
| 7278 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7279 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7280 | switch (channel->media_type()) { |
| 7281 | case cricket::MEDIA_TYPE_AUDIO: |
| 7282 | channel_manager()->DestroyVoiceChannel( |
| 7283 | static_cast<cricket::VoiceChannel*>(channel)); |
| 7284 | break; |
| 7285 | case cricket::MEDIA_TYPE_VIDEO: |
| 7286 | channel_manager()->DestroyVideoChannel( |
| 7287 | static_cast<cricket::VideoChannel*>(channel)); |
| 7288 | break; |
| 7289 | case cricket::MEDIA_TYPE_DATA: |
| 7290 | channel_manager()->DestroyRtpDataChannel( |
| 7291 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 7292 | break; |
| 7293 | default: |
| 7294 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 7295 | break; |
| 7296 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7297 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7298 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7299 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7300 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7301 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 7302 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7303 | MediaTransportInterface* media_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7304 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 13:08:37 +0100 | [diff] [blame] | 7305 | RTC_DCHECK_RUN_ON(network_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 7306 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7307 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7308 | auto base_channel = GetChannel(mid); |
| 7309 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7310 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7311 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 7312 | |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 7313 | if (use_media_transport_) { |
Tommi | 78a7138 | 2019-08-08 12:27:53 +0200 | [diff] [blame] | 7314 | RTC_LOG(LS_ERROR) << "Media transport isn't supported."; |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 7315 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 7316 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7317 | if (data_channel_transport_ && mid == sctp_mid_ && |
| 7318 | data_channel_transport_ != data_channel_transport) { |
| 7319 | // Changed which data channel transport is used for |sctp_mid_| (eg. now |
| 7320 | // it's bundled). |
| 7321 | data_channel_transport_->SetDataSink(nullptr); |
| 7322 | data_channel_transport_ = data_channel_transport; |
| 7323 | if (data_channel_transport) { |
| 7324 | data_channel_transport->SetDataSink(this); |
| 7325 | |
| 7326 | // There's a new data channel transport. This needs to be signaled to the |
| 7327 | // |sctp_data_channels_| so that they can reopen and reconnect. This is |
| 7328 | // necessary when bundling is applied. |
| 7329 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 7330 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 7331 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7332 | for (auto channel : sctp_data_channels_) { |
| 7333 | channel->OnTransportChannelCreated(); |
| 7334 | } |
| 7335 | }); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7336 | } |
| 7337 | } |
| 7338 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7339 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7340 | } |
| 7341 | |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 7342 | void PeerConnection::OnSetStreams() { |
| 7343 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7344 | if (IsUnifiedPlan()) |
| 7345 | UpdateNegotiationNeeded(); |
| 7346 | } |
| 7347 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7348 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 7349 | // In earlier production code, the pointer was not cleared on close, |
| 7350 | // which might have led to undefined behavior if the observer was not |
| 7351 | // deallocated, or strange crashes if it was. |
| 7352 | // We use CHECK in order to catch such behavior if it exists. |
| 7353 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 7354 | RTC_CHECK(observer_); |
| 7355 | return observer_; |
| 7356 | } |
| 7357 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 7358 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 7359 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 7360 | // after it has been removed. |
| 7361 | return configuration_.crypto_options.has_value() |
| 7362 | ? *configuration_.crypto_options |
| 7363 | : factory_->options().crypto_options; |
| 7364 | } |
| 7365 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 7366 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 7367 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 7368 | if (stats_collector_) { |
| 7369 | stats_collector_->ClearCachedStatsReport(); |
| 7370 | } |
| 7371 | } |
| 7372 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7373 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 7374 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 7375 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7376 | } |
| 7377 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7378 | void PeerConnection::UpdateNegotiationNeeded() { |
| 7379 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7380 | if (!IsUnifiedPlan()) { |
| 7381 | Observer()->OnRenegotiationNeeded(); |
| 7382 | return; |
| 7383 | } |
| 7384 | |
| 7385 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7386 | if (IsClosed()) |
| 7387 | return; |
| 7388 | |
| 7389 | // If connection's signaling state is not "stable", abort these steps. |
| 7390 | if (signaling_state() != kStable) |
| 7391 | return; |
| 7392 | |
| 7393 | // NOTE |
| 7394 | // The negotiation-needed flag will be updated once the state transitions to |
| 7395 | // "stable", as part of the steps for setting an RTCSessionDescription. |
| 7396 | |
| 7397 | // If the result of checking if negotiation is needed is false, clear the |
| 7398 | // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot |
| 7399 | // to false, and abort these steps. |
| 7400 | bool is_negotiation_needed = CheckIfNegotiationIsNeeded(); |
| 7401 | if (!is_negotiation_needed) { |
| 7402 | is_negotiation_needed_ = false; |
| 7403 | return; |
| 7404 | } |
| 7405 | |
| 7406 | // If connection's [[NegotiationNeeded]] slot is already true, abort these |
| 7407 | // steps. |
| 7408 | if (is_negotiation_needed_) |
| 7409 | return; |
| 7410 | |
| 7411 | // Set connection's [[NegotiationNeeded]] slot to true. |
| 7412 | is_negotiation_needed_ = true; |
| 7413 | |
| 7414 | // Queue a task that runs the following steps: |
| 7415 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7416 | // If connection's [[NegotiationNeeded]] slot is false, abort these steps. |
| 7417 | // Fire an event named negotiationneeded at connection. |
| 7418 | Observer()->OnRenegotiationNeeded(); |
| 7419 | } |
| 7420 | |
| 7421 | bool PeerConnection::CheckIfNegotiationIsNeeded() { |
| 7422 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7423 | // 1. If any implementation-specific negotiation is required, as described at |
| 7424 | // the start of this section, return true. |
| 7425 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7426 | // 2. If connection's [[RestartIce]] internal slot is true, return true. |
| 7427 | if (local_ice_credentials_to_replace_->HasIceCredentials()) { |
| 7428 | return true; |
| 7429 | } |
| 7430 | |
| 7431 | // 3. Let description be connection.[[CurrentLocalDescription]]. |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7432 | const SessionDescriptionInterface* description = current_local_description(); |
| 7433 | if (!description) |
| 7434 | return true; |
| 7435 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7436 | // 4. If connection has created any RTCDataChannels, and no m= section in |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7437 | // description has been negotiated yet for data, return true. |
| 7438 | if (!sctp_data_channels_.empty()) { |
| 7439 | if (!cricket::GetFirstDataContent(description->description()->contents())) |
| 7440 | return true; |
| 7441 | } |
| 7442 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7443 | // 5. For each transceiver in connection's set of transceivers, perform the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7444 | // following checks: |
| 7445 | for (const auto& transceiver : transceivers_) { |
| 7446 | const ContentInfo* current_local_msection = |
| 7447 | FindTransceiverMSection(transceiver.get(), description); |
| 7448 | |
| 7449 | const ContentInfo* current_remote_msection = FindTransceiverMSection( |
| 7450 | transceiver.get(), current_remote_description()); |
| 7451 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7452 | // 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] | 7453 | // but the associated m= section is not yet rejected in |
| 7454 | // connection.[[CurrentLocalDescription]] or |
| 7455 | // connection.[[CurrentRemoteDescription]], return true. |
| 7456 | if (transceiver->stopped()) { |
| 7457 | if (current_local_msection && !current_local_msection->rejected && |
| 7458 | ((current_remote_msection && !current_remote_msection->rejected) || |
| 7459 | !current_remote_msection)) { |
| 7460 | return true; |
| 7461 | } |
| 7462 | continue; |
| 7463 | } |
| 7464 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7465 | // 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] | 7466 | // section in description, return true. |
| 7467 | if (!current_local_msection) |
| 7468 | return true; |
| 7469 | |
| 7470 | const MediaContentDescription* current_local_media_description = |
| 7471 | current_local_msection->media_description(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7472 | // 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] | 7473 | // in description then perform the following checks: |
| 7474 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7475 | // 5.2.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7476 | // associated m= section in description either doesn't contain a single |
| 7477 | // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this |
| 7478 | // m= section, or the MSID values themselves, differ from what is in |
| 7479 | // transceiver.sender.[[AssociatedMediaStreamIds]], return true. |
| 7480 | if (RtpTransceiverDirectionHasSend(transceiver->direction())) { |
| 7481 | if (current_local_media_description->streams().size() == 0) |
| 7482 | return true; |
| 7483 | |
| 7484 | std::vector<std::string> msection_msids; |
| 7485 | for (const auto& stream : current_local_media_description->streams()) { |
| 7486 | for (const std::string& msid : stream.stream_ids()) |
| 7487 | msection_msids.push_back(msid); |
| 7488 | } |
| 7489 | |
| 7490 | std::vector<std::string> transceiver_msids = |
| 7491 | transceiver->sender()->stream_ids(); |
| 7492 | if (msection_msids.size() != transceiver_msids.size()) |
| 7493 | return true; |
| 7494 | |
| 7495 | absl::c_sort(transceiver_msids); |
| 7496 | absl::c_sort(msection_msids); |
| 7497 | if (transceiver_msids != msection_msids) |
| 7498 | return true; |
| 7499 | } |
| 7500 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7501 | // 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] | 7502 | // associated m= section in neither connection.[[CurrentLocalDescription]] |
| 7503 | // nor connection.[[CurrentRemoteDescription]] matches |
| 7504 | // transceiver.[[Direction]], return true. |
| 7505 | if (description->GetType() == SdpType::kOffer) { |
| 7506 | if (!current_remote_description()) |
| 7507 | return true; |
| 7508 | |
| 7509 | if (!current_remote_msection) |
| 7510 | return true; |
| 7511 | |
| 7512 | RtpTransceiverDirection current_local_direction = |
| 7513 | current_local_media_description->direction(); |
| 7514 | RtpTransceiverDirection current_remote_direction = |
| 7515 | current_remote_msection->media_description()->direction(); |
| 7516 | if (transceiver->direction() != current_local_direction && |
| 7517 | transceiver->direction() != |
| 7518 | RtpTransceiverDirectionReversed(current_remote_direction)) { |
| 7519 | return true; |
| 7520 | } |
| 7521 | } |
| 7522 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7523 | // 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] | 7524 | // associated m= section in the description does not match |
| 7525 | // transceiver.[[Direction]] intersected with the offered direction (as |
| 7526 | // described in [JSEP] (section 5.3.1.)), return true. |
| 7527 | if (description->GetType() == SdpType::kAnswer) { |
| 7528 | if (!remote_description()) |
| 7529 | return true; |
| 7530 | |
| 7531 | const ContentInfo* offered_remote_msection = |
| 7532 | FindTransceiverMSection(transceiver.get(), remote_description()); |
| 7533 | |
| 7534 | RtpTransceiverDirection offered_direction = |
| 7535 | offered_remote_msection |
| 7536 | ? offered_remote_msection->media_description()->direction() |
| 7537 | : RtpTransceiverDirection::kInactive; |
| 7538 | |
| 7539 | if (current_local_media_description->direction() != |
| 7540 | (RtpTransceiverDirectionIntersection( |
| 7541 | transceiver->direction(), |
| 7542 | RtpTransceiverDirectionReversed(offered_direction)))) { |
| 7543 | return true; |
| 7544 | } |
| 7545 | } |
| 7546 | } |
| 7547 | |
| 7548 | // If all the preceding checks were performed and true was not returned, |
| 7549 | // nothing remains to be negotiated; return false. |
| 7550 | return false; |
| 7551 | } |
| 7552 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 7553 | } // namespace webrtc |