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; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 781 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 782 | bool offer_extmap_allow_mixed; |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 783 | std::string turn_logging_id; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 784 | }; |
| 785 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 786 | "Did you add something to RTCConfiguration and forget to " |
| 787 | "update operator==?"); |
| 788 | return type == o.type && servers == o.servers && |
| 789 | bundle_policy == o.bundle_policy && |
| 790 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 791 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 792 | candidate_network_policy == o.candidate_network_policy && |
| 793 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 794 | audio_jitter_buffer_fast_accelerate == |
| 795 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 796 | audio_jitter_buffer_min_delay_ms == |
| 797 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 798 | audio_jitter_buffer_enable_rtx_handling == |
| 799 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 800 | ice_connection_receiving_timeout == |
| 801 | o.ice_connection_receiving_timeout && |
| 802 | ice_backup_candidate_pair_ping_interval == |
| 803 | o.ice_backup_candidate_pair_ping_interval && |
| 804 | continual_gathering_policy == o.continual_gathering_policy && |
| 805 | certificates == o.certificates && |
| 806 | prioritize_most_likely_ice_candidate_pairs == |
| 807 | o.prioritize_most_likely_ice_candidate_pairs && |
| 808 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 809 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 810 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 811 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 812 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 813 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 814 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 815 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 816 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 817 | prune_turn_ports == o.prune_turn_ports && |
| 818 | presume_writable_when_fully_relayed == |
| 819 | o.presume_writable_when_fully_relayed && |
| 820 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 821 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 822 | surface_ice_candidates_on_ice_transport_type_changed == |
| 823 | o.surface_ice_candidates_on_ice_transport_type_changed && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 824 | ice_check_interval_strong_connectivity == |
| 825 | o.ice_check_interval_strong_connectivity && |
| 826 | ice_check_interval_weak_connectivity == |
| 827 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 828 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 829 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 830 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 831 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 832 | stun_candidate_keepalive_interval == |
| 833 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 834 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 835 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 836 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 837 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 838 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 839 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 840 | use_media_transport_for_data_channels == |
| 841 | o.use_media_transport_for_data_channels && |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 842 | use_datagram_transport == o.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 843 | use_datagram_transport_for_data_channels == |
| 844 | o.use_datagram_transport_for_data_channels && |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 845 | crypto_options == o.crypto_options && |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 846 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed && |
| 847 | turn_logging_id == o.turn_logging_id; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 851 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 852 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 853 | } |
| 854 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 855 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 856 | std::string GenerateRtcpCname() { |
| 857 | std::string cname; |
| 858 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 859 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 860 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 861 | } |
| 862 | return cname; |
| 863 | } |
| 864 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 865 | bool ValidateOfferAnswerOptions( |
| 866 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 867 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 868 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 869 | } |
| 870 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 871 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 872 | // m= sections (in other words, nothing that involves a map/array). |
| 873 | void ExtractSharedMediaSessionOptions( |
| 874 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 875 | cricket::MediaSessionOptions* session_options) { |
| 876 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 877 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
Mirta Dvornicic | 479a3c0 | 2019-06-04 15:38:50 +0200 | [diff] [blame] | 878 | session_options->raw_packetization_for_video = |
| 879 | rtc_options.raw_packetization_for_video; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 880 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 881 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 882 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 883 | std::unique_ptr<RtcEventLog> event_log, |
| 884 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 885 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 886 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 887 | event_log_ptr_(event_log_.get()), |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 888 | datagram_transport_config_( |
| 889 | field_trial::FindFullName(kDatagramTransportFieldTrial)), |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 890 | datagram_transport_data_channel_config_( |
| 891 | field_trial::FindFullName(kDatagramTransportDataChannelFieldTrial)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 892 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 893 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 894 | remote_streams_(StreamCollection::Create()), |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 895 | call_(std::move(call)), |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 896 | call_ptr_(call_.get()), |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 897 | data_channel_transport_(nullptr), |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 898 | local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 899 | |
| 900 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 901 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 902 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 903 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 904 | // Need to stop transceivers before destroying the stats collector because |
| 905 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 906 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 907 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 908 | transceiver->Stop(); |
| 909 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 910 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 911 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 912 | if (stats_collector_) { |
| 913 | stats_collector_->WaitForPendingRequest(); |
| 914 | stats_collector_ = nullptr; |
| 915 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 916 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 917 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 918 | // the last stats request can still read from the channels. |
| 919 | DestroyAllChannels(); |
| 920 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 921 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 922 | |
| 923 | webrtc_session_desc_factory_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 924 | sctp_factory_.reset(); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 925 | data_channel_transport_invoker_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 926 | transport_controller_.reset(); |
| 927 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 928 | // port_allocator_ lives on the network thread and should be destroyed there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 929 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 930 | RTC_DCHECK_RUN_ON(network_thread()); |
| 931 | port_allocator_.reset(); |
| 932 | }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 933 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 934 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 935 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 936 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 937 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 938 | event_log_.reset(); |
| 939 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 942 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 943 | // Destroy video channels first since they may have a pointer to a voice |
| 944 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 945 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 946 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 947 | DestroyTransceiverChannel(transceiver); |
| 948 | } |
| 949 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 950 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 951 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 952 | DestroyTransceiverChannel(transceiver); |
| 953 | } |
| 954 | } |
| 955 | DestroyDataChannel(); |
| 956 | } |
| 957 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 958 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 959 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 960 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 961 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 962 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 963 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 964 | |
| 965 | RTCError config_error = ValidateConfiguration(configuration); |
| 966 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 967 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 968 | return false; |
| 969 | } |
| 970 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 971 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 972 | RTC_LOG(LS_ERROR) |
| 973 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 974 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 975 | return false; |
| 976 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 977 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 978 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 979 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 980 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 981 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 982 | return false; |
| 983 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 984 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 985 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 986 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 987 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 988 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 989 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 990 | cricket::ServerAddresses stun_servers; |
| 991 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 992 | |
| 993 | RTCErrorType parse_error = |
| 994 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 995 | if (parse_error != RTCErrorType::NONE) { |
| 996 | return false; |
| 997 | } |
| 998 | |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 999 | // Add the turn logging id to all turn servers |
| 1000 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 1001 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 1002 | } |
| 1003 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 1004 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1005 | // there. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1006 | const auto pa_result = |
| 1007 | network_thread()->Invoke<InitializePortAllocatorResult>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 1008 | RTC_FROM_HERE, |
| 1009 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1010 | stun_servers, turn_servers, configuration)); |
| 1011 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 1012 | // If initialization was successful, note if STUN or TURN servers |
| 1013 | // were supplied. |
| 1014 | if (!stun_servers.empty()) { |
| 1015 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1016 | } |
| 1017 | if (!turn_servers.empty()) { |
| 1018 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1019 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1020 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1021 | // Send information about IPv4/IPv6 status. |
| 1022 | PeerConnectionAddressFamilyCounter address_family; |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 1023 | if (pa_result.enable_ipv6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1024 | address_family = kPeerConnection_IPv6; |
| 1025 | } else { |
| 1026 | address_family = kPeerConnection_IPv4; |
| 1027 | } |
| 1028 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 1029 | kPeerConnectionAddressFamilyCounter_Max); |
| 1030 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1031 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 1032 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1033 | // RFC 3264: The numeric value of the session id and version in the |
| 1034 | // o line MUST be representable with a "64 bit signed integer". |
| 1035 | // Due to this constraint session id |session_id_| is max limited to |
| 1036 | // LLONG_MAX. |
| 1037 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1038 | JsepTransportController::Config config; |
| 1039 | config.redetermine_role_on_ice_restart = |
| 1040 | configuration.redetermine_role_on_ice_restart; |
| 1041 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 1042 | config.disable_encryption = options.disable_encryption; |
| 1043 | config.bundle_policy = configuration.bundle_policy; |
| 1044 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1045 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 1046 | // stub. |
| 1047 | config.crypto_options = configuration.crypto_options.has_value() |
| 1048 | ? *configuration.crypto_options |
| 1049 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1050 | config.transport_observer = this; |
Sebastian Jansson | 1b83a9e | 2019-09-18 18:22:12 +0200 | [diff] [blame] | 1051 | // It's safe to pass |this| and using |rtcp_invoker_| and the |call_| pointer |
| 1052 | // since the JsepTransportController instance is owned by this PeerConnection |
| 1053 | // instance and is destroyed before both |rtcp_invoker_| and the |call_| |
| 1054 | // pointer. |
| 1055 | config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet, |
| 1056 | int64_t packet_time_us) { |
| 1057 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1058 | rtcp_invoker_.AsyncInvoke<void>( |
| 1059 | RTC_FROM_HERE, worker_thread(), [this, packet, packet_time_us] { |
| 1060 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1061 | // |call_| is reset on the worker thread in the PeerConnection |
| 1062 | // destructor, so we check that it's still valid before propagating |
| 1063 | // the packet. |
| 1064 | if (call_) { |
| 1065 | call_->Receiver()->DeliverPacket(MediaType::ANY, packet, |
| 1066 | packet_time_us); |
| 1067 | } |
| 1068 | }); |
| 1069 | }; |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 1070 | config.event_log = event_log_ptr_; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1071 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 1072 | config.enable_external_auth = true; |
| 1073 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 1074 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1075 | |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1076 | use_datagram_transport_ = datagram_transport_config_.enabled && |
Bjorn A Mellem | 238aab9 | 2019-07-02 11:06:00 -0700 | [diff] [blame] | 1077 | configuration.use_datagram_transport.value_or( |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1078 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1079 | use_datagram_transport_for_data_channels_ = |
| 1080 | datagram_transport_data_channel_config_.enabled && |
| 1081 | configuration.use_datagram_transport_for_data_channels.value_or( |
| 1082 | datagram_transport_data_channel_config_.default_value); |
| 1083 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_ || |
| 1084 | configuration.use_media_transport || |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 1085 | configuration.use_media_transport_for_data_channels) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1086 | if (!factory_->media_transport_factory()) { |
| 1087 | RTC_DCHECK(false) |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 1088 | << "PeerConnecton is initialized with use_media_transport = true or " |
| 1089 | << "use_media_transport_for_data_channels = true " |
| 1090 | << "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1091 | return false; |
| 1092 | } |
| 1093 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 1094 | if (configuration.use_media_transport || |
| 1095 | configuration.use_media_transport_for_data_channels) { |
| 1096 | // TODO(bugs.webrtc.org/9719): This check will eventually go away, when |
| 1097 | // RTP media transport is introduced. But until then, we require SDES to |
| 1098 | // be enabled. |
| 1099 | if (configuration.enable_dtls_srtp.has_value() && |
| 1100 | configuration.enable_dtls_srtp.value()) { |
| 1101 | RTC_LOG(LS_WARNING) |
| 1102 | << "When media transport is used, SDES must be enabled. Set " |
| 1103 | "configuration.enable_dtls_srtp to false. use_media_transport=" |
| 1104 | << configuration.use_media_transport |
| 1105 | << ", use_media_transport_for_data_channels=" |
| 1106 | << configuration.use_media_transport_for_data_channels; |
| 1107 | return false; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 1111 | config.use_media_transport_for_media = configuration.use_media_transport; |
| 1112 | config.use_media_transport_for_data_channels = |
| 1113 | configuration.use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 1114 | config.use_datagram_transport = use_datagram_transport_; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 1115 | config.use_datagram_transport_for_data_channels = |
| 1116 | use_datagram_transport_for_data_channels_; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1117 | config.media_transport_factory = factory_->media_transport_factory(); |
| 1118 | } |
| 1119 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 1120 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1121 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1122 | if (!configuration.certificates.empty()) { |
| 1123 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1124 | // just picking the first one. The decision should be made based on the DTLS |
| 1125 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1126 | certificate = configuration.certificates[0]; |
| 1127 | } |
| 1128 | |
| 1129 | if (options.disable_encryption) { |
| 1130 | dtls_enabled_ = false; |
| 1131 | } else { |
| 1132 | // Enable DTLS by default if we have an identity store or a certificate. |
| 1133 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
| 1134 | // |configuration| can override the default |dtls_enabled_| value. |
| 1135 | if (configuration.enable_dtls_srtp) { |
| 1136 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
| 1141 | |
| 1142 | if (use_datagram_transport_for_data_channels_) { |
| 1143 | if (configuration.enable_rtp_data_channel) { |
| 1144 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1145 | "use_datagram_transport_for_data_channels are " |
| 1146 | "incompatible and cannot both be set to true"; |
| 1147 | return false; |
| 1148 | } |
| 1149 | if (configuration.enable_dtls_srtp && !*configuration.enable_dtls_srtp) { |
| 1150 | RTC_LOG(LS_INFO) << "Using data channel transport with no fallback"; |
| 1151 | data_channel_type_ = cricket::DCT_DATA_CHANNEL_TRANSPORT; |
| 1152 | } else { |
| 1153 | RTC_LOG(LS_INFO) << "Using data channel transport with fallback to SCTP"; |
| 1154 | data_channel_type_ = cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP; |
| 1155 | config.sctp_factory = sctp_factory_.get(); |
| 1156 | } |
| 1157 | } else if (configuration.use_media_transport_for_data_channels) { |
| 1158 | if (configuration.enable_rtp_data_channel) { |
| 1159 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1160 | "use_media_transport_for_data_channels are " |
| 1161 | "incompatible and cannot both be set to true"; |
| 1162 | return false; |
| 1163 | } |
| 1164 | data_channel_type_ = cricket::DCT_MEDIA_TRANSPORT; |
| 1165 | } else if (configuration.enable_rtp_data_channel) { |
| 1166 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1167 | // set. It takes precendence over the disable_sctp_data_channels |
| 1168 | // PeerConnectionFactoryInterface::Options. |
| 1169 | data_channel_type_ = cricket::DCT_RTP; |
| 1170 | } else { |
| 1171 | // DTLS has to be enabled to use SCTP. |
| 1172 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1173 | data_channel_type_ = cricket::DCT_SCTP; |
| 1174 | config.sctp_factory = sctp_factory_.get(); |
| 1175 | } |
| 1176 | } |
| 1177 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1178 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 1179 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1180 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1181 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1182 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1183 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1184 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1185 | transport_controller_->SignalConnectionState.connect( |
| 1186 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1187 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1188 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1189 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1190 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 1191 | transport_controller_->SignalIceCandidateError.connect( |
| 1192 | this, &PeerConnection::OnTransportControllerCandidateError); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1193 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1194 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1195 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1196 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 1197 | transport_controller_->SignalIceCandidatePairChanged.connect( |
| 1198 | this, &PeerConnection::OnTransportControllerCandidateChanged); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1199 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1200 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1201 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1202 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1203 | configuration_ = configuration; |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1204 | use_media_transport_ = configuration.use_media_transport; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1205 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1206 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1207 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1208 | video_options_.screencast_min_bitrate_kbps = |
| 1209 | configuration.screencast_min_bitrate; |
| 1210 | audio_options_.combined_audio_video_bwe = |
| 1211 | configuration.combined_audio_video_bwe; |
| 1212 | |
| 1213 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1214 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1215 | |
| 1216 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1217 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1218 | |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 1219 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1220 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1221 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 1222 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1223 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1224 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1225 | // Whether the certificate generator/certificate is null or not determines |
| 1226 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1227 | // the right instructions by clearing the variables if needed. |
| 1228 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1229 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1230 | certificate = nullptr; |
| 1231 | } else if (certificate) { |
| 1232 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1233 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1237 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1238 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1239 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1240 | this, &PeerConnection::OnCertificateReady); |
| 1241 | |
| 1242 | if (options.disable_encryption) { |
| 1243 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1244 | } |
| 1245 | |
| 1246 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1247 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1248 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1249 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1250 | // Add default audio/video transceivers for Plan B SDP. |
| 1251 | if (!IsUnifiedPlan()) { |
| 1252 | transceivers_.push_back( |
| 1253 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1254 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1255 | transceivers_.push_back( |
| 1256 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1257 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1258 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1259 | int delay_ms = |
| 1260 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1261 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1262 | MSG_REPORT_USAGE_PATTERN, nullptr); |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 1263 | |
| 1264 | if (dependencies.video_bitrate_allocator_factory) { |
| 1265 | video_bitrate_allocator_factory_ = |
| 1266 | std::move(dependencies.video_bitrate_allocator_factory); |
| 1267 | } else { |
| 1268 | video_bitrate_allocator_factory_ = |
| 1269 | CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1270 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1271 | return true; |
| 1272 | } |
| 1273 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1274 | RTCError PeerConnection::ValidateConfiguration( |
| 1275 | const RTCConfiguration& config) const { |
| 1276 | if (config.ice_regather_interval_range && |
| 1277 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1278 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1279 | "ice_regather_interval_range specified but continual " |
| 1280 | "gathering policy is GATHER_ONCE"); |
| 1281 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1282 | auto result = |
| 1283 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1284 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1287 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1288 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1289 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1290 | "Plan SdpSemantics. Please use GetSenders " |
| 1291 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1292 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1295 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_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()) << "remote_streams is not available with Unified " |
| 1298 | "Plan SdpSemantics. Please use GetReceivers " |
| 1299 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1300 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1303 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +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()) << "AddStream is not available with Unified Plan " |
| 1306 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1307 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1308 | if (IsClosed()) { |
| 1309 | return false; |
| 1310 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1311 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1314 | |
| 1315 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1316 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1317 | observer->SignalAudioTrackAdded.connect(this, |
| 1318 | &PeerConnection::OnAudioTrackAdded); |
| 1319 | observer->SignalAudioTrackRemoved.connect( |
| 1320 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1321 | observer->SignalVideoTrackAdded.connect(this, |
| 1322 | &PeerConnection::OnVideoTrackAdded); |
| 1323 | observer->SignalVideoTrackRemoved.connect( |
| 1324 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1325 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1326 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1327 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1328 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1329 | } |
| 1330 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1331 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1334 | stats_->AddStream(local_stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1335 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1336 | return true; |
| 1337 | } |
| 1338 | |
| 1339 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1340 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1341 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1342 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1343 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1344 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1345 | if (!IsClosed()) { |
| 1346 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1347 | RemoveAudioTrack(track.get(), local_stream); |
| 1348 | } |
| 1349 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1350 | RemoveVideoTrack(track.get(), local_stream); |
| 1351 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1352 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1353 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1354 | stream_observers_.erase( |
| 1355 | std::remove_if( |
| 1356 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1357 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1358 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1359 | }), |
| 1360 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1361 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1362 | if (IsClosed()) { |
| 1363 | return; |
| 1364 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1365 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1368 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1369 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1370 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1371 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1372 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1373 | if (!track) { |
| 1374 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1375 | } |
| 1376 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1377 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1378 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1379 | "Track has invalid kind: " + track->kind()); |
| 1380 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1381 | if (IsClosed()) { |
| 1382 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1383 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1384 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1385 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1386 | LOG_AND_RETURN_ERROR( |
| 1387 | RTCErrorType::INVALID_PARAMETER, |
| 1388 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1389 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1390 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1391 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1392 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1393 | if (sender_or_error.ok()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1394 | UpdateNegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1395 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1396 | } |
| 1397 | return sender_or_error; |
| 1398 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1399 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1400 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1401 | PeerConnection::AddTrackPlanB( |
| 1402 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1403 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1404 | if (stream_ids.size() > 1u) { |
| 1405 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1406 | "AddTrack with more than one stream is not " |
| 1407 | "supported with Plan B semantics."); |
| 1408 | } |
| 1409 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1410 | if (adjusted_stream_ids.empty()) { |
| 1411 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1412 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1413 | cricket::MediaType media_type = |
| 1414 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1415 | ? cricket::MEDIA_TYPE_AUDIO |
| 1416 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1417 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1418 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1419 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1420 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1421 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1422 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1423 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1424 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1425 | if (sender_info) { |
| 1426 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1427 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1428 | } else { |
| 1429 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1430 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1431 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1432 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1433 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1434 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1435 | if (sender_info) { |
| 1436 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1437 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1438 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1439 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1440 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1441 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1442 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1443 | PeerConnection::AddTrackUnifiedPlan( |
| 1444 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1445 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1446 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1447 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1448 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1449 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1450 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1451 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1452 | transceiver->internal()->set_direction( |
| 1453 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1454 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1455 | transceiver->internal()->set_direction( |
| 1456 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1457 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1458 | transceiver->sender()->SetTrack(track); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1459 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1460 | } else { |
| 1461 | cricket::MediaType media_type = |
| 1462 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1463 | ? cricket::MEDIA_TYPE_AUDIO |
| 1464 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1465 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1466 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1467 | std::string sender_id = track->id(); |
| 1468 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1469 | // This can happen if this is the second time AddTrack has created a sender |
| 1470 | // for this track. |
| 1471 | if (FindSenderById(sender_id)) { |
| 1472 | sender_id = rtc::CreateRandomUuid(); |
| 1473 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1474 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1475 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1476 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1477 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1478 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1479 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1480 | return transceiver->sender(); |
| 1481 | } |
| 1482 | |
| 1483 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1484 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1485 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1486 | RTC_DCHECK(track); |
| 1487 | for (auto transceiver : transceivers_) { |
| 1488 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1489 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1490 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1491 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1492 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1493 | return transceiver; |
| 1494 | } |
| 1495 | } |
| 1496 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1500 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1501 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1504 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1505 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1506 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1507 | if (!sender) { |
| 1508 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1509 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1510 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1511 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1512 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1513 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1514 | if (IsUnifiedPlan()) { |
| 1515 | auto transceiver = FindTransceiverBySender(sender); |
| 1516 | if (!transceiver || !sender->track()) { |
| 1517 | return RTCError::OK(); |
| 1518 | } |
| 1519 | sender->SetTrack(nullptr); |
| 1520 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1521 | transceiver->internal()->set_direction( |
| 1522 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1523 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1524 | transceiver->internal()->set_direction( |
| 1525 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1526 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1527 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1528 | bool removed; |
| 1529 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1530 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1531 | } else { |
| 1532 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1533 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1534 | } |
| 1535 | if (!removed) { |
| 1536 | LOG_AND_RETURN_ERROR( |
| 1537 | RTCErrorType::INVALID_PARAMETER, |
| 1538 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1539 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1540 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1541 | UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1542 | return RTCError::OK(); |
| 1543 | } |
| 1544 | |
| 1545 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1546 | PeerConnection::FindTransceiverBySender( |
| 1547 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1548 | for (auto transceiver : transceivers_) { |
| 1549 | if (transceiver->sender() == sender) { |
| 1550 | return transceiver; |
| 1551 | } |
| 1552 | } |
| 1553 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1554 | } |
| 1555 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1556 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1557 | PeerConnection::AddTransceiver( |
| 1558 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1559 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1560 | } |
| 1561 | |
| 1562 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1563 | PeerConnection::AddTransceiver( |
| 1564 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1565 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1566 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1567 | RTC_CHECK(IsUnifiedPlan()) |
| 1568 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1569 | if (!track) { |
| 1570 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1571 | } |
| 1572 | cricket::MediaType media_type; |
| 1573 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1574 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1575 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1576 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1577 | } else { |
| 1578 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1579 | "Track kind is not audio or video"); |
| 1580 | } |
| 1581 | return AddTransceiver(media_type, track, init); |
| 1582 | } |
| 1583 | |
| 1584 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1585 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1586 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1587 | } |
| 1588 | |
| 1589 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1590 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1591 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 1592 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1593 | RTC_CHECK(IsUnifiedPlan()) |
| 1594 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1595 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1596 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1597 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1598 | "media type is not audio or video"); |
| 1599 | } |
| 1600 | return AddTransceiver(media_type, nullptr, init); |
| 1601 | } |
| 1602 | |
| 1603 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1604 | PeerConnection::AddTransceiver( |
| 1605 | cricket::MediaType media_type, |
| 1606 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1607 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1608 | bool update_negotiation_needed) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1609 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1610 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1611 | if (track) { |
| 1612 | RTC_DCHECK_EQ(media_type, |
| 1613 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1614 | ? cricket::MEDIA_TYPE_AUDIO |
| 1615 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1616 | } |
| 1617 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 1618 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1619 | init.send_encodings.size(), 0, 7, 8); |
| 1620 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1621 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1622 | [](const RtpEncodingParameters& encoding) { |
| 1623 | return !encoding.rid.empty(); |
| 1624 | }); |
| 1625 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1626 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1627 | RTCErrorType::INVALID_PARAMETER, |
| 1628 | "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] | 1629 | } |
| 1630 | |
Amit Hilbuch | f477040 | 2019-04-08 14:11:57 -0700 | [diff] [blame] | 1631 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1632 | [](const RtpEncodingParameters& encoding) { |
| 1633 | return !IsLegalRsidName(encoding.rid); |
| 1634 | })) { |
| 1635 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1636 | "Invalid RID value provided."); |
| 1637 | } |
| 1638 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1639 | if (absl::c_any_of(init.send_encodings, |
| 1640 | [](const RtpEncodingParameters& encoding) { |
| 1641 | return encoding.ssrc.has_value(); |
| 1642 | })) { |
| 1643 | LOG_AND_RETURN_ERROR( |
| 1644 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1645 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | RtpParameters parameters; |
| 1649 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1650 | |
| 1651 | // Encodings are dropped from the tail if too many are provided. |
| 1652 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1653 | parameters.encodings.erase( |
| 1654 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1655 | parameters.encodings.end()); |
| 1656 | } |
| 1657 | |
| 1658 | // Single RID should be removed. |
| 1659 | if (parameters.encodings.size() == 1 && |
| 1660 | !parameters.encodings[0].rid.empty()) { |
| 1661 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1662 | parameters.encodings[0].rid.clear(); |
| 1663 | } |
| 1664 | |
| 1665 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1666 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1667 | rtc::UniqueStringGenerator rid_generator; |
| 1668 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1669 | encoding.rid = rid_generator(); |
| 1670 | } |
| 1671 | } |
| 1672 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1673 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1674 | LOG_AND_RETURN_ERROR( |
| 1675 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1676 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1677 | } |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1678 | |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 1679 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1680 | if (!result.ok()) { |
| 1681 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1682 | } |
| 1683 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1684 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1685 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1686 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1687 | // that sender ID is already in use. |
| 1688 | std::string sender_id = |
| 1689 | (track && !FindSenderById(track->id()) ? track->id() |
| 1690 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1691 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1692 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1693 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1694 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1695 | transceiver->internal()->set_direction(init.direction); |
| 1696 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1697 | if (update_negotiation_needed) { |
| 1698 | UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1699 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1700 | |
| 1701 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1702 | } |
| 1703 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1704 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1705 | PeerConnection::CreateSender( |
| 1706 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1707 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1708 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1709 | const std::vector<std::string>& stream_ids, |
| 1710 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1711 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1712 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1713 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1714 | RTC_DCHECK(!track || |
| 1715 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1716 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1717 | signaling_thread(), |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1718 | AudioRtpSender::Create(worker_thread(), id, stats_.get(), this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1719 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1720 | } else { |
| 1721 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1722 | RTC_DCHECK(!track || |
| 1723 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1724 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1725 | signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1726 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1727 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1728 | bool set_track_succeeded = sender->SetTrack(track); |
| 1729 | RTC_DCHECK(set_track_succeeded); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1730 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1731 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1732 | return sender; |
| 1733 | } |
| 1734 | |
| 1735 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1736 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1737 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1738 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1739 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1740 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1741 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1742 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1743 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1744 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1745 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1746 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1747 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1748 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1749 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1750 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1751 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1752 | return receiver; |
| 1753 | } |
| 1754 | |
| 1755 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1756 | PeerConnection::CreateAndAddTransceiver( |
| 1757 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1758 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1759 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1760 | // Ensure that the new sender does not have an ID that is already in use by |
| 1761 | // another sender. |
| 1762 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1763 | // could be invalid, but should not cause a crash). |
| 1764 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1765 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 1766 | signaling_thread(), |
| 1767 | new RtpTransceiver(sender, receiver, channel_manager())); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1768 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1769 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1770 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1771 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1772 | } |
| 1773 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1774 | void PeerConnection::OnNegotiationNeeded() { |
| 1775 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1776 | RTC_DCHECK(!IsClosed()); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 1777 | UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1778 | } |
| 1779 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1780 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1781 | const std::string& kind, |
| 1782 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1783 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1784 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1785 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1786 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1787 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1788 | if (IsClosed()) { |
| 1789 | return nullptr; |
| 1790 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1791 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1792 | // Internally we need to have one stream with Plan B semantics, so we |
| 1793 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1794 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1795 | if (stream_id.empty()) { |
| 1796 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1797 | RTC_LOG(LS_INFO) |
| 1798 | << "No stream_id specified for sender. Generated stream ID: " |
| 1799 | << stream_ids[0]; |
| 1800 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1801 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1804 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1805 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1806 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1807 | auto audio_sender = AudioRtpSender::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1808 | worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1809 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1810 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1811 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1812 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1813 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 1814 | auto video_sender = |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 1815 | VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1816 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1817 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1818 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1819 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1820 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1821 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1822 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1823 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 1824 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1825 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1826 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1827 | } |
| 1828 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1829 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1830 | const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 1831 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1832 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1833 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1834 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1835 | } |
| 1836 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1839 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1840 | PeerConnection::GetSendersInternal() const { |
| 1841 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1842 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1843 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1844 | auto senders = transceiver->internal()->senders(); |
| 1845 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1846 | } |
| 1847 | return all_senders; |
| 1848 | } |
| 1849 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1850 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1851 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 1852 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1853 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1854 | for (const auto& receiver : GetReceiversInternal()) { |
| 1855 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1856 | } |
| 1857 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1860 | std::vector< |
| 1861 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1862 | PeerConnection::GetReceiversInternal() const { |
| 1863 | std::vector< |
| 1864 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1865 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1866 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1867 | auto receivers = transceiver->internal()->receivers(); |
| 1868 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1869 | receivers.end()); |
| 1870 | } |
| 1871 | return all_receivers; |
| 1872 | } |
| 1873 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1874 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1875 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 1876 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1877 | RTC_CHECK(IsUnifiedPlan()) |
| 1878 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1879 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1880 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1881 | all_transceivers.push_back(transceiver); |
| 1882 | } |
| 1883 | return all_transceivers; |
| 1884 | } |
| 1885 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1886 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1887 | MediaStreamTrackInterface* track, |
| 1888 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1889 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1890 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1891 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1892 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1893 | return false; |
| 1894 | } |
| 1895 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1896 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1897 | // The StatsCollector is used to tell if a track is valid because it may |
| 1898 | // remember tracks that the PeerConnection previously removed. |
| 1899 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1900 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1901 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1902 | return false; |
| 1903 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1904 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1905 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1906 | return true; |
| 1907 | } |
| 1908 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1909 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1910 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1911 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1912 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1913 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1914 | stats_collector_->GetStatsReport(callback); |
| 1915 | } |
| 1916 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1917 | void PeerConnection::GetStats( |
| 1918 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1919 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1920 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1921 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1922 | RTC_DCHECK(callback); |
| 1923 | RTC_DCHECK(stats_collector_); |
| 1924 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1925 | if (selector) { |
| 1926 | for (const auto& proxy_transceiver : transceivers_) { |
| 1927 | for (const auto& proxy_sender : |
| 1928 | proxy_transceiver->internal()->senders()) { |
| 1929 | if (proxy_sender == selector) { |
| 1930 | internal_sender = proxy_sender->internal(); |
| 1931 | break; |
| 1932 | } |
| 1933 | } |
| 1934 | if (internal_sender) |
| 1935 | break; |
| 1936 | } |
| 1937 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1938 | // 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] | 1939 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1940 | // PeerConnection). This means that "all the stats objects representing the |
| 1941 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1942 | // produces an empty stats report. |
| 1943 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1944 | } |
| 1945 | |
| 1946 | void PeerConnection::GetStats( |
| 1947 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1948 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1949 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 1950 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1951 | RTC_DCHECK(callback); |
| 1952 | RTC_DCHECK(stats_collector_); |
| 1953 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1954 | if (selector) { |
| 1955 | for (const auto& proxy_transceiver : transceivers_) { |
| 1956 | for (const auto& proxy_receiver : |
| 1957 | proxy_transceiver->internal()->receivers()) { |
| 1958 | if (proxy_receiver == selector) { |
| 1959 | internal_receiver = proxy_receiver->internal(); |
| 1960 | break; |
| 1961 | } |
| 1962 | } |
| 1963 | if (internal_receiver) |
| 1964 | break; |
| 1965 | } |
| 1966 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1967 | // 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] | 1968 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1969 | // the PeerConnection). This means that "all the stats objects representing |
| 1970 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1971 | // selector produces an empty stats report. |
| 1972 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1973 | } |
| 1974 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1975 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1976 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1977 | return signaling_state_; |
| 1978 | } |
| 1979 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1980 | PeerConnectionInterface::IceConnectionState |
| 1981 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1982 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1983 | return ice_connection_state_; |
| 1984 | } |
| 1985 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1986 | PeerConnectionInterface::IceConnectionState |
| 1987 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1988 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1989 | return standardized_ice_connection_state_; |
| 1990 | } |
| 1991 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1992 | PeerConnectionInterface::PeerConnectionState |
| 1993 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 1994 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1995 | return connection_state_; |
| 1996 | } |
| 1997 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1998 | PeerConnectionInterface::IceGatheringState |
| 1999 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2000 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2001 | return ice_gathering_state_; |
| 2002 | } |
| 2003 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2004 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2005 | const std::string& label, |
| 2006 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 10:17:47 +0100 | [diff] [blame] | 2007 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2008 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2009 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2010 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2011 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 2012 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 2013 | if (config) { |
| 2014 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 2015 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2016 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2017 | InternalCreateDataChannel(label, internal_config.get())); |
| 2018 | if (!channel.get()) { |
| 2019 | return nullptr; |
| 2020 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2021 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2022 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 2023 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2024 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2025 | UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 2026 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2027 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2028 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 2029 | } |
| 2030 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 2031 | void PeerConnection::RestartIce() { |
| 2032 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2033 | local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions( |
| 2034 | current_local_description_.get(), pending_local_description_.get()); |
| 2035 | UpdateNegotiationNeeded(); |
| 2036 | } |
| 2037 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2038 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 2039 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2040 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2041 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2042 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2043 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2044 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 2045 | return; |
| 2046 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2047 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2048 | if (IsClosed()) { |
| 2049 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2050 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2051 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2052 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2053 | return; |
| 2054 | } |
| 2055 | |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 2056 | // If a session error has occurred the PeerConnection is in a possibly |
| 2057 | // inconsistent state so fail right away. |
| 2058 | if (session_error() != SessionError::kNone) { |
| 2059 | std::string error_message = GetSessionErrorMsg(); |
| 2060 | RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message; |
| 2061 | PostCreateSessionDescriptionFailure( |
| 2062 | observer, |
| 2063 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2064 | return; |
| 2065 | } |
| 2066 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2067 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2068 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2069 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2070 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2071 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2072 | return; |
| 2073 | } |
| 2074 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2075 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 2076 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 2077 | if (IsUnifiedPlan()) { |
| 2078 | RTCError error = HandleLegacyOfferOptions(options); |
| 2079 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2080 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2081 | return; |
| 2082 | } |
| 2083 | } |
| 2084 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2085 | cricket::MediaSessionOptions session_options; |
| 2086 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2087 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2090 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 2091 | const RTCOfferAnswerOptions& options) { |
| 2092 | RTC_DCHECK(IsUnifiedPlan()); |
| 2093 | |
| 2094 | if (options.offer_to_receive_audio == 0) { |
| 2095 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2096 | cricket::MEDIA_TYPE_AUDIO); |
| 2097 | } else if (options.offer_to_receive_audio == 1) { |
| 2098 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 2099 | } else if (options.offer_to_receive_audio > 1) { |
| 2100 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2101 | "offer_to_receive_audio > 1 is not supported."); |
| 2102 | } |
| 2103 | |
| 2104 | if (options.offer_to_receive_video == 0) { |
| 2105 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2106 | cricket::MEDIA_TYPE_VIDEO); |
| 2107 | } else if (options.offer_to_receive_video == 1) { |
| 2108 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 2109 | } else if (options.offer_to_receive_video > 1) { |
| 2110 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2111 | "offer_to_receive_video > 1 is not supported."); |
| 2112 | } |
| 2113 | |
| 2114 | return RTCError::OK(); |
| 2115 | } |
| 2116 | |
| 2117 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2118 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2119 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2120 | RtpTransceiverDirection new_direction = |
| 2121 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 2122 | if (new_direction != transceiver->direction()) { |
| 2123 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 2124 | << " transceiver (MID=" |
| 2125 | << transceiver->mid().value_or("<not set>") << ") from " |
| 2126 | << RtpTransceiverDirectionToString( |
| 2127 | transceiver->direction()) |
| 2128 | << " to " |
| 2129 | << RtpTransceiverDirectionToString(new_direction) |
| 2130 | << " since CreateOffer specified offer_to_receive=0"; |
| 2131 | transceiver->internal()->set_direction(new_direction); |
| 2132 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 2137 | cricket::MediaType media_type) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 2138 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2139 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2140 | RTC_LOG(LS_INFO) |
| 2141 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 2142 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2143 | RtpTransceiverInit init; |
| 2144 | init.direction = RtpTransceiverDirection::kRecvOnly; |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2145 | AddTransceiver(media_type, nullptr, init, |
| 2146 | /*update_negotiation_needed=*/false); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2151 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 2152 | std::vector< |
| 2153 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2154 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2155 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2156 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2157 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 2158 | receiving_transceivers.push_back(transceiver); |
| 2159 | } |
| 2160 | } |
| 2161 | return receiving_transceivers; |
| 2162 | } |
| 2163 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2164 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 2165 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 8d2e228 | 2019-02-17 13:00:07 +0100 | [diff] [blame] | 2166 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2167 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2168 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2169 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2170 | return; |
| 2171 | } |
| 2172 | |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 2173 | // If a session error has occurred the PeerConnection is in a possibly |
| 2174 | // inconsistent state so fail right away. |
| 2175 | if (session_error() != SessionError::kNone) { |
| 2176 | std::string error_message = GetSessionErrorMsg(); |
| 2177 | RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message; |
| 2178 | PostCreateSessionDescriptionFailure( |
| 2179 | observer, |
| 2180 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2181 | return; |
| 2182 | } |
| 2183 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2184 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 2185 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 2186 | std::string error = |
| 2187 | "PeerConnection cannot create an answer in a state other than " |
| 2188 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2189 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2190 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 2191 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2192 | return; |
| 2193 | } |
| 2194 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 2195 | // The remote description should be set if we're in the right state. |
| 2196 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2197 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 2198 | if (IsUnifiedPlan()) { |
| 2199 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2200 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2201 | "supported with Unified Plan semantics. Use the " |
| 2202 | "RtpTransceiver API instead."; |
| 2203 | } |
| 2204 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2205 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2206 | "supported with Unified Plan semantics. Use the " |
| 2207 | "RtpTransceiver API instead."; |
| 2208 | } |
| 2209 | } |
| 2210 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2211 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2212 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2213 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2214 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | void PeerConnection::SetLocalDescription( |
| 2218 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2219 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2220 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2221 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2222 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2223 | // The SetLocalDescription contract is that we take ownership of the session |
| 2224 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 2225 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 2226 | // description as a unique_ptr argument to formalize this agreement. |
| 2227 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 2228 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2229 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2230 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2231 | return; |
| 2232 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2233 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2234 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2235 | PostSetSessionDescriptionFailure( |
| 2236 | observer, |
| 2237 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2238 | return; |
| 2239 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2240 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2241 | // If a session error has occurred the PeerConnection is in a possibly |
| 2242 | // inconsistent state so fail right away. |
| 2243 | if (session_error() != SessionError::kNone) { |
| 2244 | std::string error_message = GetSessionErrorMsg(); |
| 2245 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2246 | PostSetSessionDescriptionFailure( |
| 2247 | observer, |
| 2248 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2249 | return; |
| 2250 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2251 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2252 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2253 | if (!error.ok()) { |
| 2254 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2255 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2256 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2257 | PostSetSessionDescriptionFailure( |
| 2258 | observer, |
| 2259 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2260 | return; |
| 2261 | } |
| 2262 | |
| 2263 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2264 | // which may destroy it before returning. |
| 2265 | const SdpType type = desc->GetType(); |
| 2266 | |
| 2267 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2268 | // |desc| may be destroyed at this point. |
| 2269 | |
| 2270 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2271 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2272 | // inconsistent state, so act conservatively here and set the session error |
| 2273 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2274 | SetSessionError(SessionError::kContent, error.message()); |
| 2275 | std::string error_message = |
| 2276 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2277 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2278 | PostSetSessionDescriptionFailure( |
| 2279 | observer, |
| 2280 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2281 | return; |
| 2282 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2283 | RTC_DCHECK(local_description()); |
| 2284 | |
| 2285 | PostSetSessionDescriptionSuccess(observer); |
| 2286 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 2287 | // MaybeStartGathering needs to be called after posting |
| 2288 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2289 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2290 | transport_controller_->MaybeStartGathering(); |
| 2291 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2292 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2293 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2294 | // MaybeStartGathering... |
| 2295 | network_thread()->Invoke<void>( |
| 2296 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2297 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2298 | // Make UMA notes about what was agreed to. |
| 2299 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2300 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2301 | |
| 2302 | if (IsUnifiedPlan()) { |
| 2303 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2304 | UpdateNegotiationNeeded(); |
| 2305 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2306 | is_negotiation_needed_) { |
| 2307 | Observer()->OnRenegotiationNeeded(); |
| 2308 | } |
| 2309 | } |
| 2310 | |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 2311 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | RTCError PeerConnection::ApplyLocalDescription( |
| 2315 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2316 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2317 | RTC_DCHECK(desc); |
| 2318 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2319 | // Update stats here so that we have the most recent stats for tracks and |
| 2320 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2321 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2322 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2323 | // Take a reference to the old local description since it's used below to |
| 2324 | // compare against the new local description. When setting the new local |
| 2325 | // description, grab ownership of the replaced session description in case it |
| 2326 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2327 | // of the method. |
| 2328 | const SessionDescriptionInterface* old_local_description = |
| 2329 | local_description(); |
| 2330 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2331 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2332 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2333 | replaced_local_description = pending_local_description_ |
| 2334 | ? std::move(pending_local_description_) |
| 2335 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2336 | current_local_description_ = std::move(desc); |
| 2337 | pending_local_description_ = nullptr; |
| 2338 | current_remote_description_ = std::move(pending_remote_description_); |
| 2339 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2340 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2341 | pending_local_description_ = std::move(desc); |
| 2342 | } |
| 2343 | // The session description to apply now must be accessed by |
| 2344 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2345 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2346 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 2347 | // Report statistics about any use of simulcast. |
| 2348 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 2349 | *local_description()->description()); |
| 2350 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2351 | if (!is_caller_) { |
| 2352 | if (remote_description()) { |
| 2353 | // Remote description was applied first, so this PC is the callee. |
| 2354 | is_caller_ = false; |
| 2355 | } else { |
| 2356 | // Local description is applied first, so this PC is the caller. |
| 2357 | is_caller_ = true; |
| 2358 | } |
| 2359 | } |
| 2360 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2361 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2362 | if (!error.ok()) { |
| 2363 | return error; |
| 2364 | } |
| 2365 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2366 | if (IsUnifiedPlan()) { |
| 2367 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2368 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2369 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2370 | if (!error.ok()) { |
| 2371 | return error; |
| 2372 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2373 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2374 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2375 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2376 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2377 | // Note that code paths that don't set MID won't be able to use |
| 2378 | // information about DTLS transports. |
| 2379 | if (transceiver->mid()) { |
| 2380 | auto dtls_transport = |
| 2381 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2382 | transceiver->internal()->sender_internal()->set_transport( |
| 2383 | dtls_transport); |
| 2384 | transceiver->internal()->receiver_internal()->set_transport( |
| 2385 | dtls_transport); |
| 2386 | } |
| 2387 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2388 | const ContentInfo* content = |
| 2389 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2390 | if (!content) { |
| 2391 | continue; |
| 2392 | } |
| 2393 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2394 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2395 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2396 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2397 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2398 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2399 | // "recvonly", process the removal of a remote track for the media |
| 2400 | // description, given transceiver, removeList, and muteTracks. |
| 2401 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2402 | (transceiver->internal()->fired_direction() && |
| 2403 | RtpTransceiverDirectionHasRecv( |
| 2404 | *transceiver->internal()->fired_direction()))) { |
| 2405 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2406 | &removed_streams); |
| 2407 | } |
| 2408 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2409 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2410 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2411 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2412 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2413 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2414 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2415 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2416 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2417 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2418 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2419 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2420 | } |
| 2421 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2422 | // Media channels will be created only when offer is set. These may use new |
| 2423 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2424 | if (type == SdpType::kOffer) { |
| 2425 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2426 | // description is applied. Restore back to old description. |
| 2427 | RTCError error = CreateChannels(*local_description()->description()); |
| 2428 | if (!error.ok()) { |
| 2429 | return error; |
| 2430 | } |
| 2431 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2432 | // Remove unused channels if MediaContentDescription is rejected. |
| 2433 | RemoveUnusedChannels(local_description()->description()); |
| 2434 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2435 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2436 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2437 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2438 | if (!error.ok()) { |
| 2439 | return error; |
| 2440 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2441 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2442 | if (remote_description()) { |
| 2443 | // Now that we have a local description, we can push down remote candidates. |
| 2444 | UseCandidatesInSessionDescription(remote_description()); |
| 2445 | } |
| 2446 | |
| 2447 | pending_ice_restarts_.clear(); |
| 2448 | if (session_error() != SessionError::kNone) { |
| 2449 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2450 | } |
| 2451 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2452 | // If setting the description decided our SSL role, allocate any necessary |
| 2453 | // SCTP sids. |
| 2454 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2455 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2456 | AllocateSctpSids(role); |
| 2457 | } |
| 2458 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2459 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2460 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2461 | const ContentInfo* content = |
| 2462 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2463 | if (!content) { |
| 2464 | continue; |
| 2465 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2466 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2467 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2468 | // 0 is a special value meaning "this sender has no associated send |
| 2469 | // stream". Need to call this so the sender won't attempt to configure |
| 2470 | // a no longer existing stream and run into DCHECKs in the lower |
| 2471 | // layers. |
| 2472 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2473 | } else { |
| 2474 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2475 | const std::vector<StreamParams>& streams = channel->local_streams(); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 2476 | transceiver->internal()->sender_internal()->set_stream_ids( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2477 | streams[0].stream_ids()); |
| 2478 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2479 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | } else { |
| 2483 | // Plan B semantics. |
| 2484 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2485 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2486 | // local session description. |
| 2487 | const cricket::ContentInfo* audio_content = |
| 2488 | GetFirstAudioContent(local_description()->description()); |
| 2489 | if (audio_content) { |
| 2490 | if (audio_content->rejected) { |
| 2491 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2492 | } else { |
| 2493 | const cricket::AudioContentDescription* audio_desc = |
| 2494 | audio_content->media_description()->as_audio(); |
| 2495 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2496 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2497 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2498 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2499 | const cricket::ContentInfo* video_content = |
| 2500 | GetFirstVideoContent(local_description()->description()); |
| 2501 | if (video_content) { |
| 2502 | if (video_content->rejected) { |
| 2503 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2504 | } else { |
| 2505 | const cricket::VideoContentDescription* video_desc = |
| 2506 | video_content->media_description()->as_video(); |
| 2507 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2508 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2509 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2513 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2514 | if (data_content) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 2515 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 2516 | data_content->media_description()->as_rtp_data(); |
| 2517 | // rtp_data_desc will be null if this is an SCTP description. |
| 2518 | if (rtp_data_desc) { |
| 2519 | UpdateLocalRtpDataChannels(rtp_data_desc->streams()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 2523 | if (type == SdpType::kAnswer && |
| 2524 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 2525 | *current_local_description_)) { |
| 2526 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 2527 | } |
| 2528 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2529 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2532 | // The SDP parser used to populate these values by default for the 'content |
| 2533 | // name' if an a=mid line was absent. |
| 2534 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2535 | switch (media_type) { |
| 2536 | case cricket::MEDIA_TYPE_AUDIO: |
| 2537 | return cricket::CN_AUDIO; |
| 2538 | case cricket::MEDIA_TYPE_VIDEO: |
| 2539 | return cricket::CN_VIDEO; |
| 2540 | case cricket::MEDIA_TYPE_DATA: |
| 2541 | return cricket::CN_DATA; |
| 2542 | } |
| 2543 | RTC_NOTREACHED(); |
| 2544 | return ""; |
| 2545 | } |
| 2546 | |
| 2547 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2548 | cricket::SessionDescription* new_remote_description) { |
| 2549 | RTC_DCHECK(new_remote_description); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2550 | const cricket::ContentInfos no_infos; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2551 | const cricket::ContentInfos& local_contents = |
| 2552 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2553 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2554 | const cricket::ContentInfos& remote_contents = |
| 2555 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 2556 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2557 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2558 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2559 | if (!content.name.empty()) { |
| 2560 | continue; |
| 2561 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2562 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2563 | absl::string_view source_explanation; |
| 2564 | if (IsUnifiedPlan()) { |
| 2565 | if (i < local_contents.size()) { |
| 2566 | new_mid = local_contents[i].name; |
| 2567 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2568 | } else if (i < remote_contents.size()) { |
| 2569 | new_mid = remote_contents[i].name; |
| 2570 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2571 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2572 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2573 | source_explanation = "generated just now"; |
| 2574 | } |
| 2575 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2576 | new_mid = std::string( |
| 2577 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2578 | source_explanation = "to match pre-existing behavior"; |
| 2579 | } |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2580 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2581 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2582 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2583 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2584 | << " is missing an a=mid line. Filling in the value '" |
| 2585 | << new_mid << "' " << source_explanation << "."; |
| 2586 | } |
| 2587 | } |
| 2588 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2589 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2590 | SetSessionDescriptionObserver* observer, |
| 2591 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2592 | SetRemoteDescription( |
| 2593 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2594 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2595 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2596 | } |
| 2597 | |
| 2598 | void PeerConnection::SetRemoteDescription( |
| 2599 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2600 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 2601 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2602 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2603 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2604 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2605 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2608 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2609 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2610 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2611 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2612 | return; |
| 2613 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2614 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2615 | // If a session error has occurred the PeerConnection is in a possibly |
| 2616 | // inconsistent state so fail right away. |
| 2617 | if (session_error() != SessionError::kNone) { |
| 2618 | std::string error_message = GetSessionErrorMsg(); |
| 2619 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2620 | observer->OnSetRemoteDescriptionComplete( |
| 2621 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2622 | return; |
| 2623 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2624 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2625 | if (desc->GetType() == SdpType::kOffer) { |
| 2626 | // Report to UMA the format of the received offer. |
| 2627 | ReportSdpFormatReceived(*desc); |
| 2628 | } |
| 2629 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2630 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 2631 | // points. |
| 2632 | FillInMissingRemoteMids(desc->description()); |
| 2633 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2634 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2635 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2636 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2637 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2638 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2639 | observer->OnSetRemoteDescriptionComplete( |
| 2640 | RTCError(error.type(), std::move(error_message))); |
| 2641 | return; |
| 2642 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2643 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2644 | // Grab the description type before moving ownership to |
| 2645 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2646 | const SdpType type = desc->GetType(); |
| 2647 | |
| 2648 | error = ApplyRemoteDescription(std::move(desc)); |
| 2649 | // |desc| may be destroyed at this point. |
| 2650 | |
| 2651 | if (!error.ok()) { |
| 2652 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2653 | // inconsistent state, so act conservatively here and set the session error |
| 2654 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2655 | SetSessionError(SessionError::kContent, error.message()); |
| 2656 | std::string error_message = |
| 2657 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2658 | RTC_LOG(LS_ERROR) << error_message; |
| 2659 | observer->OnSetRemoteDescriptionComplete( |
| 2660 | RTCError(error.type(), std::move(error_message))); |
| 2661 | return; |
| 2662 | } |
| 2663 | RTC_DCHECK(remote_description()); |
| 2664 | |
| 2665 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2666 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2667 | // MaybeStartGathering... |
| 2668 | network_thread()->Invoke<void>( |
| 2669 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2670 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2671 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2672 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2673 | } |
| 2674 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 2675 | if (IsUnifiedPlan()) { |
| 2676 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2677 | UpdateNegotiationNeeded(); |
| 2678 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2679 | is_negotiation_needed_) { |
| 2680 | Observer()->OnRenegotiationNeeded(); |
| 2681 | } |
| 2682 | } |
| 2683 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2684 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 2685 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2689 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2690 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2691 | RTC_DCHECK(desc); |
| 2692 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2693 | // Update stats here so that we have the most recent stats for tracks and |
| 2694 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2695 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2696 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2697 | // Take a reference to the old remote description since it's used below to |
| 2698 | // compare against the new remote description. When setting the new remote |
| 2699 | // description, grab ownership of the replaced session description in case it |
| 2700 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2701 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2702 | const SessionDescriptionInterface* old_remote_description = |
| 2703 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2704 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2705 | SdpType type = desc->GetType(); |
| 2706 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2707 | replaced_remote_description = pending_remote_description_ |
| 2708 | ? std::move(pending_remote_description_) |
| 2709 | : std::move(current_remote_description_); |
| 2710 | current_remote_description_ = std::move(desc); |
| 2711 | pending_remote_description_ = nullptr; |
| 2712 | current_local_description_ = std::move(pending_local_description_); |
| 2713 | } else { |
| 2714 | replaced_remote_description = std::move(pending_remote_description_); |
| 2715 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2716 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2717 | // The session description to apply now must be accessed by |
| 2718 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2719 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2720 | |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 2721 | // Report statistics about any use of simulcast. |
| 2722 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 2723 | *remote_description()->description()); |
| 2724 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2725 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2726 | if (!error.ok()) { |
| 2727 | return error; |
| 2728 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2729 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2730 | if (IsUnifiedPlan()) { |
| 2731 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2732 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2733 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2734 | if (!error.ok()) { |
| 2735 | return error; |
| 2736 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2737 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2738 | // Media channels will be created only when offer is set. These may use new |
| 2739 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2740 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2741 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2742 | // description is applied. Restore back to old description. |
| 2743 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2744 | if (!error.ok()) { |
| 2745 | return error; |
| 2746 | } |
| 2747 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2748 | // Remove unused channels if MediaContentDescription is rejected. |
| 2749 | RemoveUnusedChannels(remote_description()->description()); |
| 2750 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2751 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2752 | // NOTE: Candidates allocation will be initiated only when |
| 2753 | // SetLocalDescription is called. |
| 2754 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2755 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2756 | if (!error.ok()) { |
| 2757 | return error; |
| 2758 | } |
| 2759 | |
| 2760 | if (local_description() && |
| 2761 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2762 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2763 | } |
| 2764 | |
| 2765 | if (old_remote_description) { |
| 2766 | for (const cricket::ContentInfo& content : |
| 2767 | old_remote_description->description()->contents()) { |
| 2768 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2769 | // password that indicates the remote peer requests an ICE restart. |
| 2770 | // TODO(deadbeef): When we start storing both the current and pending |
| 2771 | // remote description, this should reset pending_ice_restarts and compare |
| 2772 | // against the current description. |
| 2773 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2774 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2775 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2776 | pending_ice_restarts_.insert(content.name); |
| 2777 | } |
| 2778 | } else { |
| 2779 | // We retain all received candidates only if ICE is not restarted. |
| 2780 | // When ICE is restarted, all previous candidates belong to an old |
| 2781 | // generation and should not be kept. |
| 2782 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2783 | // description should only contain candidates from the last set remote |
| 2784 | // description plus any candidates added since then. We should remove |
| 2785 | // this once we're sure it won't break anything. |
| 2786 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2787 | old_remote_description, content.name, mutable_remote_description()); |
| 2788 | } |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | if (session_error() != SessionError::kNone) { |
| 2793 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2794 | } |
| 2795 | |
| 2796 | // Set the the ICE connection state to connecting since the connection may |
| 2797 | // become writable with peer reflexive candidates before any remote candidate |
| 2798 | // is signaled. |
| 2799 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2800 | // is to have a new signal the indicates a change in checking state from the |
| 2801 | // transport and expose a new checking() member from transport that can be |
| 2802 | // read to determine the current checking state. The existing SignalConnecting |
| 2803 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2804 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2805 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2806 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2807 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2808 | } |
| 2809 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2810 | // If setting the description decided our SSL role, allocate any necessary |
| 2811 | // SCTP sids. |
| 2812 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2813 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2814 | AllocateSctpSids(role); |
| 2815 | } |
| 2816 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2817 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2818 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2819 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2820 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2821 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2822 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2823 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2824 | const ContentInfo* content = |
| 2825 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2826 | if (!content) { |
| 2827 | continue; |
| 2828 | } |
| 2829 | const MediaContentDescription* media_desc = content->media_description(); |
| 2830 | RtpTransceiverDirection local_direction = |
| 2831 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2832 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 2833 | // RTCSessionDescription: Set the associated remote streams given |
| 2834 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 2835 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 2836 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2837 | std::vector<std::string> stream_ids; |
| 2838 | if (!media_desc->streams().empty()) { |
| 2839 | // The remote description has signaled the stream IDs. |
| 2840 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2841 | } |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2842 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 2843 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 2844 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 2845 | stream_ids, &added_streams, |
| 2846 | &removed_streams); |
| 2847 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 2848 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 2849 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 2850 | // process the addition of a remote track for the media description. |
| 2851 | if (!transceiver->fired_direction() || |
| 2852 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 2853 | RTC_LOG(LS_INFO) |
| 2854 | << "Processing the addition of a remote track for MID=" |
| 2855 | << content->name << "."; |
| 2856 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 11:25:05 +0100 | [diff] [blame] | 2857 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2858 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2859 | // 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] | 2860 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2861 | // removal of a remote track for the media description, given transceiver, |
| 2862 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2863 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2864 | (transceiver->fired_direction() && |
| 2865 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2866 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2867 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2868 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2869 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2870 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2871 | // 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] | 2872 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2873 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2874 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2875 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2876 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2877 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 2878 | if (transceiver->mid()) { |
| 2879 | auto dtls_transport = |
| 2880 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2881 | transceiver->internal()->sender_internal()->set_transport( |
| 2882 | dtls_transport); |
| 2883 | transceiver->internal()->receiver_internal()->set_transport( |
| 2884 | dtls_transport); |
| 2885 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2886 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2887 | // 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] | 2888 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2889 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2890 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2891 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2892 | transceiver->Stop(); |
| 2893 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2894 | if (!content->rejected && |
| 2895 | RtpTransceiverDirectionHasRecv(local_direction)) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2896 | if (!media_desc->streams().empty() && |
| 2897 | media_desc->streams()[0].has_ssrcs()) { |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 2898 | uint32_t ssrc = media_desc->streams()[0].first_ssrc(); |
| 2899 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
| 2900 | } else { |
| 2901 | transceiver->internal() |
| 2902 | ->receiver_internal() |
| 2903 | ->SetupUnsignaledMediaChannel(); |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2904 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2905 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2906 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2907 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2908 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2909 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2910 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2911 | observer->OnTrack(transceiver); |
| 2912 | observer->OnAddTrack(transceiver->receiver(), |
| 2913 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2914 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2915 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2916 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2917 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2918 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2919 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2920 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2921 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2922 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2923 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2924 | } |
| 2925 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2926 | const cricket::ContentInfo* audio_content = |
| 2927 | GetFirstAudioContent(remote_description()->description()); |
| 2928 | const cricket::ContentInfo* video_content = |
| 2929 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2930 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2931 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2932 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2933 | GetFirstVideoContentDescription(remote_description()->description()); |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 2934 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 2935 | GetFirstRtpDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2936 | |
| 2937 | // Check if the descriptions include streams, just in case the peer supports |
| 2938 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2939 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2940 | (audio_desc && !audio_desc->streams().empty()) || |
| 2941 | (video_desc && !video_desc->streams().empty())) { |
| 2942 | remote_peer_supports_msid_ = true; |
| 2943 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2944 | |
| 2945 | // We wait to signal new streams until we finish processing the description, |
| 2946 | // since only at that point will new streams have all their tracks. |
| 2947 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2948 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2949 | if (!IsUnifiedPlan()) { |
| 2950 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2951 | // rejected media section, there is some cleanup logic that expects the |
| 2952 | // voice/ video channel to still be set. But in this method the voice/video |
| 2953 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2954 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2955 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2956 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2957 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2958 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2959 | // and MediaStreams. |
| 2960 | if (audio_content) { |
| 2961 | if (audio_content->rejected) { |
| 2962 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2963 | } else { |
| 2964 | bool default_audio_track_needed = |
| 2965 | !remote_peer_supports_msid_ && |
| 2966 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2967 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2968 | default_audio_track_needed, audio_desc->type(), |
| 2969 | new_streams); |
| 2970 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2971 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2972 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2973 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2974 | // and MediaStreams. |
| 2975 | if (video_content) { |
| 2976 | if (video_content->rejected) { |
| 2977 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2978 | } else { |
| 2979 | bool default_video_track_needed = |
| 2980 | !remote_peer_supports_msid_ && |
| 2981 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2982 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2983 | default_video_track_needed, video_desc->type(), |
| 2984 | new_streams); |
| 2985 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2986 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2987 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 2988 | // If this is an RTP data transport, update the DataChannels with the |
| 2989 | // information from the remote peer. |
| 2990 | if (rtp_data_desc) { |
| 2991 | UpdateRemoteRtpDataChannels(GetActiveStreams(rtp_data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2992 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2993 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2994 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2995 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2996 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2997 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2998 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2999 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3000 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 3001 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3002 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3003 | UpdateEndedRemoteMediaStreams(); |
| 3004 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3005 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 3006 | if (type == SdpType::kAnswer && |
| 3007 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 3008 | *current_local_description_)) { |
| 3009 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 3010 | } |
| 3011 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 3012 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 3013 | } |
| 3014 | |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3015 | void PeerConnection::SetAssociatedRemoteStreams( |
| 3016 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 3017 | const std::vector<std::string>& stream_ids, |
| 3018 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 3019 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3020 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 3021 | for (const std::string& stream_id : stream_ids) { |
| 3022 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3023 | remote_streams_->find(stream_id); |
| 3024 | if (!stream) { |
| 3025 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3026 | MediaStream::Create(stream_id)); |
| 3027 | remote_streams_->AddStream(stream); |
| 3028 | added_streams->push_back(stream); |
| 3029 | } |
| 3030 | media_streams.push_back(stream); |
| 3031 | } |
| 3032 | // Special case: "a=msid" missing, use random stream ID. |
| 3033 | if (media_streams.empty() && |
| 3034 | !(remote_description()->description()->msid_signaling() & |
| 3035 | cricket::kMsidSignalingMediaSection)) { |
| 3036 | if (!missing_msid_default_stream_) { |
| 3037 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 3038 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 3039 | added_streams->push_back(missing_msid_default_stream_); |
| 3040 | } |
| 3041 | media_streams.push_back(missing_msid_default_stream_); |
| 3042 | } |
| 3043 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 3044 | receiver->streams(); |
| 3045 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 3046 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 3047 | // update the stream-track relationships in a later step. We do this earlier, |
| 3048 | // changing the order of things, but the end-result is the same. |
| 3049 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 3050 | // instead. https://crbug.com/webrtc/9480 |
| 3051 | receiver->SetStreams(media_streams); |
| 3052 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3053 | } |
| 3054 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3055 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 3056 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3057 | transceiver, |
| 3058 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 3059 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3060 | RTC_DCHECK(transceiver->mid()); |
| 3061 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 3062 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3063 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3064 | transceiver->internal()->receiver_internal()->streams(); |
| 3065 | // This will remove the remote track from the streams. |
| 3066 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 3067 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3068 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3069 | } |
| 3070 | |
| 3071 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 3072 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 3073 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3074 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 3075 | // streams, see if the stream was removed by checking if this was the last |
| 3076 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3077 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 3078 | if (remote_stream->GetAudioTracks().empty() && |
| 3079 | remote_stream->GetVideoTracks().empty()) { |
| 3080 | remote_streams_->RemoveStream(remote_stream); |
| 3081 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | } |
| 3085 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3086 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 3087 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3088 | const SessionDescriptionInterface& new_session, |
| 3089 | const SessionDescriptionInterface* old_local_description, |
| 3090 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3091 | RTC_DCHECK(IsUnifiedPlan()); |
| 3092 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 3093 | const cricket::ContentGroup* bundle_group = nullptr; |
| 3094 | if (new_session.GetType() == SdpType::kOffer) { |
| 3095 | auto bundle_group_or_error = |
| 3096 | GetEarlyBundleGroup(*new_session.description()); |
| 3097 | if (!bundle_group_or_error.ok()) { |
| 3098 | return bundle_group_or_error.MoveError(); |
| 3099 | } |
| 3100 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3101 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3102 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3103 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3104 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 3105 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3106 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 3107 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3108 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3109 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3110 | const cricket::ContentInfo* old_local_content = nullptr; |
| 3111 | if (old_local_description && |
| 3112 | i < old_local_description->description()->contents().size()) { |
| 3113 | old_local_content = |
| 3114 | &old_local_description->description()->contents()[i]; |
| 3115 | } |
| 3116 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 3117 | if (old_remote_description && |
| 3118 | i < old_remote_description->description()->contents().size()) { |
| 3119 | old_remote_content = |
| 3120 | &old_remote_description->description()->contents()[i]; |
| 3121 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3122 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3123 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 3124 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3125 | if (!transceiver_or_error.ok()) { |
| 3126 | return transceiver_or_error.MoveError(); |
| 3127 | } |
| 3128 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3129 | RTCError error = |
| 3130 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 3131 | if (!error.ok()) { |
| 3132 | return error; |
| 3133 | } |
| 3134 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3135 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 3136 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3137 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 3138 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3139 | continue; |
| 3140 | } |
| 3141 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 3142 | if (!error.ok()) { |
| 3143 | return error; |
| 3144 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3145 | } else { |
| 3146 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3147 | "Unknown section type."); |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | return RTCError::OK(); |
| 3152 | } |
| 3153 | |
| 3154 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 3155 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3156 | transceiver, |
| 3157 | const cricket::ContentInfo& content, |
| 3158 | const cricket::ContentGroup* bundle_group) { |
| 3159 | RTC_DCHECK(IsUnifiedPlan()); |
| 3160 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3161 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3162 | if (content.rejected) { |
| 3163 | if (channel) { |
| 3164 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3165 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3166 | } |
| 3167 | } else { |
| 3168 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3169 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3170 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3171 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3172 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3173 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3174 | } |
| 3175 | if (!channel) { |
| 3176 | LOG_AND_RETURN_ERROR( |
| 3177 | RTCErrorType::INTERNAL_ERROR, |
| 3178 | "Failed to create channel for mid=" + content.name); |
| 3179 | } |
| 3180 | transceiver->internal()->SetChannel(channel); |
| 3181 | } |
| 3182 | } |
| 3183 | return RTCError::OK(); |
| 3184 | } |
| 3185 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3186 | RTCError PeerConnection::UpdateDataChannel( |
| 3187 | cricket::ContentSource source, |
| 3188 | const cricket::ContentInfo& content, |
| 3189 | const cricket::ContentGroup* bundle_group) { |
| 3190 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3191 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3192 | // will take care of rejecting it. |
| 3193 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3194 | } |
| 3195 | if (content.rejected) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3196 | RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3197 | DestroyDataChannel(); |
| 3198 | } else { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 3199 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3200 | RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3201 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3202 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3203 | "Failed to create data channel."); |
| 3204 | } |
| 3205 | } |
| 3206 | if (source == cricket::CS_REMOTE) { |
| 3207 | const MediaContentDescription* data_desc = content.media_description(); |
| 3208 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 3209 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 3210 | } |
| 3211 | } |
| 3212 | } |
| 3213 | return RTCError::OK(); |
| 3214 | } |
| 3215 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3216 | // This method will extract any send encodings that were sent by the remote |
| 3217 | // connection. This is currently only relevant for Simulcast scenario (where |
| 3218 | // the number of layers may be communicated by the server). |
| 3219 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 3220 | const MediaContentDescription& desc) { |
| 3221 | if (!desc.HasSimulcast()) { |
| 3222 | return {}; |
| 3223 | } |
| 3224 | std::vector<RtpEncodingParameters> result; |
| 3225 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 3226 | |
| 3227 | // This is a remote description, the parameters we are after should appear |
| 3228 | // as receive streams. |
| 3229 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 3230 | RTC_DCHECK(!alternatives.empty()); |
| 3231 | // There is currently no way to specify or choose from alternatives. |
| 3232 | // We will always use the first alternative, which is the most preferred. |
| 3233 | const SimulcastLayer& layer = alternatives[0]; |
| 3234 | RtpEncodingParameters parameters; |
| 3235 | parameters.rid = layer.rid; |
| 3236 | parameters.active = !layer.is_paused; |
| 3237 | result.push_back(parameters); |
| 3238 | } |
| 3239 | |
| 3240 | return result; |
| 3241 | } |
| 3242 | |
| 3243 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 3244 | const std::vector<SimulcastLayer>& layers, |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3245 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3246 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3247 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3248 | std::vector<std::string> disabled_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3249 | |
| 3250 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 3251 | // So we will iterate over the send encodings rather than the layers. |
| 3252 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3253 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 3254 | [&encoding](const SimulcastLayer& layer) { |
| 3255 | return layer.rid == encoding.rid; |
| 3256 | }); |
| 3257 | // 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] | 3258 | if (iter == layers.end()) { |
| 3259 | disabled_layers.push_back(encoding.rid); |
| 3260 | continue; |
| 3261 | } |
| 3262 | |
| 3263 | encoding.active = !iter->is_paused; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3264 | } |
| 3265 | |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3266 | RTCError result = sender->SetParametersInternal(parameters); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3267 | if (result.ok()) { |
| 3268 | result = sender->DisableEncodingLayers(disabled_layers); |
| 3269 | } |
| 3270 | |
| 3271 | return result; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3272 | } |
| 3273 | |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3274 | static bool SimulcastIsRejected( |
| 3275 | const ContentInfo* local_content, |
| 3276 | const MediaContentDescription& answer_media_desc) { |
| 3277 | bool simulcast_offered = local_content && |
| 3278 | local_content->media_description() && |
| 3279 | local_content->media_description()->HasSimulcast(); |
| 3280 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 3281 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 3282 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 3283 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 3284 | } |
| 3285 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3286 | static RTCError DisableSimulcastInSender( |
| 3287 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3288 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 15:55:19 -0800 | [diff] [blame] | 3289 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3290 | if (parameters.encodings.size() <= 1) { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3291 | return RTCError::OK(); |
| 3292 | } |
| 3293 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3294 | std::vector<std::string> disabled_layers; |
| 3295 | std::transform( |
| 3296 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 3297 | std::back_inserter(disabled_layers), |
| 3298 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 3299 | return sender->DisableEncodingLayers(disabled_layers); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3300 | } |
| 3301 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3302 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3303 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3304 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3305 | size_t mline_index, |
| 3306 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3307 | const ContentInfo* old_local_content, |
| 3308 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3309 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3310 | // If this is an offer then the m= section might be recycled. If the m= |
| 3311 | // section is being recycled (defined as: rejected in the current local or |
| 3312 | // remote description and not rejected in new description), dissociate the |
| 3313 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3314 | // and discard the mapping between the transceiver and its m= section index. |
| 3315 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3316 | old_remote_content)) { |
| 3317 | // We want to dissociate the transceiver that has the rejected mid. |
| 3318 | const std::string& old_mid = |
| 3319 | (old_local_content && old_local_content->rejected) |
| 3320 | ? old_local_content->name |
| 3321 | : old_remote_content->name; |
| 3322 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3323 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3324 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3325 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3326 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3327 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | const MediaContentDescription* media_desc = content.media_description(); |
| 3331 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3332 | if (source == cricket::CS_LOCAL) { |
| 3333 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3334 | // mapping between transceivers and m= section indices established when |
| 3335 | // creating the offer. |
| 3336 | if (!transceiver) { |
| 3337 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3338 | } |
| 3339 | if (!transceiver) { |
| 3340 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3341 | "Unknown transceiver"); |
| 3342 | } |
| 3343 | } else { |
| 3344 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3345 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3346 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3347 | // When simulcast is requested, a transceiver cannot be associated because |
| 3348 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3349 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 3350 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3351 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3352 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3353 | } |
| 3354 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3355 | // recvonly direction. |
| 3356 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3357 | RTC_LOG(LS_INFO) << "Adding " |
| 3358 | << cricket::MediaTypeToString(media_desc->type()) |
| 3359 | << " transceiver for MID=" << content.name |
| 3360 | << " at i=" << mline_index |
| 3361 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3362 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3363 | std::vector<RtpEncodingParameters> send_encodings = |
| 3364 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3365 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3366 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3367 | std::string receiver_id; |
| 3368 | if (!media_desc->streams().empty()) { |
| 3369 | receiver_id = media_desc->streams()[0].id; |
| 3370 | } else { |
| 3371 | receiver_id = rtc::CreateRandomUuid(); |
| 3372 | } |
| 3373 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3374 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3375 | transceiver->internal()->set_direction( |
| 3376 | RtpTransceiverDirection::kRecvOnly); |
| 3377 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3378 | |
| 3379 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3380 | // This can happen when simulcast is not supported on the remote party. |
Amit Hilbuch | aabd036 | 2019-03-01 13:14:46 -0800 | [diff] [blame] | 3381 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
Amit Hilbuch | e2a284d | 2019-03-05 12:36:31 -0800 | [diff] [blame] | 3382 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3383 | RTCError error = |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3384 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3385 | if (!error.ok()) { |
| 3386 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3387 | return std::move(error); |
| 3388 | } |
| 3389 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3390 | } |
| 3391 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3392 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3393 | LOG_AND_RETURN_ERROR( |
| 3394 | RTCErrorType::INVALID_PARAMETER, |
| 3395 | "Transceiver type does not match media description type."); |
| 3396 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3397 | if (media_desc->HasSimulcast()) { |
| 3398 | std::vector<SimulcastLayer> layers = |
| 3399 | source == cricket::CS_LOCAL |
| 3400 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3401 | : media_desc->simulcast_description() |
| 3402 | .receive_layers() |
| 3403 | .GetAllLayers(); |
| 3404 | RTCError error = UpdateSimulcastLayerStatusInSender( |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 3405 | layers, transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3406 | if (!error.ok()) { |
| 3407 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3408 | return std::move(error); |
| 3409 | } |
| 3410 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3411 | // Associate the found or created RtpTransceiver with the m= section by |
| 3412 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3413 | // section, and establish a mapping between the transceiver and the index of |
| 3414 | // the m= section. |
| 3415 | transceiver->internal()->set_mid(content.name); |
| 3416 | transceiver->internal()->set_mline_index(mline_index); |
| 3417 | return std::move(transceiver); |
| 3418 | } |
| 3419 | |
| 3420 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3421 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3422 | RTC_DCHECK(IsUnifiedPlan()); |
| 3423 | for (auto transceiver : transceivers_) { |
| 3424 | if (transceiver->mid() == mid) { |
| 3425 | return transceiver; |
| 3426 | } |
| 3427 | } |
| 3428 | return nullptr; |
| 3429 | } |
| 3430 | |
| 3431 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3432 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3433 | RTC_DCHECK(IsUnifiedPlan()); |
| 3434 | for (auto transceiver : transceivers_) { |
| 3435 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3436 | return transceiver; |
| 3437 | } |
| 3438 | } |
| 3439 | return nullptr; |
| 3440 | } |
| 3441 | |
| 3442 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3443 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3444 | cricket::MediaType media_type) const { |
| 3445 | RTC_DCHECK(IsUnifiedPlan()); |
| 3446 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3447 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3448 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3449 | // associated with any m= section and are not stopped, find the first such |
| 3450 | // RtpTransceiver. |
| 3451 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3452 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3453 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3454 | !transceiver->stopped()) { |
| 3455 | return transceiver; |
| 3456 | } |
| 3457 | } |
| 3458 | return nullptr; |
| 3459 | } |
| 3460 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3461 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3462 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3463 | transceiver, |
| 3464 | const SessionDescriptionInterface* sdesc) const { |
| 3465 | RTC_DCHECK(transceiver); |
| 3466 | RTC_DCHECK(sdesc); |
| 3467 | if (IsUnifiedPlan()) { |
| 3468 | if (!transceiver->internal()->mid()) { |
| 3469 | // This transceiver is not associated with a media section yet. |
| 3470 | return nullptr; |
| 3471 | } |
| 3472 | return sdesc->description()->GetContentByName( |
| 3473 | *transceiver->internal()->mid()); |
| 3474 | } else { |
| 3475 | // Plan B only allows at most one audio and one video section, so use the |
| 3476 | // first media section of that type. |
| 3477 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3478 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3479 | } |
| 3480 | } |
| 3481 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3482 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3483 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3484 | return configuration_; |
| 3485 | } |
| 3486 | |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3487 | RTCError PeerConnection::SetConfiguration( |
| 3488 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3489 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3490 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3491 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3492 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3493 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 3494 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3497 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3498 | // size is not allowed, and changing the set of ICE servers will not result |
| 3499 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3500 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3501 | configuration_.ice_candidate_pool_size) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3502 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3503 | "Can't change candidate pool size after calling " |
| 3504 | "SetLocalDescription."); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3505 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3506 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3507 | if (local_description() && |
| 3508 | configuration.use_media_transport != configuration_.use_media_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3509 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3510 | "Can't change media_transport after calling " |
| 3511 | "SetLocalDescription."); |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | if (remote_description() && |
| 3515 | configuration.use_media_transport != configuration_.use_media_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3516 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3517 | "Can't change media_transport after calling " |
| 3518 | "SetRemoteDescription."); |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3521 | if (local_description() && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3522 | configuration.use_media_transport_for_data_channels != |
| 3523 | configuration_.use_media_transport_for_data_channels) { |
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_for_data_channels " |
| 3526 | "after calling SetLocalDescription."); |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | if (remote_description() && |
| 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 SetRemoteDescription."); |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3538 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3539 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3540 | "Can't change crypto_options after calling " |
| 3541 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3542 | } |
| 3543 | |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3544 | if (local_description() && configuration.use_datagram_transport != |
| 3545 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3546 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3547 | "Can't change use_datagram_transport " |
| 3548 | "after calling SetLocalDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | if (remote_description() && configuration.use_datagram_transport != |
| 3552 | configuration_.use_datagram_transport) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3553 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3554 | "Can't change use_datagram_transport " |
| 3555 | "after calling SetRemoteDescription."); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3556 | } |
| 3557 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3558 | if (local_description() && |
| 3559 | configuration.use_datagram_transport_for_data_channels != |
| 3560 | configuration_.use_datagram_transport_for_data_channels) { |
| 3561 | LOG_AND_RETURN_ERROR( |
| 3562 | RTCErrorType::INVALID_MODIFICATION, |
| 3563 | "Can't change use_datagram_transport_for_data_channels " |
| 3564 | "after calling SetLocalDescription."); |
| 3565 | } |
| 3566 | |
| 3567 | if (remote_description() && |
| 3568 | configuration.use_datagram_transport_for_data_channels != |
| 3569 | configuration_.use_datagram_transport_for_data_channels) { |
| 3570 | LOG_AND_RETURN_ERROR( |
| 3571 | RTCErrorType::INVALID_MODIFICATION, |
| 3572 | "Can't change use_datagram_transport_for_data_channels " |
| 3573 | "after calling SetRemoteDescription."); |
| 3574 | } |
| 3575 | |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3576 | if (configuration.use_media_transport_for_data_channels || |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3577 | configuration.use_media_transport || |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 3578 | (configuration.use_datagram_transport && |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3579 | *configuration.use_datagram_transport) || |
| 3580 | (configuration.use_datagram_transport_for_data_channels && |
| 3581 | *configuration.use_datagram_transport_for_data_channels)) { |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3582 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 3583 | << "Media transport requires MaxBundle policy."; |
| 3584 | } |
| 3585 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3586 | // The simplest (and most future-compatible) way to tell if the config was |
| 3587 | // modified in an invalid way is to copy each property we do support |
| 3588 | // modifying, then use operator==. There are far more properties we don't |
| 3589 | // support modifying than those we do, and more could be added. |
| 3590 | RTCConfiguration modified_config = configuration_; |
| 3591 | modified_config.servers = configuration.servers; |
| 3592 | modified_config.type = configuration.type; |
| 3593 | modified_config.ice_candidate_pool_size = |
| 3594 | configuration.ice_candidate_pool_size; |
| 3595 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
Qingsi Wang | bca1485 | 2019-06-26 14:56:02 -0700 | [diff] [blame] | 3596 | modified_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 3597 | configuration.surface_ice_candidates_on_ice_transport_type_changed; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3598 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 3599 | modified_config.ice_check_interval_strong_connectivity = |
| 3600 | configuration.ice_check_interval_strong_connectivity; |
| 3601 | modified_config.ice_check_interval_weak_connectivity = |
| 3602 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 3603 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 3604 | modified_config.ice_unwritable_min_checks = |
| 3605 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 3606 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3607 | modified_config.stun_candidate_keepalive_interval = |
| 3608 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3609 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 3610 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3611 | modified_config.active_reset_srtp_params = |
| 3612 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3613 | modified_config.use_media_transport = configuration.use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3614 | modified_config.use_media_transport_for_data_channels = |
| 3615 | configuration.use_media_transport_for_data_channels; |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 3616 | modified_config.use_datagram_transport = configuration.use_datagram_transport; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3617 | modified_config.use_datagram_transport_for_data_channels = |
| 3618 | configuration.use_datagram_transport_for_data_channels; |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 3619 | modified_config.turn_logging_id = configuration.turn_logging_id; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3620 | if (configuration != modified_config) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3621 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3622 | "Modifying the configuration in an unsupported way."); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3623 | } |
| 3624 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 3625 | // Validate the modified configuration. |
| 3626 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 3627 | if (!validate_error.ok()) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3628 | return validate_error; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 3629 | } |
| 3630 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3631 | // Note that this isn't possible through chromium, since it's an unsigned |
| 3632 | // short in WebIDL. |
| 3633 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 3634 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3635 | return RTCError(RTCErrorType::INVALID_RANGE); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | // Parse ICE servers before hopping to network thread. |
| 3639 | cricket::ServerAddresses stun_servers; |
| 3640 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 3641 | RTCErrorType parse_error = |
| 3642 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 3643 | if (parse_error != RTCErrorType::NONE) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3644 | return RTCError(parse_error); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3645 | } |
Jonas Oreland | 3c02842 | 2019-08-22 16:16:35 +0200 | [diff] [blame] | 3646 | // Add the turn logging id to all turn servers |
| 3647 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 3648 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 3649 | } |
| 3650 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3651 | // Note if STUN or TURN servers were supplied. |
| 3652 | if (!stun_servers.empty()) { |
| 3653 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 3654 | } |
| 3655 | if (!turn_servers.empty()) { |
| 3656 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 3657 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3658 | |
| 3659 | // In theory this shouldn't fail. |
| 3660 | if (!network_thread()->Invoke<bool>( |
| 3661 | RTC_FROM_HERE, |
| 3662 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 3663 | stun_servers, turn_servers, modified_config.type, |
| 3664 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3665 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3666 | modified_config.turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3667 | modified_config.stun_candidate_keepalive_interval, |
| 3668 | static_cast<bool>(local_description())))) { |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3669 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3670 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3671 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3672 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3673 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 3674 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 3675 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3676 | if (modified_config.servers != configuration_.servers || |
| 3677 | modified_config.type != configuration_.type || |
| 3678 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3679 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3680 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3681 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 3682 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 3683 | |
| 3684 | use_datagram_transport_ = datagram_transport_config_.enabled && |
| 3685 | modified_config.use_datagram_transport.value_or( |
| 3686 | datagram_transport_config_.default_value); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3687 | use_datagram_transport_for_data_channels_ = |
| 3688 | datagram_transport_data_channel_config_.enabled && |
| 3689 | modified_config.use_datagram_transport_for_data_channels.value_or( |
| 3690 | datagram_transport_data_channel_config_.default_value); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 3691 | transport_controller_->SetMediaTransportSettings( |
| 3692 | modified_config.use_media_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 3693 | modified_config.use_media_transport_for_data_channels, |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 3694 | use_datagram_transport_, use_datagram_transport_for_data_channels_); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3695 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3696 | if (configuration_.active_reset_srtp_params != |
| 3697 | modified_config.active_reset_srtp_params) { |
| 3698 | transport_controller_->SetActiveResetSrtpParams( |
| 3699 | modified_config.active_reset_srtp_params); |
| 3700 | } |
| 3701 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3702 | configuration_ = modified_config; |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 3703 | use_media_transport_ = configuration.use_media_transport; |
Niels Möller | 2579f0c | 2019-08-19 09:58:17 +0200 | [diff] [blame] | 3704 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3707 | bool PeerConnection::AddIceCandidate( |
| 3708 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 3709 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3710 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3711 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3712 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3713 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3714 | return false; |
| 3715 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3716 | |
| 3717 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3718 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3719 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3720 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3721 | return false; |
| 3722 | } |
| 3723 | |
| 3724 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3725 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3726 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3727 | return false; |
| 3728 | } |
| 3729 | |
| 3730 | bool valid = false; |
| 3731 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 3732 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3733 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3734 | return false; |
| 3735 | } |
| 3736 | |
| 3737 | // Add this candidate to the remote session description. |
| 3738 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3739 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3740 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3741 | return false; |
| 3742 | } |
| 3743 | |
| 3744 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3745 | bool result = UseCandidate(ice_candidate); |
| 3746 | if (result) { |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 3747 | NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED); |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3748 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 3749 | } else { |
| 3750 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 3751 | } |
| 3752 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3753 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3754 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3755 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3756 | return true; |
| 3757 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3760 | bool PeerConnection::RemoveIceCandidates( |
| 3761 | const std::vector<cricket::Candidate>& candidates) { |
| 3762 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3763 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3764 | if (IsClosed()) { |
| 3765 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 3766 | return false; |
| 3767 | } |
| 3768 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3769 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3770 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 3771 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3772 | return false; |
| 3773 | } |
| 3774 | |
| 3775 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3776 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3777 | return false; |
| 3778 | } |
| 3779 | |
| 3780 | size_t number_removed = |
| 3781 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3782 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3783 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3784 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3785 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3786 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3790 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3791 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3792 | RTC_LOG(LS_ERROR) |
| 3793 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3794 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3795 | } |
| 3796 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3799 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3800 | if (!worker_thread()->IsCurrent()) { |
| 3801 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3802 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3803 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 3804 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3805 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3806 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3807 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3808 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3809 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3810 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3811 | "min_bitrate_bps <= 0"); |
| 3812 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3813 | if (has_start) { |
| 3814 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3815 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3816 | "start_bitrate_bps < min_bitrate_bps"); |
| 3817 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3818 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3819 | "curent_bitrate_bps < 0"); |
| 3820 | } |
| 3821 | } |
| 3822 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3823 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3824 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3825 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3826 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3827 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3828 | "max_bitrate_bps < min_bitrate_bps"); |
| 3829 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3830 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3831 | "max_bitrate_bps < 0"); |
| 3832 | } |
| 3833 | } |
| 3834 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3835 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 10:31:54 -0700 | [diff] [blame] | 3836 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3837 | |
| 3838 | return RTCError::OK(); |
| 3839 | } |
| 3840 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3841 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3842 | if (!worker_thread()->IsCurrent()) { |
| 3843 | worker_thread()->Invoke<void>( |
| 3844 | RTC_FROM_HERE, |
| 3845 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3846 | return; |
| 3847 | } |
| 3848 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3849 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3850 | audio_state->SetPlayout(playout); |
| 3851 | } |
| 3852 | |
| 3853 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3854 | if (!worker_thread()->IsCurrent()) { |
| 3855 | worker_thread()->Invoke<void>( |
| 3856 | RTC_FROM_HERE, |
| 3857 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3858 | return; |
| 3859 | } |
| 3860 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3861 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3862 | audio_state->SetRecording(recording); |
| 3863 | } |
| 3864 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3865 | std::unique_ptr<rtc::SSLCertificate> |
| 3866 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3867 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3868 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3869 | return nullptr; |
| 3870 | } |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 3871 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3872 | } |
| 3873 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3874 | std::unique_ptr<rtc::SSLCertChain> |
| 3875 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 3876 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3877 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3878 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3879 | return nullptr; |
| 3880 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3881 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3882 | audio_transceiver->internal()->channel()->transport_name()); |
| 3883 | } |
| 3884 | |
| 3885 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3886 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3887 | for (auto transceiver : transceivers_) { |
| 3888 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3889 | return transceiver; |
| 3890 | } |
| 3891 | } |
| 3892 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3893 | } |
| 3894 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3895 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3896 | int64_t output_period_ms) { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3897 | return worker_thread()->Invoke<bool>( |
Danil Chapovalov | 116ffe7 | 2019-09-05 10:21:11 +0200 | [diff] [blame] | 3898 | RTC_FROM_HERE, |
| 3899 | [this, output = std::move(output), output_period_ms]() mutable { |
| 3900 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3901 | }); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3902 | } |
| 3903 | |
Niels Möller | f00ca1a | 2019-05-10 11:33:12 +0200 | [diff] [blame] | 3904 | bool PeerConnection::StartRtcEventLog( |
| 3905 | std::unique_ptr<RtcEventLogOutput> output) { |
Niels Möller | 695cf6a | 2019-05-13 12:27:23 +0200 | [diff] [blame] | 3906 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 3907 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 3908 | output_period_ms = 5000; |
| 3909 | } |
| 3910 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 11:33:12 +0200 | [diff] [blame] | 3911 | } |
| 3912 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3913 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3914 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3915 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3916 | } |
| 3917 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 3918 | rtc::scoped_refptr<DtlsTransportInterface> |
| 3919 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3920 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 3921 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3922 | } |
| 3923 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3924 | rtc::scoped_refptr<DtlsTransport> |
| 3925 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3926 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3927 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3928 | } |
| 3929 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 3930 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 3931 | const { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 3932 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 3933 | if (!sctp_mid_) { |
| 3934 | return nullptr; |
| 3935 | } |
| 3936 | return transport_controller_->GetSctpTransport(*sctp_mid_); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 3937 | } |
| 3938 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3939 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3940 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3941 | return pending_local_description_ ? pending_local_description_.get() |
| 3942 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3946 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3947 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3948 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3951 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3952 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3953 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3954 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3955 | } |
| 3956 | |
| 3957 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3958 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3959 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3960 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3964 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3965 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3966 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3967 | } |
| 3968 | |
| 3969 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3970 | const { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 3971 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3972 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3973 | } |
| 3974 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3975 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 3976 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3977 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3978 | // Update stats here so that we have the most recent stats for tracks and |
| 3979 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3980 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3981 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3982 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3983 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3984 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3985 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3986 | transceiver->Stop(); |
| 3987 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 3988 | |
| 3989 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3990 | // the transport controller below. |
| 3991 | if (stats_collector_) { |
| 3992 | stats_collector_->WaitForPendingRequest(); |
| 3993 | } |
| 3994 | |
| 3995 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3996 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3997 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3998 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3999 | // The event log is used in the transport controller, which must be outlived |
| 4000 | // by the former. CreateOffer by the peer connection is implemented |
| 4001 | // asynchronously and if the peer connection is closed without resetting the |
| 4002 | // WebRTC session description factory, the session description factory would |
| 4003 | // call the transport controller. |
| 4004 | webrtc_session_desc_factory_.reset(); |
| 4005 | transport_controller_.reset(); |
| 4006 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 4007 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4008 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 4009 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4010 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 4011 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 4012 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 4013 | call_.reset(); |
| 4014 | // The event log must outlive call (and any other object that uses it). |
| 4015 | event_log_.reset(); |
| 4016 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4017 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4018 | // The .h file says that observer can be discarded after close() returns. |
| 4019 | // Make sure this is true. |
| 4020 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4023 | void PeerConnection::OnMessage(rtc::Message* msg) { |
Karl Wiberg | 744310f | 2019-02-14 10:18:56 +0100 | [diff] [blame] | 4024 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4025 | switch (msg->message_id) { |
| 4026 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 4027 | SetSessionDescriptionMsg* param = |
| 4028 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4029 | param->observer->OnSuccess(); |
| 4030 | delete param; |
| 4031 | break; |
| 4032 | } |
| 4033 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 4034 | SetSessionDescriptionMsg* param = |
| 4035 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4036 | param->observer->OnFailure(std::move(param->error)); |
| 4037 | delete param; |
| 4038 | break; |
| 4039 | } |
| 4040 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 4041 | CreateSessionDescriptionMsg* param = |
| 4042 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 4043 | param->observer->OnFailure(std::move(param->error)); |
| 4044 | delete param; |
| 4045 | break; |
| 4046 | } |
| 4047 | case MSG_GETSTATS: { |
| 4048 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 4049 | StatsReports reports; |
| 4050 | stats_->GetStats(param->track, &reports); |
| 4051 | param->observer->OnComplete(reports); |
| 4052 | delete param; |
| 4053 | break; |
| 4054 | } |
| 4055 | case MSG_FREE_DATACHANNELS: { |
| 4056 | sctp_data_channels_to_free_.clear(); |
| 4057 | break; |
| 4058 | } |
| 4059 | case MSG_REPORT_USAGE_PATTERN: { |
| 4060 | ReportUsagePattern(); |
| 4061 | break; |
| 4062 | } |
| 4063 | default: |
| 4064 | RTC_NOTREACHED() << "Not implemented"; |
| 4065 | break; |
| 4066 | } |
| 4067 | } |
| 4068 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 4069 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 4070 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4071 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 4072 | GetAudioTransceiver()->internal()->channel()); |
| 4073 | if (voice_channel) { |
| 4074 | return voice_channel->media_channel(); |
| 4075 | } else { |
| 4076 | return nullptr; |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 4081 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4082 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 4083 | GetVideoTransceiver()->internal()->channel()); |
| 4084 | if (video_channel) { |
| 4085 | return video_channel->media_channel(); |
| 4086 | } else { |
| 4087 | return nullptr; |
| 4088 | } |
| 4089 | } |
| 4090 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4091 | void PeerConnection::CreateAudioReceiver( |
| 4092 | MediaStreamInterface* stream, |
| 4093 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 4094 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4095 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 4096 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4097 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4098 | auto* audio_receiver = new AudioRtpReceiver( |
| 4099 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4100 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 4101 | if (remote_sender_info.sender_id == kDefaultAudioSenderId) { |
| 4102 | audio_receiver->SetupUnsignaledMediaChannel(); |
| 4103 | } else { |
| 4104 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4105 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4106 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4107 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4108 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 4109 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4110 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4113 | void PeerConnection::CreateVideoReceiver( |
| 4114 | MediaStreamInterface* stream, |
| 4115 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 4116 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4117 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 4118 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4119 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4120 | auto* video_receiver = new VideoRtpReceiver( |
| 4121 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4122 | video_receiver->SetMediaChannel(video_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 4123 | if (remote_sender_info.sender_id == kDefaultVideoSenderId) { |
| 4124 | video_receiver->SetupUnsignaledMediaChannel(); |
| 4125 | } else { |
| 4126 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4127 | } |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 4128 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4129 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4130 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 4131 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 4132 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4135 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 4136 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4137 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4138 | const RtpSenderInfo& remote_sender_info) { |
| 4139 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 4140 | if (!receiver) { |
| 4141 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 4142 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4143 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4144 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4145 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4146 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 4147 | } else { |
| 4148 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 4149 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4150 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4153 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 4154 | MediaStreamInterface* stream) { |
| 4155 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4156 | RTC_DCHECK(track); |
| 4157 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4158 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4159 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4160 | // We already have a sender for this track, so just change the stream_id |
| 4161 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 4162 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4163 | return; |
| 4164 | } |
| 4165 | |
| 4166 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4167 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4168 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4169 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4170 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4171 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 4172 | // which will connect the sender to the underlying transport. This can |
| 4173 | // occur if a local session description that contains the ID of the sender |
| 4174 | // is set before AddStream is called. It can also occur if the local |
| 4175 | // session description is not changed and RemoveStream is called, and |
| 4176 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4177 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4178 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4179 | if (sender_info) { |
| 4180 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 4185 | // indefinitely, when we have unified plan SDP. |
| 4186 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 4187 | MediaStreamInterface* stream) { |
| 4188 | RTC_DCHECK(!IsClosed()); |
| 4189 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4190 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4191 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4192 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4193 | return; |
| 4194 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4195 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 4199 | MediaStreamInterface* stream) { |
| 4200 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4201 | RTC_DCHECK(track); |
| 4202 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4203 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4204 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4205 | // We already have a sender for this track, so just change the stream_id |
| 4206 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 4207 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4208 | return; |
| 4209 | } |
| 4210 | |
| 4211 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 4212 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 4213 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 4214 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4215 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 4216 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4217 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4218 | if (sender_info) { |
| 4219 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 4224 | MediaStreamInterface* stream) { |
| 4225 | RTC_DCHECK(!IsClosed()); |
| 4226 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4227 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4228 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4229 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4230 | return; |
| 4231 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4232 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4233 | } |
| 4234 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4235 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4236 | if (ice_connection_state_ == new_state) { |
| 4237 | return; |
| 4238 | } |
| 4239 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4240 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4241 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4242 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 4243 | return; |
| 4244 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4245 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4246 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 4247 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4248 | RTC_DCHECK(ice_connection_state_ != |
| 4249 | PeerConnectionInterface::kIceConnectionClosed); |
| 4250 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4251 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4252 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4255 | void PeerConnection::SetStandardizedIceConnectionState( |
| 4256 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4257 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4258 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4262 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 11:37:26 -0700 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 4266 | << standardized_ice_connection_state_ << " => " << new_state; |
| 4267 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4268 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 11:25:14 +0100 | [diff] [blame] | 4269 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4272 | void PeerConnection::SetConnectionState( |
| 4273 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4274 | if (connection_state_ == new_state) |
| 4275 | return; |
| 4276 | if (IsClosed()) |
| 4277 | return; |
| 4278 | connection_state_ = new_state; |
| 4279 | Observer()->OnConnectionChange(new_state); |
| 4280 | } |
| 4281 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4282 | void PeerConnection::OnIceGatheringChange( |
| 4283 | PeerConnectionInterface::IceGatheringState new_state) { |
| 4284 | if (IsClosed()) { |
| 4285 | return; |
| 4286 | } |
| 4287 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4288 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 4291 | void PeerConnection::OnIceCandidate( |
| 4292 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4293 | if (IsClosed()) { |
| 4294 | return; |
| 4295 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 4296 | ReportIceCandidateCollected(candidate->candidate()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4297 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 4300 | void PeerConnection::OnIceCandidateError(const std::string& host_candidate, |
| 4301 | const std::string& url, |
| 4302 | int error_code, |
| 4303 | const std::string& error_text) { |
| 4304 | if (IsClosed()) { |
| 4305 | return; |
| 4306 | } |
| 4307 | Observer()->OnIceCandidateError(host_candidate, url, error_code, error_text); |
| 4308 | } |
| 4309 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4310 | void PeerConnection::OnIceCandidatesRemoved( |
| 4311 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4312 | if (IsClosed()) { |
| 4313 | return; |
| 4314 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4315 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 4316 | } |
| 4317 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 4318 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 4319 | const cricket::CandidatePairChangeEvent& event) { |
| 4320 | if (IsClosed()) { |
| 4321 | return; |
| 4322 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 4323 | |
Qingsi Wang | cc46b10 | 2019-09-12 11:19:01 -0700 | [diff] [blame] | 4324 | if (event.selected_candidate_pair.local_candidate().type() == |
| 4325 | LOCAL_PORT_TYPE && |
| 4326 | event.selected_candidate_pair.remote_candidate().type() == |
| 4327 | LOCAL_PORT_TYPE) { |
| 4328 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 4329 | } |
| 4330 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 4331 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 4332 | } |
| 4333 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4334 | void PeerConnection::ChangeSignalingState( |
| 4335 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4336 | if (signaling_state_ == signaling_state) { |
| 4337 | return; |
| 4338 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4339 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 4340 | << GetSignalingStateString(signaling_state_) |
| 4341 | << " New state: " |
| 4342 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4343 | signaling_state_ = signaling_state; |
| 4344 | if (signaling_state == kClosed) { |
| 4345 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4346 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 4347 | standardized_ice_connection_state_ = |
| 4348 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 4349 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 4350 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4351 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 4352 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4353 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4354 | } |
| 4355 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4356 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4359 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4360 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4361 | if (IsClosed()) { |
| 4362 | return; |
| 4363 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4364 | AddAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4365 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4366 | } |
| 4367 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4368 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4369 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4370 | if (IsClosed()) { |
| 4371 | return; |
| 4372 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4373 | RemoveAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4374 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4378 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4379 | if (IsClosed()) { |
| 4380 | return; |
| 4381 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4382 | AddVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4383 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4387 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4388 | if (IsClosed()) { |
| 4389 | return; |
| 4390 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4391 | RemoveVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 4392 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4393 | } |
| 4394 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4395 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4396 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4397 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4398 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4399 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4400 | } |
| 4401 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4402 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4403 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4404 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4405 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4406 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4407 | msg->error = std::move(error); |
| 4408 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4409 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4413 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4414 | RTCError error) { |
| 4415 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4416 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4417 | msg->error = std::move(error); |
| 4418 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4419 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4422 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4423 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4424 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4425 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4426 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4427 | if (IsUnifiedPlan()) { |
| 4428 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4429 | } else { |
| 4430 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 4431 | } |
| 4432 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4433 | // Intentionally unset the data channel type for RTP data channel with the |
| 4434 | // second condition. Otherwise the RTP data channels would be successfully |
| 4435 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4436 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4437 | // people won't try to use them. |
| 4438 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4439 | session_options->data_channel_type = data_channel_type(); |
| 4440 | } |
| 4441 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4442 | // Apply ICE restart flag and renomination flag. |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 4443 | bool ice_restart = offer_answer_options.ice_restart || |
| 4444 | local_ice_credentials_to_replace_->HasIceCredentials(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4445 | for (auto& options : session_options->media_description_options) { |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 4446 | options.transport_options.ice_restart = ice_restart; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4447 | options.transport_options.enable_ice_renomination = |
| 4448 | configuration_.enable_ice_renomination; |
| 4449 | } |
| 4450 | |
| 4451 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4452 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4453 | session_options->pooled_ice_credentials = |
| 4454 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4455 | RTC_FROM_HERE, |
| 4456 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4457 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 4458 | session_options->offer_extmap_allow_mixed = |
| 4459 | configuration_.offer_extmap_allow_mixed; |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 4460 | |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4461 | if (configuration_.use_media_transport || |
| 4462 | configuration_.use_media_transport_for_data_channels) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 4463 | session_options->media_transport_settings = |
| 4464 | transport_controller_->GenerateOrGetLastMediaTransportOffer(); |
| 4465 | } |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4466 | |
| 4467 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4468 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4469 | for (auto& options : session_options->media_description_options) { |
| 4470 | options.transport_options.opaque_parameters = |
| 4471 | transport_controller_->GetTransportParameters(options.mid); |
| 4472 | } |
| 4473 | } |
| 4474 | |
Harald Alvestrand | 4aa1192 | 2019-05-14 22:00:01 +0200 | [diff] [blame] | 4475 | // Allow fallback for using obsolete SCTP syntax. |
| 4476 | // Note that the default in |session_options| is true, while |
| 4477 | // the default in |options| is false. |
| 4478 | session_options->use_obsolete_sctp_sdp = |
| 4479 | offer_answer_options.use_obsolete_sctp_sdp; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4483 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4484 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4485 | // Figure out transceiver directional preferences. |
| 4486 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4487 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4488 | |
| 4489 | // By default, generate sendrecv/recvonly m= sections. |
| 4490 | bool recv_audio = true; |
| 4491 | bool recv_video = true; |
| 4492 | |
| 4493 | // By default, only offer a new m= section if we have media to send with it. |
| 4494 | bool offer_new_audio_description = send_audio; |
| 4495 | bool offer_new_video_description = send_video; |
| 4496 | bool offer_new_data_description = HasDataChannels(); |
| 4497 | |
| 4498 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4499 | if (offer_answer_options.offer_to_receive_audio != |
| 4500 | RTCOfferAnswerOptions::kUndefined) { |
| 4501 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4502 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4503 | offer_new_audio_description || |
| 4504 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4505 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4506 | if (offer_answer_options.offer_to_receive_video != |
| 4507 | RTCOfferAnswerOptions::kUndefined) { |
| 4508 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4509 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4510 | offer_new_video_description || |
| 4511 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4512 | } |
| 4513 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4514 | absl::optional<size_t> audio_index; |
| 4515 | absl::optional<size_t> video_index; |
| 4516 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4517 | // If a current description exists, generate m= sections in the same order, |
| 4518 | // using the first audio/video/data section that appears and rejecting |
| 4519 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4520 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4521 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4522 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4523 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4524 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4525 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4526 | } |
| 4527 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4528 | // Add audio/video/data m= sections to the end if needed. |
| 4529 | if (!audio_index && offer_new_audio_description) { |
| 4530 | session_options->media_description_options.push_back( |
| 4531 | cricket::MediaDescriptionOptions( |
| 4532 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4533 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4534 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4535 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4536 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4537 | if (!video_index && offer_new_video_description) { |
| 4538 | session_options->media_description_options.push_back( |
| 4539 | cricket::MediaDescriptionOptions( |
| 4540 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4541 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4542 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4543 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4544 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4545 | if (!data_index && offer_new_data_description) { |
| 4546 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4547 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4548 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4552 | !audio_index ? nullptr |
| 4553 | : &session_options->media_description_options[*audio_index]; |
| 4554 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4555 | !video_index ? nullptr |
| 4556 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4557 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4558 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4559 | audio_media_description_options, |
| 4560 | video_media_description_options, |
| 4561 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4562 | } |
| 4563 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4564 | static cricket::MediaDescriptionOptions |
| 4565 | GetMediaDescriptionOptionsForTransceiver( |
| 4566 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4567 | transceiver, |
| 4568 | const std::string& mid) { |
| 4569 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4570 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4571 | transceiver->stopped()); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 4572 | media_description_options.codec_preferences = |
| 4573 | transceiver->codec_preferences(); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4574 | // This behavior is specified in JSEP. The gist is that: |
| 4575 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 4576 | // sendrecv. |
| 4577 | // 2. If the MSID is included, then it must be included in any subsequent |
| 4578 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4579 | if (transceiver->stopped() || |
| 4580 | (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 4581 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 4582 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4583 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4584 | |
| 4585 | cricket::SenderOptions sender_options; |
| 4586 | sender_options.track_id = transceiver->sender()->id(); |
| 4587 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 4588 | |
| 4589 | // The following sets up RIDs and Simulcast. |
| 4590 | // 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] | 4591 | RtpParameters send_parameters = |
| 4592 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4593 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 4594 | send_parameters.encodings.end(), |
| 4595 | [](const RtpEncodingParameters& encoding) { |
| 4596 | return !encoding.rid.empty(); |
| 4597 | }); |
| 4598 | |
Amit Hilbuch | b7446ed | 2019-01-28 12:25:25 -0800 | [diff] [blame] | 4599 | std::vector<RidDescription> send_rids; |
| 4600 | SimulcastLayerList send_layers; |
| 4601 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 4602 | if (encoding.rid.empty()) { |
| 4603 | continue; |
| 4604 | } |
| 4605 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 4606 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 4607 | } |
| 4608 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4609 | if (has_rids) { |
| 4610 | sender_options.rids = send_rids; |
| 4611 | } |
| 4612 | |
| 4613 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4614 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 4615 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 4616 | // simulcast, or simulcast is acheived by munging the SDP. |
| 4617 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 4618 | media_description_options.sender_options.push_back(sender_options); |
| 4619 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4620 | return media_description_options; |
| 4621 | } |
| 4622 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4623 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 4624 | static const ContentInfo* GetContentByIndex( |
| 4625 | const SessionDescriptionInterface* sdesc, |
| 4626 | size_t i) { |
| 4627 | if (!sdesc) { |
| 4628 | return nullptr; |
| 4629 | } |
| 4630 | const ContentInfos& contents = sdesc->description()->contents(); |
| 4631 | return (i < contents.size() ? &contents[i] : nullptr); |
| 4632 | } |
| 4633 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4634 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 4635 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4636 | cricket::MediaSessionOptions* session_options) { |
| 4637 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 4638 | // Offers) and 5.2.2 (Subsequent Offers). |
| 4639 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 4640 | const ContentInfos no_infos; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4641 | const ContentInfos& local_contents = |
| 4642 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 4643 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4644 | const ContentInfos& remote_contents = |
| 4645 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 4646 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4647 | // The mline indices that can be recycled. New transceivers should reuse these |
| 4648 | // slots first. |
| 4649 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4650 | // First, go through each media section that exists in either the local or |
| 4651 | // remote description and generate a media section in this offer for the |
| 4652 | // associated transceiver. If a media section can be recycled, generate a |
| 4653 | // default, rejected media section here that can be later overwritten. |
| 4654 | for (size_t i = 0; |
| 4655 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 4656 | // Either |local_content| or |remote_content| is non-null. |
| 4657 | const ContentInfo* local_content = |
| 4658 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4659 | const ContentInfo* current_local_content = |
| 4660 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4661 | const ContentInfo* remote_content = |
| 4662 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4663 | const ContentInfo* current_remote_content = |
| 4664 | GetContentByIndex(current_remote_description(), i); |
| 4665 | bool had_been_rejected = |
| 4666 | (current_local_content && current_local_content->rejected) || |
| 4667 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4668 | const std::string& mid = |
| 4669 | (local_content ? local_content->name : remote_content->name); |
| 4670 | cricket::MediaType media_type = |
| 4671 | (local_content ? local_content->media_description()->type() |
| 4672 | : remote_content->media_description()->type()); |
| 4673 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4674 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4675 | auto transceiver = GetAssociatedTransceiver(mid); |
| 4676 | RTC_CHECK(transceiver); |
| 4677 | // 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] | 4678 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 4679 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4680 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4681 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 4682 | RtpTransceiverDirection::kInactive, |
| 4683 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4684 | recycleable_mline_indices.push(i); |
| 4685 | } else { |
| 4686 | session_options->media_description_options.push_back( |
| 4687 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 4688 | // CreateOffer shouldn't really cause any state changes in |
| 4689 | // PeerConnection, but we need a way to match new transceivers to new |
| 4690 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 4691 | // by recording the index of the media section generated for the |
| 4692 | // transceiver in the offer. |
| 4693 | transceiver->internal()->set_mline_index(i); |
| 4694 | } |
| 4695 | } else { |
| 4696 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4697 | RTC_CHECK(GetDataMid()); |
| 4698 | if (had_been_rejected || mid != *GetDataMid()) { |
| 4699 | session_options->media_description_options.push_back( |
| 4700 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 4701 | } else { |
| 4702 | session_options->media_description_options.push_back( |
| 4703 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 4704 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4705 | } |
| 4706 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4707 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4708 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 4709 | // and not associated). Reuse media sections marked as recyclable first, |
| 4710 | // otherwise append to the end of the offer. New media sections should be |
| 4711 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 4712 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4713 | if (transceiver->mid() || transceiver->stopped()) { |
| 4714 | continue; |
| 4715 | } |
| 4716 | size_t mline_index; |
| 4717 | if (!recycleable_mline_indices.empty()) { |
| 4718 | mline_index = recycleable_mline_indices.front(); |
| 4719 | recycleable_mline_indices.pop(); |
| 4720 | session_options->media_description_options[mline_index] = |
| 4721 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4722 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4723 | } else { |
| 4724 | mline_index = session_options->media_description_options.size(); |
| 4725 | session_options->media_description_options.push_back( |
| 4726 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4727 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4728 | } |
| 4729 | // See comment above for why CreateOffer changes the transceiver's state. |
| 4730 | transceiver->internal()->set_mline_index(mline_index); |
| 4731 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4732 | // Lastly, add a m-section if we have local data channels and an m section |
| 4733 | // does not already exist. |
| 4734 | if (!GetDataMid() && HasDataChannels()) { |
| 4735 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4736 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4737 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | void PeerConnection::GetOptionsForAnswer( |
| 4741 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4742 | cricket::MediaSessionOptions* session_options) { |
| 4743 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 4744 | |
| 4745 | if (IsUnifiedPlan()) { |
| 4746 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 4747 | } else { |
| 4748 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 4749 | } |
| 4750 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4751 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 4752 | // the RTP data channels would be successfully negotiated by default and the |
| 4753 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 4754 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 4755 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4756 | session_options->data_channel_type = data_channel_type(); |
| 4757 | } |
| 4758 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4759 | // Apply ICE renomination flag. |
| 4760 | for (auto& options : session_options->media_description_options) { |
| 4761 | options.transport_options.enable_ice_renomination = |
| 4762 | configuration_.enable_ice_renomination; |
| 4763 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 4764 | |
| 4765 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4766 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4767 | session_options->pooled_ice_credentials = |
| 4768 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4769 | RTC_FROM_HERE, |
| 4770 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4771 | port_allocator_.get())); |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4772 | |
| 4773 | // If datagram transport is in use, add opaque transport parameters. |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4774 | if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) { |
Bjorn A Mellem | c85ebbe | 2019-06-07 10:28:06 -0700 | [diff] [blame] | 4775 | for (auto& options : session_options->media_description_options) { |
| 4776 | options.transport_options.opaque_parameters = |
| 4777 | transport_controller_->GetTransportParameters(options.mid); |
| 4778 | } |
| 4779 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4780 | } |
| 4781 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4782 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 4783 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4784 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4785 | // Figure out transceiver directional preferences. |
| 4786 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4787 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4788 | |
| 4789 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 4790 | // restricted by the direction in the offer. |
| 4791 | bool recv_audio = true; |
| 4792 | bool recv_video = true; |
| 4793 | |
| 4794 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4795 | if (offer_answer_options.offer_to_receive_audio != |
| 4796 | RTCOfferAnswerOptions::kUndefined) { |
| 4797 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 4798 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4799 | if (offer_answer_options.offer_to_receive_video != |
| 4800 | RTCOfferAnswerOptions::kUndefined) { |
| 4801 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4802 | } |
| 4803 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4804 | absl::optional<size_t> audio_index; |
| 4805 | absl::optional<size_t> video_index; |
| 4806 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 4807 | |
| 4808 | // Generate m= sections that match those in the offer. |
| 4809 | // Note that mediasession.cc will handle intersection our preferred |
| 4810 | // direction with the offered direction. |
| 4811 | GenerateMediaDescriptionOptions( |
| 4812 | remote_description(), |
| 4813 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4814 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 4815 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4816 | |
| 4817 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4818 | !audio_index ? nullptr |
| 4819 | : &session_options->media_description_options[*audio_index]; |
| 4820 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4821 | !video_index ? nullptr |
| 4822 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4823 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4824 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4825 | audio_media_description_options, |
| 4826 | video_media_description_options, |
| 4827 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4828 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4829 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4830 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 4831 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4832 | cricket::MediaSessionOptions* session_options) { |
| 4833 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 4834 | // Answers) and 5.3.2 (Subsequent Answers). |
| 4835 | RTC_DCHECK(remote_description()); |
| 4836 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 4837 | for (const ContentInfo& content : |
| 4838 | remote_description()->description()->contents()) { |
| 4839 | cricket::MediaType media_type = content.media_description()->type(); |
| 4840 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4841 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4842 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 4843 | RTC_CHECK(transceiver); |
| 4844 | session_options->media_description_options.push_back( |
| 4845 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 4846 | } else { |
| 4847 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 4848 | // Reject all data sections if data channels are disabled. |
| 4849 | // Reject a data section if it has already been rejected. |
| 4850 | // Reject all data sections except for the first one. |
| 4851 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 4852 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4853 | session_options->media_description_options.push_back( |
| 4854 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4855 | } else { |
| 4856 | session_options->media_description_options.push_back( |
| 4857 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4858 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4859 | } |
| 4860 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4861 | } |
| 4862 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4863 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 4864 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4865 | RtpTransceiverDirection audio_direction, |
| 4866 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4867 | absl::optional<size_t>* audio_index, |
| 4868 | absl::optional<size_t>* video_index, |
| 4869 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4870 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4871 | for (const cricket::ContentInfo& content : |
| 4872 | session_desc->description()->contents()) { |
| 4873 | if (IsAudioContent(&content)) { |
| 4874 | // If we already have an audio m= section, reject this extra one. |
| 4875 | if (*audio_index) { |
| 4876 | session_options->media_description_options.push_back( |
| 4877 | cricket::MediaDescriptionOptions( |
| 4878 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4879 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4880 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4881 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4882 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4883 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 4884 | content.name, audio_direction, |
| 4885 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4886 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4887 | } |
| 4888 | } else if (IsVideoContent(&content)) { |
| 4889 | // If we already have an video m= section, reject this extra one. |
| 4890 | if (*video_index) { |
| 4891 | session_options->media_description_options.push_back( |
| 4892 | cricket::MediaDescriptionOptions( |
| 4893 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4894 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4895 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4896 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4897 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4898 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 4899 | content.name, video_direction, |
| 4900 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4901 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4902 | } |
| 4903 | } else { |
| 4904 | RTC_DCHECK(IsDataContent(&content)); |
| 4905 | // If we already have an data m= section, reject this extra one. |
| 4906 | if (*data_index) { |
| 4907 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4908 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4909 | } else { |
| 4910 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4911 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4912 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4913 | } |
| 4914 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4915 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4916 | } |
| 4917 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4918 | cricket::MediaDescriptionOptions |
| 4919 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4920 | const std::string& mid) const { |
| 4921 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4922 | // expect sendrecv. |
| 4923 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4924 | RtpTransceiverDirection::kSendRecv, |
| 4925 | /*stopped=*/false); |
| 4926 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4927 | return options; |
| 4928 | } |
| 4929 | |
| 4930 | cricket::MediaDescriptionOptions |
| 4931 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4932 | const std::string& mid) const { |
| 4933 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4934 | RtpTransceiverDirection::kInactive, |
| 4935 | /*stopped=*/true); |
| 4936 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4937 | return options; |
| 4938 | } |
| 4939 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4940 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4941 | switch (data_channel_type_) { |
| 4942 | case cricket::DCT_RTP: |
| 4943 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4944 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4945 | } |
| 4946 | return rtp_data_channel_->content_name(); |
| 4947 | case cricket::DCT_SCTP: |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4948 | case cricket::DCT_MEDIA_TRANSPORT: |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 4949 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
| 4950 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
| 4951 | return sctp_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4952 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4953 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4954 | } |
| 4955 | } |
| 4956 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4957 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4958 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4959 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4960 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4961 | } |
| 4962 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4963 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4964 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4965 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4966 | cricket::MediaType media_type, |
| 4967 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4968 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4969 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4970 | std::vector<RtpSenderInfo>* current_senders = |
| 4971 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4972 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4973 | // 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] | 4974 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4975 | for (auto sender_it = current_senders->begin(); |
| 4976 | sender_it != current_senders->end(); |
| 4977 | /* incremented manually */) { |
| 4978 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4979 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4980 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4981 | std::string params_stream_id; |
| 4982 | if (params) { |
| 4983 | params_stream_id = |
| 4984 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4985 | : kDefaultStreamId); |
| 4986 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4987 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4988 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4989 | // 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] | 4990 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4991 | sender_exists) { |
| 4992 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4993 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4994 | OnRemoteSenderRemoved(info, media_type); |
| 4995 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4996 | } |
| 4997 | } |
| 4998 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4999 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5000 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 5001 | if (!params.has_ssrcs()) { |
| 5002 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 5003 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 5004 | // create a default. |
| 5005 | default_sender_needed = true; |
| 5006 | break; |
| 5007 | } |
| 5008 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5009 | // |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] | 5010 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 5011 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 5012 | // not supported in Plan B, we just take the first here and create the |
| 5013 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5014 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5015 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 5016 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5017 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5018 | uint32_t ssrc = params.first_ssrc(); |
| 5019 | |
| 5020 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5021 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5022 | if (!stream) { |
| 5023 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5024 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5025 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5026 | remote_streams_->AddStream(stream); |
| 5027 | new_streams->AddStream(stream); |
| 5028 | } |
| 5029 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5030 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5031 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5032 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5033 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5034 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5035 | } |
| 5036 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5037 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5038 | // Add default sender if necessary. |
| 5039 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5040 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5041 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5042 | if (!default_stream) { |
| 5043 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5044 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5045 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5046 | remote_streams_->AddStream(default_stream); |
| 5047 | new_streams->AddStream(default_stream); |
| 5048 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5049 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5050 | ? kDefaultAudioSenderId |
| 5051 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5052 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5053 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5054 | if (!default_sender_info) { |
| 5055 | current_senders->push_back( |
Ruslan Burakov | 7ea4605 | 2019-02-16 02:07:05 +0100 | [diff] [blame] | 5056 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5057 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 5058 | } |
| 5059 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5060 | } |
| 5061 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5062 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 5063 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 5064 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 5065 | << " receiver for track_id=" << sender_info.sender_id |
| 5066 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5067 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 5068 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5069 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5070 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5071 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5072 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5073 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 5074 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5075 | } |
| 5076 | } |
| 5077 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5078 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 5079 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 5080 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 5081 | << " receiver for track_id=" << sender_info.sender_id |
| 5082 | << " and stream_id=" << sender_info.stream_id; |
| 5083 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5084 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5085 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5086 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5087 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5088 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 5089 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5090 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5091 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5092 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5093 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5094 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5095 | } |
| 5096 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5097 | // Stopping or destroying a VideoRtpReceiver will end the |
| 5098 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5099 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5100 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5101 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5102 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 5103 | // There's no guarantee the track is still available, e.g. the track may |
| 5104 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5105 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5106 | } |
| 5107 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 5108 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5109 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5110 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5111 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 5112 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 5116 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 5117 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 5118 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 5119 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 5120 | streams_to_remove.push_back(stream); |
| 5121 | } |
| 5122 | } |
| 5123 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 5124 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5125 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5126 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5127 | } |
| 5128 | } |
| 5129 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5130 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5131 | const std::vector<cricket::StreamParams>& streams, |
| 5132 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5133 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5134 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5135 | // 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] | 5136 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5137 | for (auto sender_it = current_senders->begin(); |
| 5138 | sender_it != current_senders->end(); |
| 5139 | /* incremented manually */) { |
| 5140 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5141 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5142 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 5143 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5144 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5145 | OnLocalSenderRemoved(info, media_type); |
| 5146 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5147 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5148 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5149 | } |
| 5150 | } |
| 5151 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5152 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5153 | for (const cricket::StreamParams& params : streams) { |
| 5154 | // 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] | 5155 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5156 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5157 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5158 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5159 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5160 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5161 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5162 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5163 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5164 | } |
| 5165 | } |
| 5166 | } |
| 5167 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5168 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 5169 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 5170 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5171 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5172 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5173 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 5174 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5175 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5176 | return; |
| 5177 | } |
| 5178 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5179 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5180 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5181 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5182 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5183 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5184 | |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 +0000 | [diff] [blame] | 5185 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5186 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5187 | } |
| 5188 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5189 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 5190 | cricket::MediaType media_type) { |
| 5191 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5192 | if (!sender) { |
| 5193 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5194 | // SessionDescriptions has been renegotiated. |
| 5195 | return; |
| 5196 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5197 | |
| 5198 | // A sender has been removed from the SessionDescription but it's still |
| 5199 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 5200 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 5201 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5202 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5203 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5204 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5205 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5206 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5207 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 5211 | const cricket::StreamParamsVec& streams) { |
| 5212 | std::vector<std::string> existing_channels; |
| 5213 | |
| 5214 | // Find new and active data channels. |
| 5215 | for (const cricket::StreamParams& params : streams) { |
| 5216 | // |it->sync_label| is actually the data channel label. The reason is that |
| 5217 | // we use the same naming of data channels as we do for |
| 5218 | // MediaStreams and Tracks. |
| 5219 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 5220 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5221 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5222 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 5223 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5224 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5225 | continue; |
| 5226 | } |
| 5227 | // Set the SSRC the data channel should use for sending. |
| 5228 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 5229 | existing_channels.push_back(data_channel_it->first); |
| 5230 | } |
| 5231 | |
| 5232 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 5233 | } |
| 5234 | |
| 5235 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 5236 | const cricket::StreamParamsVec& streams) { |
| 5237 | std::vector<std::string> existing_channels; |
| 5238 | |
| 5239 | // Find new and active data channels. |
| 5240 | for (const cricket::StreamParams& params : streams) { |
| 5241 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 5242 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5243 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5244 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 5245 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5246 | auto data_channel_it = rtp_data_channels_.find(label); |
| 5247 | if (data_channel_it == rtp_data_channels_.end()) { |
| 5248 | // This is a new data channel. |
| 5249 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 5250 | } else { |
| 5251 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 5252 | } |
| 5253 | existing_channels.push_back(label); |
| 5254 | } |
| 5255 | |
| 5256 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 5257 | } |
| 5258 | |
| 5259 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 5260 | const std::vector<std::string>& active_channels, |
| 5261 | bool is_local_update) { |
| 5262 | auto it = rtp_data_channels_.begin(); |
| 5263 | while (it != rtp_data_channels_.end()) { |
| 5264 | DataChannel* data_channel = it->second; |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 5265 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5266 | ++it; |
| 5267 | continue; |
| 5268 | } |
| 5269 | |
| 5270 | if (is_local_update) { |
| 5271 | data_channel->SetSendSsrc(0); |
| 5272 | } else { |
| 5273 | data_channel->RemotePeerRequestClose(); |
| 5274 | } |
| 5275 | |
| 5276 | if (data_channel->state() == DataChannel::kClosed) { |
| 5277 | rtp_data_channels_.erase(it); |
| 5278 | it = rtp_data_channels_.begin(); |
| 5279 | } else { |
| 5280 | ++it; |
| 5281 | } |
| 5282 | } |
| 5283 | } |
| 5284 | |
| 5285 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 5286 | uint32_t remote_ssrc) { |
| 5287 | rtc::scoped_refptr<DataChannel> channel( |
| 5288 | InternalCreateDataChannel(label, nullptr)); |
| 5289 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5290 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5291 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5292 | return; |
| 5293 | } |
| 5294 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 5295 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5296 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5297 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 5301 | const std::string& label, |
| 5302 | const InternalDataChannelInit* config) { |
| 5303 | if (IsClosed()) { |
| 5304 | return nullptr; |
| 5305 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5306 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5307 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5308 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 5309 | return nullptr; |
| 5310 | } |
| 5311 | InternalDataChannelInit new_config = |
| 5312 | config ? (*config) : InternalDataChannelInit(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5313 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5314 | if (new_config.id < 0) { |
| 5315 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5316 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5317 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5318 | RTC_LOG(LS_ERROR) |
| 5319 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5320 | return nullptr; |
| 5321 | } |
| 5322 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5323 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5324 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5325 | return nullptr; |
| 5326 | } |
| 5327 | } |
| 5328 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5329 | rtc::scoped_refptr<DataChannel> channel( |
| 5330 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5331 | if (!channel) { |
| 5332 | sid_allocator_.ReleaseSid(new_config.id); |
| 5333 | return nullptr; |
| 5334 | } |
| 5335 | |
| 5336 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 5337 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5338 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 5339 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5340 | return nullptr; |
| 5341 | } |
| 5342 | rtp_data_channels_[channel->label()] = channel; |
| 5343 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5344 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5345 | sctp_data_channels_.push_back(channel); |
| 5346 | channel->SignalClosed.connect(this, |
| 5347 | &PeerConnection::OnSctpDataChannelClosed); |
| 5348 | } |
| 5349 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 5350 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5351 | return channel; |
| 5352 | } |
| 5353 | |
| 5354 | bool PeerConnection::HasDataChannels() const { |
| 5355 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 5356 | } |
| 5357 | |
| 5358 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 11:29:38 +0100 | [diff] [blame] | 5359 | std::vector<rtc::scoped_refptr<DataChannel>> channels_to_close; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5360 | for (const auto& channel : sctp_data_channels_) { |
| 5361 | if (channel->id() < 0) { |
| 5362 | int sid; |
| 5363 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 11:29:38 +0100 | [diff] [blame] | 5364 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid, closing channel."; |
| 5365 | channels_to_close.push_back(channel); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5366 | continue; |
| 5367 | } |
| 5368 | channel->SetSctpSid(sid); |
| 5369 | } |
| 5370 | } |
Harald Alvestrand | 1f928d3 | 2019-03-28 11:29:38 +0100 | [diff] [blame] | 5371 | // Since closing modifies the list of channels, we have to do the actual |
| 5372 | // closing outside the loop. |
| 5373 | for (const auto& channel : channels_to_close) { |
| 5374 | channel->CloseAbruptly(); |
| 5375 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 5379 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5380 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 5381 | ++it) { |
| 5382 | if (it->get() == channel) { |
| 5383 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5384 | // After the closing procedure is done, it's safe to use this ID for |
| 5385 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5386 | sid_allocator_.ReleaseSid(channel->id()); |
| 5387 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 5388 | // Since this method is triggered by a signal from the DataChannel, |
| 5389 | // we can't free it directly here; we need to free it asynchronously. |
| 5390 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5391 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 5392 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 5393 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5394 | return; |
| 5395 | } |
| 5396 | } |
| 5397 | } |
| 5398 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5399 | void PeerConnection::OnDataChannelDestroyed() { |
| 5400 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 5401 | // DataChannel may callback to us and try to modify the list. |
| 5402 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 5403 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 5404 | for (const auto& kv : temp_rtp_dcs) { |
| 5405 | kv.second->OnTransportChannelDestroyed(); |
| 5406 | } |
| 5407 | |
| 5408 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 5409 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 5410 | for (const auto& channel : temp_sctp_dcs) { |
| 5411 | channel->OnTransportChannelDestroyed(); |
| 5412 | } |
| 5413 | } |
| 5414 | |
| 5415 | void PeerConnection::OnDataChannelOpenMessage( |
| 5416 | const std::string& label, |
| 5417 | const InternalDataChannelInit& config) { |
| 5418 | rtc::scoped_refptr<DataChannel> channel( |
| 5419 | InternalCreateDataChannel(label, &config)); |
| 5420 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5421 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5422 | return; |
| 5423 | } |
| 5424 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 5425 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5426 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5427 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 5428 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5429 | } |
| 5430 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5431 | bool PeerConnection::HandleOpenMessage_s( |
| 5432 | const cricket::ReceiveDataParams& params, |
| 5433 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5434 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 5435 | // Received OPEN message; parse and signal that a new data channel should |
| 5436 | // be created. |
| 5437 | std::string label; |
| 5438 | InternalDataChannelInit config; |
| 5439 | config.id = params.ssrc; |
| 5440 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 5441 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 5442 | << params.ssrc; |
| 5443 | return true; |
| 5444 | } |
| 5445 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5446 | OnDataChannelOpenMessage(label, config); |
| 5447 | return true; |
| 5448 | } |
| 5449 | return false; |
| 5450 | } |
| 5451 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5452 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5453 | PeerConnection::GetAudioTransceiver() const { |
| 5454 | // This method only works with Plan B SDP, where there is a single |
| 5455 | // audio/video transceiver. |
| 5456 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5457 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5458 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5459 | return transceiver; |
| 5460 | } |
| 5461 | } |
| 5462 | RTC_NOTREACHED(); |
| 5463 | return nullptr; |
| 5464 | } |
| 5465 | |
| 5466 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5467 | PeerConnection::GetVideoTransceiver() const { |
| 5468 | // This method only works with Plan B SDP, where there is a single |
| 5469 | // audio/video transceiver. |
| 5470 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5471 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5472 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5473 | return transceiver; |
| 5474 | } |
| 5475 | } |
| 5476 | RTC_NOTREACHED(); |
| 5477 | return nullptr; |
| 5478 | } |
| 5479 | |
| 5480 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 5481 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5482 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5483 | switch (type) { |
| 5484 | case cricket::MEDIA_TYPE_AUDIO: |
| 5485 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 5486 | case cricket::MEDIA_TYPE_VIDEO: |
| 5487 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 5488 | case cricket::MEDIA_TYPE_DATA: |
| 5489 | return false; |
| 5490 | } |
| 5491 | RTC_NOTREACHED(); |
| 5492 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5493 | } |
| 5494 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5495 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5496 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5497 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5498 | for (auto sender : transceiver->internal()->senders()) { |
| 5499 | if (sender->track() == track) { |
| 5500 | return sender; |
| 5501 | } |
| 5502 | } |
| 5503 | } |
| 5504 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5505 | } |
| 5506 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5507 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5508 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5509 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5510 | for (auto sender : transceiver->internal()->senders()) { |
| 5511 | if (sender->id() == sender_id) { |
| 5512 | return sender; |
| 5513 | } |
| 5514 | } |
| 5515 | } |
| 5516 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5517 | } |
| 5518 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5519 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 5520 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5521 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5522 | for (auto receiver : transceiver->internal()->receivers()) { |
| 5523 | if (receiver->id() == receiver_id) { |
| 5524 | return receiver; |
| 5525 | } |
| 5526 | } |
| 5527 | } |
| 5528 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5529 | } |
| 5530 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5531 | std::vector<PeerConnection::RtpSenderInfo>* |
| 5532 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 5533 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5534 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 5535 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5536 | ? &remote_audio_sender_infos_ |
| 5537 | : &remote_video_sender_infos_; |
| 5538 | } |
| 5539 | |
| 5540 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5541 | cricket::MediaType media_type) { |
| 5542 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5543 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5544 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 5545 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5546 | } |
| 5547 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5548 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 5549 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5550 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5551 | const std::string sender_id) const { |
| 5552 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5553 | if (sender_info.stream_id == stream_id && |
| 5554 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5555 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5556 | } |
| 5557 | } |
| 5558 | return nullptr; |
| 5559 | } |
| 5560 | |
| 5561 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 5562 | for (const auto& channel : sctp_data_channels_) { |
| 5563 | if (channel->id() == sid) { |
| 5564 | return channel; |
| 5565 | } |
| 5566 | } |
| 5567 | return nullptr; |
| 5568 | } |
| 5569 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5570 | PeerConnection::InitializePortAllocatorResult |
| 5571 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5572 | const cricket::ServerAddresses& stun_servers, |
| 5573 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5574 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5575 | RTC_DCHECK_RUN_ON(network_thread()); |
| 5576 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 5577 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5578 | // To handle both internal and externally created port allocator, we will |
| 5579 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5580 | int port_allocator_flags = port_allocator_->flags(); |
| 5581 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5582 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5583 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5584 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5585 | // by experiment. |
| 5586 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5587 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 5588 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 5589 | .find("Disabled") == 0) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5590 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5591 | } |
| 5592 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5593 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5594 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5595 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5596 | } |
| 5597 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5598 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5599 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5600 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5601 | } |
| 5602 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5603 | if (configuration.candidate_network_policy == |
| 5604 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5605 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5606 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5607 | } |
| 5608 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5609 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5610 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5611 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5612 | } |
| 5613 | |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5614 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5615 | // No step delay is used while allocating ports. |
| 5616 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5617 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5618 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 5619 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5620 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5621 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5622 | for (auto& turn_server : turn_servers_copy) { |
| 5623 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 5624 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5625 | // Call this last since it may create pooled allocator sessions using the |
| 5626 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5627 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5628 | stun_servers, std::move(turn_servers_copy), |
| 5629 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 5630 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5631 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 14:13:22 +0100 | [diff] [blame] | 5632 | |
| 5633 | InitializePortAllocatorResult res; |
| 5634 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 5635 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5636 | } |
| 5637 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 5638 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5639 | const cricket::ServerAddresses& stun_servers, |
| 5640 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5641 | IceTransportsType type, |
| 5642 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 5643 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5644 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 5645 | absl::optional<int> stun_candidate_keepalive_interval, |
| 5646 | bool have_local_description) { |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5647 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5648 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5649 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5650 | // size is not allowed, and changing the set of ICE servers will not result |
| 5651 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 5652 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5653 | port_allocator_->FreezeCandidatePool(); |
| 5654 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5655 | // Add the custom tls turn servers if they exist. |
| 5656 | auto turn_servers_copy = turn_servers; |
| 5657 | for (auto& turn_server : turn_servers_copy) { |
| 5658 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5659 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5660 | // Call this last since it may create pooled allocator sessions using the |
| 5661 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 5662 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5663 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 5664 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5665 | } |
| 5666 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 5667 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5668 | return factory_->channel_manager(); |
| 5669 | } |
| 5670 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 5671 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5672 | std::unique_ptr<RtcEventLogOutput> output, |
| 5673 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5674 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5675 | if (!event_log_) { |
| 5676 | return false; |
| 5677 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5678 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 11:59:57 +0100 | [diff] [blame] | 5682 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5683 | if (event_log_) { |
| 5684 | event_log_->StopLogging(); |
| 5685 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5686 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 5687 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5688 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5689 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5690 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5691 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5692 | if (channel && channel->content_name() == content_name) { |
| 5693 | return channel; |
| 5694 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5695 | } |
| 5696 | if (rtp_data_channel() && |
| 5697 | rtp_data_channel()->content_name() == content_name) { |
| 5698 | return rtp_data_channel(); |
| 5699 | } |
| 5700 | return nullptr; |
| 5701 | } |
| 5702 | |
| 5703 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5704 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5705 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5706 | RTC_LOG(LS_INFO) |
| 5707 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5708 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5709 | return false; |
| 5710 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5711 | if (!data_channel_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5712 | 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] | 5713 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5714 | return false; |
| 5715 | } |
| 5716 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5717 | absl::optional<rtc::SSLRole> dtls_role; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5718 | if (sctp_mid_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5719 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5720 | if (!dtls_role && is_caller_.has_value()) { |
| 5721 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5722 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5723 | *role = *dtls_role; |
| 5724 | return true; |
| 5725 | } |
| 5726 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5730 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 5731 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5732 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5733 | RTC_LOG(LS_INFO) |
| 5734 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5735 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5736 | return false; |
| 5737 | } |
| 5738 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5739 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5740 | if (dtls_role) { |
| 5741 | *role = *dtls_role; |
| 5742 | return true; |
| 5743 | } |
| 5744 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5745 | } |
| 5746 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5747 | void PeerConnection::SetSessionError(SessionError error, |
| 5748 | const std::string& error_desc) { |
| 5749 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5750 | if (error != session_error_) { |
| 5751 | session_error_ = error; |
| 5752 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5753 | } |
| 5754 | } |
| 5755 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5756 | RTCError PeerConnection::UpdateSessionState( |
| 5757 | SdpType type, |
| 5758 | cricket::ContentSource source, |
| 5759 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5760 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5761 | |
| 5762 | // If there's already a pending error then no state transition should happen. |
| 5763 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5764 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5765 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5766 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5767 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5768 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | // Update the signaling state according to the specified state machine (see |
| 5772 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5773 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5774 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5775 | ? PeerConnectionInterface::kHaveLocalOffer |
| 5776 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5777 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5778 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5779 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 5780 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5781 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5782 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5783 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 5784 | } |
| 5785 | |
| 5786 | // Update internal objects according to the session description's media |
| 5787 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5788 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5789 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 5790 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5791 | } |
| 5792 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5793 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5794 | } |
| 5795 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5796 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5797 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5798 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5799 | const SessionDescriptionInterface* sdesc = |
| 5800 | (source == cricket::CS_LOCAL ? local_description() |
| 5801 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5802 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5803 | |
| 5804 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5805 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5806 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5807 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5808 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5809 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5810 | continue; |
| 5811 | } |
| 5812 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5813 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5814 | if (!content_desc) { |
| 5815 | continue; |
| 5816 | } |
| 5817 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5818 | bool success = (source == cricket::CS_LOCAL) |
| 5819 | ? channel->SetLocalContent(content_desc, type, &error) |
| 5820 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5821 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5822 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5823 | } |
| 5824 | } |
| 5825 | |
| 5826 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 5827 | if (rtp_data_channel_) { |
| 5828 | const ContentInfo* data_content = |
| 5829 | cricket::GetFirstDataContent(sdesc->description()); |
| 5830 | if (data_content && !data_content->rejected) { |
| 5831 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5832 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5833 | if (data_desc) { |
| 5834 | std::string error; |
| 5835 | bool success = |
| 5836 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5837 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5838 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5839 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5840 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5841 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5842 | } |
| 5843 | } |
| 5844 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5845 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5846 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 5847 | // 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] | 5848 | if (sctp_mid_ && local_description() && remote_description()) { |
| 5849 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
| 5850 | transport_controller_->GetSctpTransport(*sctp_mid_); |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 5851 | auto local_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 5852 | local_description()->description()); |
| 5853 | auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 5854 | remote_description()->description()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5855 | if (sctp_transport && local_sctp_description && remote_sctp_description) { |
Harald Alvestrand | 8d3d6cf | 2019-05-16 11:49:17 +0200 | [diff] [blame] | 5856 | int max_message_size; |
| 5857 | // A remote max message size of zero means "any size supported". |
| 5858 | // We configure the connection with our own max message size. |
| 5859 | if (remote_sctp_description->max_message_size() == 0) { |
| 5860 | max_message_size = local_sctp_description->max_message_size(); |
| 5861 | } else { |
| 5862 | max_message_size = |
| 5863 | std::min(local_sctp_description->max_message_size(), |
| 5864 | remote_sctp_description->max_message_size()); |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 5865 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5866 | sctp_transport->Start(local_sctp_description->port(), |
| 5867 | remote_sctp_description->port(), max_message_size); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5868 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5869 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5870 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5871 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5872 | } |
| 5873 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5874 | RTCError PeerConnection::PushdownTransportDescription( |
| 5875 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5876 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5877 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5878 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5879 | if (source == cricket::CS_LOCAL) { |
| 5880 | const SessionDescriptionInterface* sdesc = local_description(); |
| 5881 | RTC_DCHECK(sdesc); |
| 5882 | return transport_controller_->SetLocalDescription(type, |
| 5883 | sdesc->description()); |
| 5884 | } else { |
| 5885 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 5886 | RTC_DCHECK(sdesc); |
| 5887 | return transport_controller_->SetRemoteDescription(type, |
| 5888 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5889 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | bool PeerConnection::GetTransportDescription( |
| 5893 | const SessionDescription* description, |
| 5894 | const std::string& content_name, |
| 5895 | cricket::TransportDescription* tdesc) { |
| 5896 | if (!description || !tdesc) { |
| 5897 | return false; |
| 5898 | } |
| 5899 | const TransportInfo* transport_info = |
| 5900 | description->GetTransportInfoByName(content_name); |
| 5901 | if (!transport_info) { |
| 5902 | return false; |
| 5903 | } |
| 5904 | *tdesc = transport_info->description; |
| 5905 | return true; |
| 5906 | } |
| 5907 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5908 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 5909 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 5910 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5911 | switch (config.continual_gathering_policy) { |
| 5912 | case PeerConnectionInterface::GATHER_ONCE: |
| 5913 | gathering_policy = cricket::GATHER_ONCE; |
| 5914 | break; |
| 5915 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 5916 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 5917 | break; |
| 5918 | default: |
| 5919 | RTC_NOTREACHED(); |
| 5920 | gathering_policy = cricket::GATHER_ONCE; |
| 5921 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5922 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5923 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5924 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 5925 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5926 | ice_config.prioritize_most_likely_candidate_pairs = |
| 5927 | config.prioritize_most_likely_ice_candidate_pairs; |
| 5928 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5929 | RTCConfigurationToIceConfigOptionalInt( |
| 5930 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5931 | ice_config.continual_gathering_policy = gathering_policy; |
| 5932 | ice_config.presume_writable_when_fully_relayed = |
| 5933 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 5934 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 5935 | config.surface_ice_candidates_on_ice_transport_type_changed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 5936 | ice_config.ice_check_interval_strong_connectivity = |
| 5937 | config.ice_check_interval_strong_connectivity; |
| 5938 | ice_config.ice_check_interval_weak_connectivity = |
| 5939 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5940 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-29 23:08:51 -0800 | [diff] [blame] | 5941 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 5942 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 5943 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5944 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5945 | ice_config.regather_all_networks_interval_range = |
| 5946 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5947 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5948 | return ice_config; |
| 5949 | } |
| 5950 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5951 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5952 | const rtc::CopyOnWriteBuffer& payload, |
| 5953 | cricket::SendDataResult* result) { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 5954 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5955 | if (data_channel_transport_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5956 | SendDataParams send_params; |
| 5957 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 5958 | send_params.ordered = params.ordered; |
| 5959 | if (params.max_rtx_count >= 0) { |
| 5960 | send_params.max_rtx_count = params.max_rtx_count; |
| 5961 | } else if (params.max_rtx_ms >= 0) { |
| 5962 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 5963 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5964 | |
| 5965 | RTCError error = network_thread()->Invoke<RTCError>( |
| 5966 | RTC_FROM_HERE, [this, params, send_params, payload] { |
| 5967 | return data_channel_transport_->SendData(params.sid, send_params, |
| 5968 | payload); |
| 5969 | }); |
| 5970 | |
| 5971 | if (error.ok()) { |
| 5972 | *result = cricket::SendDataResult::SDR_SUCCESS; |
| 5973 | return true; |
| 5974 | } else if (error.type() == RTCErrorType::RESOURCE_EXHAUSTED) { |
| 5975 | // SCTP transport uses RESOURCE_EXHAUSTED when it's blocked. |
| 5976 | // TODO(mellem): Stop using RTCError here and get rid of the mapping. |
| 5977 | *result = cricket::SendDataResult::SDR_BLOCK; |
| 5978 | return false; |
| 5979 | } |
| 5980 | *result = cricket::SendDataResult::SDR_ERROR; |
| 5981 | return false; |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 5982 | } else if (rtp_data_channel_) { |
| 5983 | return rtp_data_channel_->SendData(params, payload, result); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5984 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 5985 | RTC_LOG(LS_ERROR) << "SendData called before transport is ready"; |
| 5986 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5987 | } |
| 5988 | |
| 5989 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5990 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 5991 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5992 | // Don't log an error here, because DataChannels are expected to call |
| 5993 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5994 | // whether or not the underlying transport is ready. |
| 5995 | return false; |
| 5996 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 5997 | if (data_channel_transport_) { |
| 5998 | SignalDataChannelTransportWritable_s.connect(webrtc_data_channel, |
| 5999 | &DataChannel::OnChannelReady); |
| 6000 | SignalDataChannelTransportReceivedData_s.connect( |
| 6001 | webrtc_data_channel, &DataChannel::OnDataReceived); |
| 6002 | SignalDataChannelTransportChannelClosing_s.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6003 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6004 | SignalDataChannelTransportChannelClosed_s.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6005 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6006 | } |
| 6007 | if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6008 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 6009 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 6010 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 6011 | &DataChannel::OnDataReceived); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6012 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6013 | return true; |
| 6014 | } |
| 6015 | |
| 6016 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6017 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6018 | if (!rtp_data_channel_ && !data_channel_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6019 | RTC_LOG(LS_ERROR) |
| 6020 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 6021 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6022 | return; |
| 6023 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6024 | if (data_channel_transport_) { |
| 6025 | SignalDataChannelTransportWritable_s.disconnect(webrtc_data_channel); |
| 6026 | SignalDataChannelTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 6027 | SignalDataChannelTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 6028 | SignalDataChannelTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 6029 | } |
| 6030 | if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6031 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 6032 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6033 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | void PeerConnection::AddSctpDataStream(int sid) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6037 | if (data_channel_transport_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6038 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 6039 | if (data_channel_transport_) { |
| 6040 | data_channel_transport_->OpenChannel(sid); |
| 6041 | } |
| 6042 | }); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6043 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | void PeerConnection::RemoveSctpDataStream(int sid) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6047 | if (data_channel_transport_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6048 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, sid] { |
| 6049 | if (data_channel_transport_) { |
| 6050 | data_channel_transport_->CloseChannel(sid); |
| 6051 | } |
| 6052 | }); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6053 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | bool PeerConnection::ReadyToSendData() const { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6057 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6058 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6059 | (data_channel_transport_ && data_channel_transport_ready_to_send_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6060 | } |
| 6061 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6062 | void PeerConnection::OnDataReceived(int channel_id, |
| 6063 | DataMessageType type, |
| 6064 | const rtc::CopyOnWriteBuffer& buffer) { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 6065 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6066 | cricket::ReceiveDataParams params; |
| 6067 | params.sid = channel_id; |
| 6068 | params.type = ToCricketDataMessageType(type); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6069 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6070 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 6071 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6072 | if (!HandleOpenMessage_s(params, buffer)) { |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6073 | SignalDataChannelTransportReceivedData_s(params, buffer); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6074 | } |
| 6075 | }); |
| 6076 | } |
| 6077 | |
| 6078 | void PeerConnection::OnChannelClosing(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 6079 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6080 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6081 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 6082 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6083 | SignalDataChannelTransportChannelClosing_s(channel_id); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6084 | }); |
| 6085 | } |
| 6086 | |
| 6087 | void PeerConnection::OnChannelClosed(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 11:37:28 +0200 | [diff] [blame] | 6088 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6089 | data_channel_transport_invoker_->AsyncInvoke<void>( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6090 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 6091 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6092 | SignalDataChannelTransportChannelClosed_s(channel_id); |
| 6093 | }); |
| 6094 | } |
| 6095 | |
| 6096 | void PeerConnection::OnReadyToSend() { |
| 6097 | RTC_DCHECK_RUN_ON(network_thread()); |
| 6098 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 6099 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 6100 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6101 | data_channel_transport_ready_to_send_ = true; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6102 | SignalDataChannelTransportWritable_s( |
| 6103 | data_channel_transport_ready_to_send_); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6104 | }); |
| 6105 | } |
| 6106 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6107 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 6108 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6109 | if (sctp_mid_ && transport_controller_) { |
| 6110 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 6111 | if (dtls_transport) { |
| 6112 | return dtls_transport->transport_name(); |
| 6113 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6114 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6115 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6116 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6117 | } |
| 6118 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 6119 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 6120 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 6121 | network_thread()->Invoke<void>( |
| 6122 | RTC_FROM_HERE, |
| 6123 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 6124 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 6125 | return candidate_states_list; |
| 6126 | } |
| 6127 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6128 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 6129 | const { |
Karl Wiberg | a58e169 | 2019-03-26 13:33:43 +0100 | [diff] [blame] | 6130 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6131 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6132 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6133 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6134 | if (channel) { |
| 6135 | transport_names_by_mid[channel->content_name()] = |
| 6136 | channel->transport_name(); |
| 6137 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6138 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6139 | if (rtp_data_channel_) { |
| 6140 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 6141 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6142 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6143 | if (data_channel_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6144 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6145 | RTC_DCHECK(transport_name); |
| 6146 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6147 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6148 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6149 | } |
| 6150 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6151 | std::map<std::string, cricket::TransportStats> |
| 6152 | PeerConnection::GetTransportStatsByNames( |
| 6153 | const std::set<std::string>& transport_names) { |
| 6154 | if (!network_thread()->IsCurrent()) { |
| 6155 | return network_thread() |
| 6156 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 6157 | RTC_FROM_HERE, |
| 6158 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6159 | } |
Karl Wiberg | 2cc368f | 2019-04-02 11:31:56 +0200 | [diff] [blame] | 6160 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 6161 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 6162 | for (const std::string& transport_name : transport_names) { |
| 6163 | cricket::TransportStats transport_stats; |
| 6164 | bool success = |
| 6165 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 6166 | if (success) { |
| 6167 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 6168 | } else { |
| 6169 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 6170 | << transport_name; |
| 6171 | } |
| 6172 | } |
| 6173 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | bool PeerConnection::GetLocalCertificate( |
| 6177 | const std::string& transport_name, |
| 6178 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6179 | if (!certificate) { |
| 6180 | return false; |
| 6181 | } |
| 6182 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 6183 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6184 | } |
| 6185 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 6186 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6187 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 6188 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6189 | } |
| 6190 | |
| 6191 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 6192 | return data_channel_type_; |
| 6193 | } |
| 6194 | |
| 6195 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 15:36:53 +0200 | [diff] [blame] | 6196 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6197 | return pending_ice_restarts_.find(content_name) != |
| 6198 | pending_ice_restarts_.end(); |
| 6199 | } |
| 6200 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6201 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 6202 | return transport_controller_->NeedsIceRestart(content_name); |
| 6203 | } |
| 6204 | |
| 6205 | void PeerConnection::OnCertificateReady( |
| 6206 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 6207 | transport_controller_->SetLocalCertificate(certificate); |
| 6208 | } |
| 6209 | |
| 6210 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6211 | SetSessionError(SessionError::kTransport, |
| 6212 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6213 | } |
| 6214 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 6215 | void PeerConnection::OnTransportControllerConnectionState( |
| 6216 | cricket::IceConnectionState state) { |
| 6217 | switch (state) { |
| 6218 | case cricket::kIceConnectionConnecting: |
| 6219 | // If the current state is Connected or Completed, then there were |
| 6220 | // writable channels but now there are not, so the next state must |
| 6221 | // be Disconnected. |
| 6222 | // kIceConnectionConnecting is currently used as the default, |
| 6223 | // un-connected state by the TransportController, so its only use is |
| 6224 | // detecting disconnections. |
| 6225 | if (ice_connection_state_ == |
| 6226 | PeerConnectionInterface::kIceConnectionConnected || |
| 6227 | ice_connection_state_ == |
| 6228 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 6229 | SetIceConnectionState( |
| 6230 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 6231 | } |
| 6232 | break; |
| 6233 | case cricket::kIceConnectionFailed: |
| 6234 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 6235 | break; |
| 6236 | case cricket::kIceConnectionConnected: |
| 6237 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 6238 | "all transports are writable."; |
| 6239 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6240 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6241 | break; |
| 6242 | case cricket::kIceConnectionCompleted: |
| 6243 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 6244 | "all transports are complete."; |
| 6245 | if (ice_connection_state_ != |
| 6246 | PeerConnectionInterface::kIceConnectionConnected) { |
| 6247 | // If jumping directly from "checking" to "connected", |
| 6248 | // signal "connected" first. |
| 6249 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6250 | } |
| 6251 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 6252 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6253 | ReportTransportStats(); |
| 6254 | break; |
| 6255 | default: |
| 6256 | RTC_NOTREACHED(); |
| 6257 | } |
| 6258 | } |
| 6259 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6260 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 6261 | const std::string& transport_name, |
| 6262 | const cricket::Candidates& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6263 | int sdp_mline_index; |
| 6264 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6265 | RTC_LOG(LS_ERROR) |
| 6266 | << "OnTransportControllerCandidatesGathered: content name " |
| 6267 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6268 | return; |
| 6269 | } |
| 6270 | |
| 6271 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 6272 | citer != candidates.end(); ++citer) { |
| 6273 | // Use transport_name as the candidate media id. |
| 6274 | std::unique_ptr<JsepIceCandidate> candidate( |
| 6275 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 6276 | if (local_description()) { |
| 6277 | mutable_local_description()->AddCandidate(candidate.get()); |
| 6278 | } |
| 6279 | OnIceCandidate(std::move(candidate)); |
| 6280 | } |
| 6281 | } |
| 6282 | |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 6283 | void PeerConnection::OnTransportControllerCandidateError( |
| 6284 | const cricket::IceCandidateErrorEvent& event) { |
| 6285 | OnIceCandidateError(event.host_candidate, event.url, event.error_code, |
| 6286 | event.error_text); |
| 6287 | } |
| 6288 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6289 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 6290 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6291 | // Sanity check. |
| 6292 | for (const cricket::Candidate& candidate : candidates) { |
| 6293 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6294 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6295 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6296 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6297 | return; |
| 6298 | } |
| 6299 | } |
| 6300 | |
| 6301 | if (local_description()) { |
| 6302 | mutable_local_description()->RemoveCandidates(candidates); |
| 6303 | } |
| 6304 | OnIceCandidatesRemoved(candidates); |
| 6305 | } |
| 6306 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 6307 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 6308 | const cricket::CandidatePairChangeEvent& event) { |
| 6309 | OnSelectedCandidatePairChanged(event); |
| 6310 | } |
| 6311 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6312 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 6313 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6314 | RTC_HISTOGRAM_ENUMERATION( |
| 6315 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 6316 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6317 | } |
| 6318 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6319 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6320 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6321 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 6322 | if (channel && !channel->enabled()) { |
| 6323 | channel->Enable(true); |
| 6324 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6325 | } |
| 6326 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6327 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6328 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6329 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | // Returns the media index for a local ice candidate given the content name. |
| 6333 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 6334 | const std::string& content_name, |
| 6335 | int* sdp_mline_index) { |
| 6336 | if (!local_description() || !sdp_mline_index) { |
| 6337 | return false; |
| 6338 | } |
| 6339 | |
| 6340 | bool content_found = false; |
| 6341 | const ContentInfos& contents = local_description()->description()->contents(); |
| 6342 | for (size_t index = 0; index < contents.size(); ++index) { |
| 6343 | if (contents[index].name == content_name) { |
| 6344 | *sdp_mline_index = static_cast<int>(index); |
| 6345 | content_found = true; |
| 6346 | break; |
| 6347 | } |
| 6348 | } |
| 6349 | return content_found; |
| 6350 | } |
| 6351 | |
| 6352 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 6353 | const SessionDescriptionInterface* remote_desc) { |
| 6354 | if (!remote_desc) { |
| 6355 | return true; |
| 6356 | } |
| 6357 | bool ret = true; |
| 6358 | |
| 6359 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 6360 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 6361 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 6362 | const IceCandidateInterface* candidate = candidates->at(n); |
| 6363 | bool valid = false; |
| 6364 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 6365 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6366 | RTC_LOG(LS_INFO) |
| 6367 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 6368 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6369 | } |
| 6370 | continue; |
| 6371 | } |
| 6372 | ret = UseCandidate(candidate); |
| 6373 | if (!ret) { |
| 6374 | break; |
| 6375 | } |
| 6376 | } |
| 6377 | } |
| 6378 | return ret; |
| 6379 | } |
| 6380 | |
| 6381 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6382 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 6383 | FindContentInfo(remote_description(), candidate); |
| 6384 | if (!result.ok()) { |
| 6385 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. " |
| 6386 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6387 | return false; |
| 6388 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6389 | std::vector<cricket::Candidate> candidates; |
| 6390 | candidates.push_back(candidate->candidate()); |
| 6391 | // Invoking BaseSession method to handle remote candidates. |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6392 | RTCError error = transport_controller_->AddRemoteCandidates( |
| 6393 | result.value()->name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6394 | if (error.ok()) { |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6395 | ReportRemoteIceCandidateAdded(candidate->candidate()); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6396 | // Candidates successfully submitted for checking. |
| 6397 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6398 | ice_connection_state_ == |
| 6399 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6400 | // If state is New, then the session has just gotten its first remote ICE |
| 6401 | // candidates, so go to Checking. |
| 6402 | // If state is Disconnected, the session is re-using old candidates or |
| 6403 | // receiving additional ones, so go to Checking. |
| 6404 | // If state is Connected, stay Connected. |
| 6405 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6406 | // newly added transport, then the state actually _should_ move to |
| 6407 | // checking. Add a way to distinguish that case. |
| 6408 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6409 | } |
| 6410 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 6411 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6412 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6413 | } |
| 6414 | return true; |
| 6415 | } |
| 6416 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6417 | RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo( |
| 6418 | const SessionDescriptionInterface* description, |
| 6419 | const IceCandidateInterface* candidate) { |
| 6420 | if (candidate->sdp_mline_index() >= 0) { |
| 6421 | size_t mediacontent_index = |
| 6422 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 6423 | size_t content_size = description->description()->contents().size(); |
| 6424 | if (mediacontent_index < content_size) { |
| 6425 | return &description->description()->contents()[mediacontent_index]; |
| 6426 | } else { |
| 6427 | return RTCError(RTCErrorType::INVALID_RANGE, |
| 6428 | "Media line index (" + |
| 6429 | rtc::ToString(candidate->sdp_mline_index()) + |
| 6430 | ") out of range (number of mlines: " + |
| 6431 | rtc::ToString(content_size) + ")."); |
| 6432 | } |
| 6433 | } else if (!candidate->sdp_mid().empty()) { |
| 6434 | auto& contents = description->description()->contents(); |
| 6435 | auto it = absl::c_find_if( |
| 6436 | contents, [candidate](const cricket::ContentInfo& content_info) { |
| 6437 | return content_info.mid() == candidate->sdp_mid(); |
| 6438 | }); |
| 6439 | if (it == contents.end()) { |
| 6440 | return RTCError( |
| 6441 | RTCErrorType::INVALID_PARAMETER, |
| 6442 | "Mid " + candidate->sdp_mid() + |
| 6443 | " specified but no media section with that mid found."); |
| 6444 | } else { |
| 6445 | return &*it; |
| 6446 | } |
| 6447 | } |
| 6448 | |
| 6449 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 6450 | "Neither sdp_mline_index nor sdp_mid specified."); |
| 6451 | } |
| 6452 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6453 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6454 | // Destroy video channel first since it may have a pointer to the |
| 6455 | // voice channel. |
| 6456 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6457 | if (!video_info || video_info->rejected) { |
| 6458 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6459 | } |
| 6460 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6461 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6462 | if (!audio_info || audio_info->rejected) { |
| 6463 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6464 | } |
| 6465 | |
| 6466 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6467 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6468 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6469 | } |
| 6470 | } |
| 6471 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6472 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6473 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6474 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6475 | if (configuration_.bundle_policy == |
| 6476 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6477 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6478 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6479 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6480 | "max-bundle configured but session description " |
| 6481 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6482 | } |
| 6483 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 13:47:42 +0100 | [diff] [blame] | 6484 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6485 | } |
| 6486 | |
| 6487 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6488 | // Creating the media channels. Transports should already have been created |
| 6489 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6490 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6491 | if (voice && !voice->rejected && |
| 6492 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6493 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6494 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6495 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6496 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6497 | } |
| 6498 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 6499 | } |
| 6500 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6501 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6502 | if (video && !video->rejected && |
| 6503 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6504 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6505 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6506 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6507 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6508 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6509 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6510 | } |
| 6511 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6512 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6513 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6514 | !rtp_data_channel_ && !data_channel_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6515 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6516 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6517 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6518 | } |
| 6519 | } |
| 6520 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6521 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6522 | } |
| 6523 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6524 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6525 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6526 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6527 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6528 | MediaTransportConfig media_transport_config = |
| 6529 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6530 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6531 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6532 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6533 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 6534 | GetCryptoOptions(), &ssrc_generator_, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6535 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6536 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6537 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6538 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6539 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6540 | voice_channel->SignalSentPacket.connect(this, |
| 6541 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6542 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6543 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6544 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6545 | } |
| 6546 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6547 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6548 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6549 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6550 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6551 | MediaTransportConfig media_transport_config = |
| 6552 | transport_controller_->GetMediaTransportConfig(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6553 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6554 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6555 | call_ptr_, configuration_.media_config, rtp_transport, |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 6556 | media_transport_config, signaling_thread(), mid, SrtpRequired(), |
| 6557 | GetCryptoOptions(), &ssrc_generator_, video_options_, |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 6558 | video_bitrate_allocator_factory_.get()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6559 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6560 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6561 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6562 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6563 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6564 | video_channel->SignalSentPacket.connect(this, |
| 6565 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6566 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6567 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6568 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6569 | } |
| 6570 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6571 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6572 | switch (data_channel_type_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6573 | case cricket::DCT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6574 | case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP: |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6575 | case cricket::DCT_DATA_CHANNEL_TRANSPORT: |
| 6576 | case cricket::DCT_MEDIA_TRANSPORT: |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6577 | if (!network_thread()->Invoke<bool>( |
| 6578 | RTC_FROM_HERE, |
| 6579 | rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this, |
| 6580 | mid))) { |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6581 | return false; |
| 6582 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6583 | |
| 6584 | // All non-RTP data channels must initialize |sctp_data_channels_|. |
| 6585 | for (const auto& channel : sctp_data_channels_) { |
| 6586 | channel->OnTransportChannelCreated(); |
| 6587 | } |
| 6588 | return true; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6589 | case cricket::DCT_RTP: |
| 6590 | default: |
| 6591 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 6592 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 6593 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6594 | SrtpRequired(), GetCryptoOptions(), &ssrc_generator_); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6595 | if (!rtp_data_channel_) { |
| 6596 | return false; |
| 6597 | } |
| 6598 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 6599 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6600 | rtp_data_channel_->SignalSentPacket.connect( |
| 6601 | this, &PeerConnection::OnSentPacket_w); |
| 6602 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 6603 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6604 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6605 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6606 | } |
| 6607 | |
| 6608 | Call::Stats PeerConnection::GetCallStats() { |
| 6609 | if (!worker_thread()->IsCurrent()) { |
| 6610 | return worker_thread()->Invoke<Call::Stats>( |
| 6611 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6612 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 6613 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6614 | if (call_) { |
| 6615 | return call_->GetStats(); |
| 6616 | } else { |
| 6617 | return Call::Stats(); |
| 6618 | } |
| 6619 | } |
| 6620 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6621 | bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6622 | DataChannelTransportInterface* transport = |
| 6623 | transport_controller_->GetDataChannelTransport(mid); |
| 6624 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 6625 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6626 | << "Data channel transport is not available for data channels, mid=" |
| 6627 | << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6628 | return false; |
| 6629 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6630 | RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6631 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6632 | data_channel_transport_ = transport; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 6633 | data_channel_transport_invoker_ = std::make_unique<rtc::AsyncInvoker>(); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6634 | sctp_mid_ = mid; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6635 | |
| 6636 | // Note: setting the data sink and checking initial state must be done last, |
| 6637 | // after setting up the data channel. Setting the data sink may trigger |
| 6638 | // callbacks to PeerConnection which require the transport to be completely |
| 6639 | // set up (eg. OnReadyToSend()). |
| 6640 | transport->SetDataSink(this); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6641 | return true; |
| 6642 | } |
| 6643 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6644 | void PeerConnection::TeardownDataChannelTransport_n() { |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6645 | if (!sctp_mid_ && !data_channel_transport_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6646 | return; |
| 6647 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6648 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
| 6649 | << *sctp_mid_; |
| 6650 | |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6651 | // |sctp_mid_| may still be active through an SCTP transport. If not, unset |
| 6652 | // it. |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6653 | sctp_mid_.reset(); |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 +0000 | [diff] [blame] | 6654 | data_channel_transport_invoker_ = nullptr; |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 6655 | if (data_channel_transport_) { |
| 6656 | data_channel_transport_->SetDataSink(nullptr); |
| 6657 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 6658 | data_channel_transport_ = nullptr; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6659 | } |
| 6660 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6661 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6662 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6663 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6664 | if (!bundle_enabled) |
| 6665 | return true; |
| 6666 | |
| 6667 | const cricket::ContentGroup* bundle_group = |
| 6668 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6669 | RTC_DCHECK(bundle_group != NULL); |
| 6670 | |
| 6671 | const cricket::ContentInfos& contents = desc->contents(); |
| 6672 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6673 | citer != contents.end(); ++citer) { |
| 6674 | const cricket::ContentInfo* content = (&*citer); |
| 6675 | RTC_DCHECK(content != NULL); |
| 6676 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 6677 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6678 | if (!HasRtcpMuxEnabled(content)) |
| 6679 | return false; |
| 6680 | } |
| 6681 | } |
| 6682 | // RTCP-MUX is enabled in all the contents. |
| 6683 | return true; |
| 6684 | } |
| 6685 | |
| 6686 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6687 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6688 | } |
| 6689 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6690 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6691 | std::set<std::string> mids; |
| 6692 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 11:32:20 -0800 | [diff] [blame] | 6693 | if (content.name.empty()) { |
| 6694 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6695 | "A media section is missing a MID attribute."); |
| 6696 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6697 | if (!mids.insert(content.name).second) { |
| 6698 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6699 | "Duplicate a=mid value '" + content.name + "'."); |
| 6700 | } |
| 6701 | } |
| 6702 | return RTCError::OK(); |
| 6703 | } |
| 6704 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6705 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6706 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6707 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6708 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6709 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6710 | } |
| 6711 | |
| 6712 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6713 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6714 | } |
| 6715 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6716 | SdpType type = sdesc->GetType(); |
| 6717 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6718 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6719 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 6720 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6721 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6722 | } |
| 6723 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6724 | RTCError error = ValidateMids(*sdesc->description()); |
| 6725 | if (!error.ok()) { |
| 6726 | return error; |
| 6727 | } |
| 6728 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6729 | // Verify crypto settings. |
| 6730 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6731 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6732 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6733 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6734 | if (!crypto_error.ok()) { |
| 6735 | return crypto_error; |
| 6736 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | // Verify ice-ufrag and ice-pwd. |
| 6740 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6741 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6742 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6743 | } |
| 6744 | |
| 6745 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6746 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6747 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6748 | } |
| 6749 | |
| 6750 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6751 | // m-lines that do not rtcp-mux enabled. |
| 6752 | |
| 6753 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6754 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6755 | // With an answer we want to compare the new answer session description with |
| 6756 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6757 | const cricket::SessionDescription* offer_desc = |
| 6758 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6759 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6760 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6761 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6762 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6763 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6764 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6765 | } |
| 6766 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6767 | // The re-offers should respect the order of m= sections in current |
| 6768 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6769 | // With a re-offer, either the current local or current remote descriptions |
| 6770 | // could be the most up to date, so we would like to check against both of |
| 6771 | // them if they exist. It could be the case that one of them has a 0 port |
| 6772 | // for a media section, but the other does not. This is important to check |
| 6773 | // against in the case that we are recycling an m= section. |
| 6774 | const cricket::SessionDescription* current_desc = nullptr; |
| 6775 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6776 | if (local_description()) { |
| 6777 | current_desc = local_description()->description(); |
| 6778 | if (remote_description()) { |
| 6779 | secondary_current_desc = remote_description()->description(); |
| 6780 | } |
| 6781 | } else if (remote_description()) { |
| 6782 | current_desc = remote_description()->description(); |
| 6783 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6784 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6785 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6786 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6787 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6788 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6789 | } |
| 6790 | } |
| 6791 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6792 | if (IsUnifiedPlan()) { |
| 6793 | // Ensure that each audio and video media section has at most one |
| 6794 | // "StreamParams". This will return an error if receiving a session |
| 6795 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6796 | // same type. With Unified Plan, there can only be at most one track per |
| 6797 | // media section. |
| 6798 | for (const ContentInfo& content : sdesc->description()->contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 6799 | const MediaContentDescription& desc = *content.media_description(); |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6800 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6801 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6802 | desc.streams().size() > 1u) { |
| 6803 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6804 | "Media section has more than one track specified " |
| 6805 | "with a=ssrc lines which is not supported with " |
| 6806 | "Unified Plan."); |
| 6807 | } |
| 6808 | } |
| 6809 | } |
| 6810 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6811 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6812 | } |
| 6813 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6814 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6815 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6816 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6817 | return (state == PeerConnectionInterface::kStable) || |
| 6818 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6819 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6820 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6821 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6822 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6823 | } |
| 6824 | } |
| 6825 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6826 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6827 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6828 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6829 | return (state == PeerConnectionInterface::kStable) || |
| 6830 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6831 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6832 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6833 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6834 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6835 | } |
| 6836 | } |
| 6837 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6838 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6839 | switch (error) { |
| 6840 | case SessionError::kNone: |
| 6841 | return "ERROR_NONE"; |
| 6842 | case SessionError::kContent: |
| 6843 | return "ERROR_CONTENT"; |
| 6844 | case SessionError::kTransport: |
| 6845 | return "ERROR_TRANSPORT"; |
| 6846 | } |
| 6847 | RTC_NOTREACHED(); |
| 6848 | return ""; |
| 6849 | } |
| 6850 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6851 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 6852 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6853 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6854 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 6855 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6856 | } |
| 6857 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6858 | void PeerConnection::ReportSdpFormatReceived( |
| 6859 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6860 | int num_audio_mlines = 0; |
| 6861 | int num_video_mlines = 0; |
| 6862 | int num_audio_tracks = 0; |
| 6863 | int num_video_tracks = 0; |
| 6864 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6865 | cricket::MediaType media_type = content.media_description()->type(); |
| 6866 | int num_tracks = std::max( |
| 6867 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6868 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6869 | num_audio_mlines += 1; |
| 6870 | num_audio_tracks += num_tracks; |
| 6871 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6872 | num_video_mlines += 1; |
| 6873 | num_video_tracks += num_tracks; |
| 6874 | } |
| 6875 | } |
| 6876 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6877 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6878 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6879 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6880 | format = kSdpFormatReceivedComplexPlanB; |
| 6881 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6882 | format = kSdpFormatReceivedSimple; |
| 6883 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6884 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6885 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6886 | } |
| 6887 | |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6888 | void PeerConnection::ReportIceCandidateCollected( |
| 6889 | const cricket::Candidate& candidate) { |
| 6890 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 6891 | if (candidate.address().IsPrivateIP()) { |
| 6892 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 6893 | } |
| 6894 | if (candidate.address().IsUnresolvedIP()) { |
| 6895 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 6896 | } |
| 6897 | if (candidate.address().family() == AF_INET6) { |
| 6898 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 6899 | } |
| 6900 | } |
| 6901 | |
| 6902 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 6903 | const cricket::Candidate& candidate) { |
| 6904 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 6905 | if (candidate.address().IsPrivateIP()) { |
| 6906 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 6907 | } |
| 6908 | if (candidate.address().IsUnresolvedIP()) { |
| 6909 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 6910 | } |
| 6911 | if (candidate.address().family() == AF_INET6) { |
| 6912 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 6913 | } |
| 6914 | } |
| 6915 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6916 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6917 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6918 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6919 | } |
| 6920 | |
| 6921 | void PeerConnection::ReportUsagePattern() const { |
| 6922 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6923 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6924 | usage_event_accumulator_, |
| 6925 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6926 | const int bad_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6927 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6928 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6929 | const int good_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 11:57:17 -0700 | [diff] [blame] | 6930 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6931 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6932 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6933 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6934 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6935 | // If called after close(), we can't report, because observer may have |
| 6936 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6937 | if (observer_) { |
| 6938 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6939 | } else { |
| 6940 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6941 | << usage_event_accumulator_ |
| 6942 | << " observed after observer shutdown"; |
| 6943 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6944 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6945 | } |
| 6946 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6947 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 6948 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6949 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6950 | switch (answer.description()->msid_signaling()) { |
| 6951 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6952 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6953 | break; |
| 6954 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6955 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6956 | break; |
| 6957 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6958 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6959 | break; |
| 6960 | case cricket::kMsidSignalingMediaSection | |
| 6961 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6962 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6963 | break; |
| 6964 | default: |
| 6965 | RTC_NOTREACHED(); |
| 6966 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6967 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 6968 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6969 | } |
| 6970 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6971 | // We need to check the local/remote description for the Transport instead of |
| 6972 | // the session, because a new Transport added during renegotiation may have |
| 6973 | // them unset while the session has them set from the previous negotiation. |
| 6974 | // Not doing so may trigger the auto generation of transport description and |
| 6975 | // mess up DTLS identity information, ICE credential, etc. |
| 6976 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 6977 | const IceCandidateInterface* candidate, |
| 6978 | const SessionDescriptionInterface* remote_desc, |
| 6979 | bool* valid) { |
| 6980 | *valid = true; |
| 6981 | |
| 6982 | const SessionDescriptionInterface* current_remote_desc = |
| 6983 | remote_desc ? remote_desc : remote_description(); |
| 6984 | |
| 6985 | if (!current_remote_desc) { |
| 6986 | return false; |
| 6987 | } |
| 6988 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6989 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 6990 | FindContentInfo(current_remote_desc, candidate); |
| 6991 | if (!result.ok()) { |
| 6992 | RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. " |
| 6993 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6994 | |
| 6995 | *valid = false; |
| 6996 | return false; |
| 6997 | } |
| 6998 | |
Guido Urdaneta | 4163317 | 2019-05-23 20:12:29 +0200 | [diff] [blame] | 6999 | std::string transport_name = GetTransportName(result.value()->name); |
| 7000 | return !transport_name.empty(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7001 | } |
| 7002 | |
| 7003 | bool PeerConnection::SrtpRequired() const { |
Bjorn A Mellem | 5985a04 | 2019-06-28 14:19:38 -0700 | [diff] [blame] | 7004 | return !use_datagram_transport_ && |
Anton Sukhanov | 316f3ac | 2019-05-23 15:50:38 -0700 | [diff] [blame] | 7005 | (dtls_enabled_ || |
| 7006 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7007 | } |
| 7008 | |
| 7009 | void PeerConnection::OnTransportControllerGatheringState( |
| 7010 | cricket::IceGatheringState state) { |
| 7011 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 7012 | if (state == cricket::kIceGatheringGathering) { |
| 7013 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 7014 | } else if (state == cricket::kIceGatheringComplete) { |
| 7015 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 7016 | } |
| 7017 | } |
| 7018 | |
| 7019 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7020 | std::map<std::string, std::set<cricket::MediaType>> |
| 7021 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 7022 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7023 | if (transceiver->internal()->channel()) { |
| 7024 | const std::string& transport_name = |
| 7025 | transceiver->internal()->channel()->transport_name(); |
| 7026 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 7027 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7028 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7029 | } |
| 7030 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7031 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 7032 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7033 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7034 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 7035 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7036 | if (transport_name) { |
| 7037 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7038 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7039 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7040 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7041 | for (const auto& entry : media_types_by_transport_name) { |
| 7042 | const std::string& transport_name = entry.first; |
| 7043 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7044 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7045 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7046 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7047 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7048 | } |
| 7049 | } |
| 7050 | } |
| 7051 | // Walk through the ConnectionInfos to gather best connection usage |
| 7052 | // for IPv4 and IPv6. |
| 7053 | void PeerConnection::ReportBestConnectionState( |
| 7054 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7055 | for (const cricket::TransportChannelStats& channel_stats : |
| 7056 | stats.channel_stats) { |
| 7057 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 7058 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7059 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7060 | continue; |
| 7061 | } |
| 7062 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7063 | const cricket::Candidate& local = connection_info.local_candidate; |
| 7064 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7065 | |
| 7066 | // Increment the counter for IceCandidatePairType. |
| 7067 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 7068 | (local.type() == RELAY_PORT_TYPE && |
| 7069 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7070 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 7071 | GetIceCandidatePairCounter(local, remote), |
| 7072 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7073 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7074 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 7075 | GetIceCandidatePairCounter(local, remote), |
| 7076 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7077 | } else { |
| 7078 | RTC_CHECK(0); |
| 7079 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7080 | |
| 7081 | // Increment the counter for IP type. |
| 7082 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7083 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7084 | kBestConnections_IPv4, |
| 7085 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7086 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7087 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7088 | kBestConnections_IPv6, |
| 7089 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7090 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 7091 | RTC_CHECK(!local.address().hostname().empty() && |
| 7092 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7093 | } |
| 7094 | |
| 7095 | return; |
| 7096 | } |
| 7097 | } |
| 7098 | } |
| 7099 | |
| 7100 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7101 | const cricket::TransportStats& stats, |
| 7102 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7103 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 7104 | return; |
| 7105 | } |
| 7106 | |
| 7107 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 7108 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 7109 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 7110 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7111 | return; |
| 7112 | } |
| 7113 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7114 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 7115 | for (cricket::MediaType media_type : media_types) { |
| 7116 | switch (media_type) { |
| 7117 | case cricket::MEDIA_TYPE_AUDIO: |
| 7118 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7119 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 7120 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7121 | break; |
| 7122 | case cricket::MEDIA_TYPE_VIDEO: |
| 7123 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7124 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 7125 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7126 | break; |
| 7127 | case cricket::MEDIA_TYPE_DATA: |
| 7128 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7129 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 7130 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7131 | break; |
| 7132 | default: |
| 7133 | RTC_NOTREACHED(); |
| 7134 | continue; |
| 7135 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7136 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 7137 | } |
| 7138 | |
| 7139 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7140 | for (cricket::MediaType media_type : media_types) { |
| 7141 | switch (media_type) { |
| 7142 | case cricket::MEDIA_TYPE_AUDIO: |
| 7143 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7144 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 7145 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7146 | break; |
| 7147 | case cricket::MEDIA_TYPE_VIDEO: |
| 7148 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7149 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 7150 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7151 | break; |
| 7152 | case cricket::MEDIA_TYPE_DATA: |
| 7153 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7154 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 7155 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7156 | break; |
| 7157 | default: |
| 7158 | RTC_NOTREACHED(); |
| 7159 | continue; |
| 7160 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 7161 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7162 | } |
| 7163 | } |
| 7164 | |
| 7165 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 7166 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7167 | RTC_DCHECK(call_); |
| 7168 | call_->OnSentPacket(sent_packet); |
| 7169 | } |
| 7170 | |
| 7171 | const std::string PeerConnection::GetTransportName( |
| 7172 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7173 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7174 | if (channel) { |
| 7175 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7176 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7177 | if (data_channel_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7178 | RTC_DCHECK(sctp_mid_); |
| 7179 | if (content_name == *sctp_mid_) { |
| 7180 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7181 | } |
| 7182 | } |
| 7183 | // Return an empty string if failed to retrieve the transport name. |
| 7184 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7185 | } |
| 7186 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7187 | void PeerConnection::DestroyTransceiverChannel( |
| 7188 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 7189 | transceiver) { |
| 7190 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7191 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7192 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7193 | if (channel) { |
| 7194 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7195 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7196 | } |
| 7197 | } |
| 7198 | |
| 7199 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7200 | if (rtp_data_channel_) { |
| 7201 | OnDataChannelDestroyed(); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7202 | DestroyChannelInterface(rtp_data_channel_); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7203 | rtp_data_channel_ = nullptr; |
| 7204 | } |
| 7205 | |
| 7206 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 7207 | // grab a reference to this PeerConnection. If this is called from the |
| 7208 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 7209 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 7210 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 7211 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7212 | if (sctp_mid_) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7213 | OnDataChannelDestroyed(); |
| 7214 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 7215 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7216 | TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 7217 | }); |
| 7218 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7219 | } |
| 7220 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 7221 | void PeerConnection::DestroyChannelInterface( |
| 7222 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7223 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7224 | switch (channel->media_type()) { |
| 7225 | case cricket::MEDIA_TYPE_AUDIO: |
| 7226 | channel_manager()->DestroyVoiceChannel( |
| 7227 | static_cast<cricket::VoiceChannel*>(channel)); |
| 7228 | break; |
| 7229 | case cricket::MEDIA_TYPE_VIDEO: |
| 7230 | channel_manager()->DestroyVideoChannel( |
| 7231 | static_cast<cricket::VideoChannel*>(channel)); |
| 7232 | break; |
| 7233 | case cricket::MEDIA_TYPE_DATA: |
| 7234 | channel_manager()->DestroyRtpDataChannel( |
| 7235 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 7236 | break; |
| 7237 | default: |
| 7238 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 7239 | break; |
| 7240 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7241 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 7242 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7243 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7244 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7245 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 7246 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7247 | MediaTransportInterface* media_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7248 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 13:08:37 +0100 | [diff] [blame] | 7249 | RTC_DCHECK_RUN_ON(network_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 7250 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7251 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7252 | auto base_channel = GetChannel(mid); |
| 7253 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7254 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 7255 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 7256 | |
Karl Wiberg | 5966c50 | 2019-02-21 23:55:09 +0100 | [diff] [blame] | 7257 | if (use_media_transport_) { |
Tommi | 78a7138 | 2019-08-08 12:27:53 +0200 | [diff] [blame] | 7258 | RTC_LOG(LS_ERROR) << "Media transport isn't supported."; |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 7259 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 7260 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 7261 | if (data_channel_transport_ && mid == sctp_mid_ && |
| 7262 | data_channel_transport_ != data_channel_transport) { |
| 7263 | // Changed which data channel transport is used for |sctp_mid_| (eg. now |
| 7264 | // it's bundled). |
| 7265 | data_channel_transport_->SetDataSink(nullptr); |
| 7266 | data_channel_transport_ = data_channel_transport; |
| 7267 | if (data_channel_transport) { |
| 7268 | data_channel_transport->SetDataSink(this); |
| 7269 | |
| 7270 | // There's a new data channel transport. This needs to be signaled to the |
| 7271 | // |sctp_data_channels_| so that they can reopen and reconnect. This is |
| 7272 | // necessary when bundling is applied. |
| 7273 | data_channel_transport_invoker_->AsyncInvoke<void>( |
| 7274 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 7275 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7276 | for (auto channel : sctp_data_channels_) { |
| 7277 | channel->OnTransportChannelCreated(); |
| 7278 | } |
| 7279 | }); |
Bjorn A Mellem | b689af4 | 2019-08-21 10:44:59 -0700 | [diff] [blame] | 7280 | } |
| 7281 | } |
| 7282 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 7283 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 7284 | } |
| 7285 | |
Guido Urdaneta | 1ff16c8 | 2019-05-20 19:31:53 +0200 | [diff] [blame] | 7286 | void PeerConnection::OnSetStreams() { |
| 7287 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7288 | if (IsUnifiedPlan()) |
| 7289 | UpdateNegotiationNeeded(); |
| 7290 | } |
| 7291 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7292 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 7293 | // In earlier production code, the pointer was not cleared on close, |
| 7294 | // which might have led to undefined behavior if the observer was not |
| 7295 | // deallocated, or strange crashes if it was. |
| 7296 | // We use CHECK in order to catch such behavior if it exists. |
| 7297 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 7298 | RTC_CHECK(observer_); |
| 7299 | return observer_; |
| 7300 | } |
| 7301 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 7302 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 7303 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 7304 | // after it has been removed. |
| 7305 | return configuration_.crypto_options.has_value() |
| 7306 | ? *configuration_.crypto_options |
| 7307 | : factory_->options().crypto_options; |
| 7308 | } |
| 7309 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 7310 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 09:57:01 +0100 | [diff] [blame] | 7311 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 7312 | if (stats_collector_) { |
| 7313 | stats_collector_->ClearCachedStatsReport(); |
| 7314 | } |
| 7315 | } |
| 7316 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7317 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 7318 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 7319 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 7320 | } |
| 7321 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7322 | void PeerConnection::UpdateNegotiationNeeded() { |
| 7323 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7324 | if (!IsUnifiedPlan()) { |
| 7325 | Observer()->OnRenegotiationNeeded(); |
| 7326 | return; |
| 7327 | } |
| 7328 | |
| 7329 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7330 | if (IsClosed()) |
| 7331 | return; |
| 7332 | |
| 7333 | // If connection's signaling state is not "stable", abort these steps. |
| 7334 | if (signaling_state() != kStable) |
| 7335 | return; |
| 7336 | |
| 7337 | // NOTE |
| 7338 | // The negotiation-needed flag will be updated once the state transitions to |
| 7339 | // "stable", as part of the steps for setting an RTCSessionDescription. |
| 7340 | |
| 7341 | // If the result of checking if negotiation is needed is false, clear the |
| 7342 | // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot |
| 7343 | // to false, and abort these steps. |
| 7344 | bool is_negotiation_needed = CheckIfNegotiationIsNeeded(); |
| 7345 | if (!is_negotiation_needed) { |
| 7346 | is_negotiation_needed_ = false; |
| 7347 | return; |
| 7348 | } |
| 7349 | |
| 7350 | // If connection's [[NegotiationNeeded]] slot is already true, abort these |
| 7351 | // steps. |
| 7352 | if (is_negotiation_needed_) |
| 7353 | return; |
| 7354 | |
| 7355 | // Set connection's [[NegotiationNeeded]] slot to true. |
| 7356 | is_negotiation_needed_ = true; |
| 7357 | |
| 7358 | // Queue a task that runs the following steps: |
| 7359 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7360 | // If connection's [[NegotiationNeeded]] slot is false, abort these steps. |
| 7361 | // Fire an event named negotiationneeded at connection. |
| 7362 | Observer()->OnRenegotiationNeeded(); |
| 7363 | } |
| 7364 | |
| 7365 | bool PeerConnection::CheckIfNegotiationIsNeeded() { |
| 7366 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7367 | // 1. If any implementation-specific negotiation is required, as described at |
| 7368 | // the start of this section, return true. |
| 7369 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7370 | // 2. If connection's [[RestartIce]] internal slot is true, return true. |
| 7371 | if (local_ice_credentials_to_replace_->HasIceCredentials()) { |
| 7372 | return true; |
| 7373 | } |
| 7374 | |
| 7375 | // 3. Let description be connection.[[CurrentLocalDescription]]. |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7376 | const SessionDescriptionInterface* description = current_local_description(); |
| 7377 | if (!description) |
| 7378 | return true; |
| 7379 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7380 | // 4. If connection has created any RTCDataChannels, and no m= section in |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7381 | // description has been negotiated yet for data, return true. |
| 7382 | if (!sctp_data_channels_.empty()) { |
| 7383 | if (!cricket::GetFirstDataContent(description->description()->contents())) |
| 7384 | return true; |
| 7385 | } |
| 7386 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7387 | // 5. For each transceiver in connection's set of transceivers, perform the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7388 | // following checks: |
| 7389 | for (const auto& transceiver : transceivers_) { |
| 7390 | const ContentInfo* current_local_msection = |
| 7391 | FindTransceiverMSection(transceiver.get(), description); |
| 7392 | |
| 7393 | const ContentInfo* current_remote_msection = FindTransceiverMSection( |
| 7394 | transceiver.get(), current_remote_description()); |
| 7395 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7396 | // 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] | 7397 | // but the associated m= section is not yet rejected in |
| 7398 | // connection.[[CurrentLocalDescription]] or |
| 7399 | // connection.[[CurrentRemoteDescription]], return true. |
| 7400 | if (transceiver->stopped()) { |
| 7401 | if (current_local_msection && !current_local_msection->rejected && |
| 7402 | ((current_remote_msection && !current_remote_msection->rejected) || |
| 7403 | !current_remote_msection)) { |
| 7404 | return true; |
| 7405 | } |
| 7406 | continue; |
| 7407 | } |
| 7408 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7409 | // 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] | 7410 | // section in description, return true. |
| 7411 | if (!current_local_msection) |
| 7412 | return true; |
| 7413 | |
| 7414 | const MediaContentDescription* current_local_media_description = |
| 7415 | current_local_msection->media_description(); |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7416 | // 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] | 7417 | // in description then perform the following checks: |
| 7418 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7419 | // 5.2.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the |
Guido Urdaneta | 70c2db1 | 2019-04-16 12:24:14 +0200 | [diff] [blame] | 7420 | // associated m= section in description either doesn't contain a single |
| 7421 | // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this |
| 7422 | // m= section, or the MSID values themselves, differ from what is in |
| 7423 | // transceiver.sender.[[AssociatedMediaStreamIds]], return true. |
| 7424 | if (RtpTransceiverDirectionHasSend(transceiver->direction())) { |
| 7425 | if (current_local_media_description->streams().size() == 0) |
| 7426 | return true; |
| 7427 | |
| 7428 | std::vector<std::string> msection_msids; |
| 7429 | for (const auto& stream : current_local_media_description->streams()) { |
| 7430 | for (const std::string& msid : stream.stream_ids()) |
| 7431 | msection_msids.push_back(msid); |
| 7432 | } |
| 7433 | |
| 7434 | std::vector<std::string> transceiver_msids = |
| 7435 | transceiver->sender()->stream_ids(); |
| 7436 | if (msection_msids.size() != transceiver_msids.size()) |
| 7437 | return true; |
| 7438 | |
| 7439 | absl::c_sort(transceiver_msids); |
| 7440 | absl::c_sort(msection_msids); |
| 7441 | if (transceiver_msids != msection_msids) |
| 7442 | return true; |
| 7443 | } |
| 7444 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7445 | // 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] | 7446 | // associated m= section in neither connection.[[CurrentLocalDescription]] |
| 7447 | // nor connection.[[CurrentRemoteDescription]] matches |
| 7448 | // transceiver.[[Direction]], return true. |
| 7449 | if (description->GetType() == SdpType::kOffer) { |
| 7450 | if (!current_remote_description()) |
| 7451 | return true; |
| 7452 | |
| 7453 | if (!current_remote_msection) |
| 7454 | return true; |
| 7455 | |
| 7456 | RtpTransceiverDirection current_local_direction = |
| 7457 | current_local_media_description->direction(); |
| 7458 | RtpTransceiverDirection current_remote_direction = |
| 7459 | current_remote_msection->media_description()->direction(); |
| 7460 | if (transceiver->direction() != current_local_direction && |
| 7461 | transceiver->direction() != |
| 7462 | RtpTransceiverDirectionReversed(current_remote_direction)) { |
| 7463 | return true; |
| 7464 | } |
| 7465 | } |
| 7466 | |
Henrik Boström | 79b6980 | 2019-07-18 11:16:56 +0200 | [diff] [blame] | 7467 | // 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] | 7468 | // associated m= section in the description does not match |
| 7469 | // transceiver.[[Direction]] intersected with the offered direction (as |
| 7470 | // described in [JSEP] (section 5.3.1.)), return true. |
| 7471 | if (description->GetType() == SdpType::kAnswer) { |
| 7472 | if (!remote_description()) |
| 7473 | return true; |
| 7474 | |
| 7475 | const ContentInfo* offered_remote_msection = |
| 7476 | FindTransceiverMSection(transceiver.get(), remote_description()); |
| 7477 | |
| 7478 | RtpTransceiverDirection offered_direction = |
| 7479 | offered_remote_msection |
| 7480 | ? offered_remote_msection->media_description()->direction() |
| 7481 | : RtpTransceiverDirection::kInactive; |
| 7482 | |
| 7483 | if (current_local_media_description->direction() != |
| 7484 | (RtpTransceiverDirectionIntersection( |
| 7485 | transceiver->direction(), |
| 7486 | RtpTransceiverDirectionReversed(offered_direction)))) { |
| 7487 | return true; |
| 7488 | } |
| 7489 | } |
| 7490 | } |
| 7491 | |
| 7492 | // If all the preceding checks were performed and true was not returned, |
| 7493 | // nothing remains to be negotiated; return false. |
| 7494 | return false; |
| 7495 | } |
| 7496 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 7497 | } // namespace webrtc |