henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/peer_connection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | #include <stddef.h> |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 15 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 16 | #include <algorithm> |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 17 | #include <memory> |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 18 | #include <set> |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 19 | #include <string> |
Harald Alvestrand | 3eaee6b | 2020-10-19 06:35:55 | [diff] [blame] | 20 | #include <utility> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 21 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 22 | #include "absl/algorithm/container.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 20:02:22 | [diff] [blame] | 23 | #include "absl/strings/match.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 24 | #include "absl/strings/string_view.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 25 | #include "api/jsep_ice_candidate.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 26 | #include "api/rtp_parameters.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 27 | #include "api/rtp_transceiver_direction.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "api/uma_metrics.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 29 | #include "api/video/video_codec_constants.h" |
| 30 | #include "call/audio_state.h" |
| 31 | #include "call/packet_receiver.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 32 | #include "media/base/media_channel.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 33 | #include "media/base/media_config.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 34 | #include "media/base/media_engine.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 35 | #include "media/base/rid_description.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 36 | #include "media/base/stream_params.h" |
| 37 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 38 | #include "p2p/base/basic_async_resolver_factory.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 39 | #include "p2p/base/connection.h" |
| 40 | #include "p2p/base/connection_info.h" |
| 41 | #include "p2p/base/dtls_transport_internal.h" |
| 42 | #include "p2p/base/p2p_constants.h" |
| 43 | #include "p2p/base/p2p_transport_channel.h" |
| 44 | #include "p2p/base/transport_info.h" |
Harald Alvestrand | 9e334b7 | 2022-05-04 13:38:31 | [diff] [blame] | 45 | #include "pc/channel_manager.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 46 | #include "pc/ice_server_parsing.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 47 | #include "pc/rtp_receiver.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 48 | #include "pc/rtp_receiver_proxy.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 49 | #include "pc/rtp_sender.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 50 | #include "pc/rtp_sender_proxy.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 51 | #include "pc/sctp_transport.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 52 | #include "pc/simulcast_description.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 53 | #include "pc/webrtc_session_description_factory.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 54 | #include "rtc_base/helpers.h" |
| 55 | #include "rtc_base/ip_address.h" |
| 56 | #include "rtc_base/location.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 57 | #include "rtc_base/logging.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 58 | #include "rtc_base/net_helper.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 59 | #include "rtc_base/network.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 60 | #include "rtc_base/network_constants.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 61 | #include "rtc_base/ref_counted_object.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 62 | #include "rtc_base/socket_address.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 63 | #include "rtc_base/string_encode.h" |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 64 | #include "rtc_base/task_utils/to_queued_task.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 65 | #include "rtc_base/trace_event.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 66 | #include "rtc_base/unique_id_generator.h" |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 67 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 68 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 69 | using cricket::ContentInfo; |
| 70 | using cricket::ContentInfos; |
| 71 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 72 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 73 | using cricket::RidDescription; |
| 74 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 75 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 76 | using cricket::SimulcastDescription; |
| 77 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 78 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 79 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 80 | using cricket::TransportInfo; |
| 81 | |
| 82 | using cricket::LOCAL_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 83 | using cricket::PRFLX_PORT_TYPE; |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 84 | using cricket::RELAY_PORT_TYPE; |
| 85 | using cricket::STUN_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 86 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 87 | namespace webrtc { |
| 88 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 89 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 90 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 91 | // UMA metric names. |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 92 | const char kSimulcastNumberOfEncodings[] = |
| 93 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 94 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 95 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 96 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 97 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 98 | PeerConnectionInterface::IceTransportsType type) { |
| 99 | switch (type) { |
| 100 | case PeerConnectionInterface::kNone: |
| 101 | return cricket::CF_NONE; |
| 102 | case PeerConnectionInterface::kRelay: |
| 103 | return cricket::CF_RELAY; |
| 104 | case PeerConnectionInterface::kNoHost: |
| 105 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 106 | case PeerConnectionInterface::kAll: |
| 107 | return cricket::CF_ALL; |
| 108 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 109 | RTC_DCHECK_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 110 | } |
| 111 | return cricket::CF_NONE; |
| 112 | } |
| 113 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 114 | IceCandidatePairType GetIceCandidatePairCounter( |
| 115 | const cricket::Candidate& local, |
| 116 | const cricket::Candidate& remote) { |
| 117 | const auto& l = local.type(); |
| 118 | const auto& r = remote.type(); |
| 119 | const auto& host = LOCAL_PORT_TYPE; |
| 120 | const auto& srflx = STUN_PORT_TYPE; |
| 121 | const auto& relay = RELAY_PORT_TYPE; |
| 122 | const auto& prflx = PRFLX_PORT_TYPE; |
| 123 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 124 | bool local_hostname = |
| 125 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 126 | bool remote_hostname = !remote.address().hostname().empty() && |
| 127 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 128 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 129 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 130 | if (local_hostname) { |
| 131 | if (remote_hostname) { |
| 132 | return kIceCandidatePairHostNameHostName; |
| 133 | } else if (remote_private) { |
| 134 | return kIceCandidatePairHostNameHostPrivate; |
| 135 | } else { |
| 136 | return kIceCandidatePairHostNameHostPublic; |
| 137 | } |
| 138 | } else if (local_private) { |
| 139 | if (remote_hostname) { |
| 140 | return kIceCandidatePairHostPrivateHostName; |
| 141 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 142 | return kIceCandidatePairHostPrivateHostPrivate; |
| 143 | } else { |
| 144 | return kIceCandidatePairHostPrivateHostPublic; |
| 145 | } |
| 146 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 147 | if (remote_hostname) { |
| 148 | return kIceCandidatePairHostPublicHostName; |
| 149 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 150 | return kIceCandidatePairHostPublicHostPrivate; |
| 151 | } else { |
| 152 | return kIceCandidatePairHostPublicHostPublic; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | if (l == host && r == srflx) |
| 157 | return kIceCandidatePairHostSrflx; |
| 158 | if (l == host && r == relay) |
| 159 | return kIceCandidatePairHostRelay; |
| 160 | if (l == host && r == prflx) |
| 161 | return kIceCandidatePairHostPrflx; |
| 162 | if (l == srflx && r == host) |
| 163 | return kIceCandidatePairSrflxHost; |
| 164 | if (l == srflx && r == srflx) |
| 165 | return kIceCandidatePairSrflxSrflx; |
| 166 | if (l == srflx && r == relay) |
| 167 | return kIceCandidatePairSrflxRelay; |
| 168 | if (l == srflx && r == prflx) |
| 169 | return kIceCandidatePairSrflxPrflx; |
| 170 | if (l == relay && r == host) |
| 171 | return kIceCandidatePairRelayHost; |
| 172 | if (l == relay && r == srflx) |
| 173 | return kIceCandidatePairRelaySrflx; |
| 174 | if (l == relay && r == relay) |
| 175 | return kIceCandidatePairRelayRelay; |
| 176 | if (l == relay && r == prflx) |
| 177 | return kIceCandidatePairRelayPrflx; |
| 178 | if (l == prflx && r == host) |
| 179 | return kIceCandidatePairPrflxHost; |
| 180 | if (l == prflx && r == srflx) |
| 181 | return kIceCandidatePairPrflxSrflx; |
| 182 | if (l == prflx && r == relay) |
| 183 | return kIceCandidatePairPrflxRelay; |
| 184 | return kIceCandidatePairMax; |
| 185 | } |
| 186 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 187 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 188 | int rtc_configuration_parameter) { |
| 189 | if (rtc_configuration_parameter == |
| 190 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 191 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 192 | } |
| 193 | return rtc_configuration_parameter; |
| 194 | } |
| 195 | |
Jonas Oreland | e309651 | 2020-05-27 07:01:05 | [diff] [blame] | 196 | // Check if the changes of IceTransportsType motives an ice restart. |
| 197 | bool NeedIceRestart(bool surface_ice_candidates_on_ice_transport_type_changed, |
| 198 | PeerConnectionInterface::IceTransportsType current, |
| 199 | PeerConnectionInterface::IceTransportsType modified) { |
| 200 | if (current == modified) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | if (!surface_ice_candidates_on_ice_transport_type_changed) { |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | auto current_filter = ConvertIceTransportTypeToCandidateFilter(current); |
| 209 | auto modified_filter = ConvertIceTransportTypeToCandidateFilter(modified); |
| 210 | |
| 211 | // If surface_ice_candidates_on_ice_transport_type_changed is true and we |
| 212 | // extend the filter, then no ice restart is needed. |
| 213 | return (current_filter & modified_filter) != current_filter; |
| 214 | } |
| 215 | |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 216 | cricket::IceConfig ParseIceConfig( |
| 217 | const PeerConnectionInterface::RTCConfiguration& config) { |
| 218 | cricket::ContinualGatheringPolicy gathering_policy; |
| 219 | switch (config.continual_gathering_policy) { |
| 220 | case PeerConnectionInterface::GATHER_ONCE: |
| 221 | gathering_policy = cricket::GATHER_ONCE; |
| 222 | break; |
| 223 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 224 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 225 | break; |
| 226 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 227 | RTC_DCHECK_NOTREACHED(); |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 228 | gathering_policy = cricket::GATHER_ONCE; |
| 229 | } |
| 230 | |
| 231 | cricket::IceConfig ice_config; |
| 232 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 233 | config.ice_connection_receiving_timeout); |
| 234 | ice_config.prioritize_most_likely_candidate_pairs = |
| 235 | config.prioritize_most_likely_ice_candidate_pairs; |
| 236 | ice_config.backup_connection_ping_interval = |
| 237 | RTCConfigurationToIceConfigOptionalInt( |
| 238 | config.ice_backup_candidate_pair_ping_interval); |
| 239 | ice_config.continual_gathering_policy = gathering_policy; |
| 240 | ice_config.presume_writable_when_fully_relayed = |
| 241 | config.presume_writable_when_fully_relayed; |
| 242 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 243 | config.surface_ice_candidates_on_ice_transport_type_changed; |
| 244 | ice_config.ice_check_interval_strong_connectivity = |
| 245 | config.ice_check_interval_strong_connectivity; |
| 246 | ice_config.ice_check_interval_weak_connectivity = |
| 247 | config.ice_check_interval_weak_connectivity; |
| 248 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
| 249 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 250 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
| 251 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
| 252 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
| 253 | ice_config.network_preference = config.network_preference; |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 254 | ice_config.stable_writable_connection_ping_interval = |
| 255 | config.stable_writable_connection_ping_interval_ms; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 256 | return ice_config; |
| 257 | } |
| 258 | |
| 259 | // Ensures the configuration doesn't have any parameters with invalid values, |
| 260 | // or values that conflict with other parameters. |
| 261 | // |
| 262 | // Returns RTCError::OK() if there are no issues. |
| 263 | RTCError ValidateConfiguration( |
| 264 | const PeerConnectionInterface::RTCConfiguration& config) { |
| 265 | return cricket::P2PTransportChannel::ValidateIceConfig( |
| 266 | ParseIceConfig(config)); |
| 267 | } |
| 268 | |
| 269 | bool HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
| 270 | return content->media_description()->rtcp_mux(); |
| 271 | } |
| 272 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 273 | bool DtlsEnabled(const PeerConnectionInterface::RTCConfiguration& configuration, |
| 274 | const PeerConnectionFactoryInterface::Options& options, |
| 275 | const PeerConnectionDependencies& dependencies) { |
| 276 | if (options.disable_encryption) |
| 277 | return false; |
| 278 | |
| 279 | // Enable DTLS by default if we have an identity store or a certificate. |
| 280 | bool default_enabled = |
| 281 | (dependencies.cert_generator || !configuration.certificates.empty()); |
| 282 | |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 283 | #if defined(WEBRTC_FUCHSIA) |
Harald Alvestrand | 50b9552 | 2021-11-18 10:01:06 | [diff] [blame] | 284 | // The `configuration` can override the default value. |
| 285 | return configuration.enable_dtls_srtp.value_or(default_enabled); |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 286 | #else |
| 287 | return default_enabled; |
| 288 | #endif |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 289 | } |
| 290 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 291 | } // namespace |
| 292 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 293 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 294 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 295 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 296 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 297 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 | [diff] [blame] | 298 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 299 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 300 | BundlePolicy bundle_policy; |
| 301 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 302 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 303 | int ice_candidate_pool_size; |
| 304 | bool disable_ipv6; |
| 305 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 306 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 307 | bool disable_link_local_networks; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 308 | absl::optional<int> screencast_min_bitrate; |
| 309 | absl::optional<bool> combined_audio_video_bwe; |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 310 | #if defined(WEBRTC_FUCHSIA) |
Harald Alvestrand | 50b9552 | 2021-11-18 10:01:06 | [diff] [blame] | 311 | absl::optional<bool> enable_dtls_srtp; |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 312 | #endif |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 313 | TcpCandidatePolicy tcp_candidate_policy; |
| 314 | CandidateNetworkPolicy candidate_network_policy; |
| 315 | int audio_jitter_buffer_max_packets; |
| 316 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 317 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 318 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 319 | int ice_connection_receiving_timeout; |
| 320 | int ice_backup_candidate_pair_ping_interval; |
| 321 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 322 | bool prioritize_most_likely_ice_candidate_pairs; |
| 323 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 324 | bool prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 325 | PortPrunePolicy turn_port_prune_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 326 | bool presume_writable_when_fully_relayed; |
| 327 | bool enable_ice_renomination; |
| 328 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 329 | bool surface_ice_candidates_on_ice_transport_type_changed; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 330 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 331 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 332 | absl::optional<int> ice_check_min_interval; |
| 333 | absl::optional<int> ice_unwritable_timeout; |
| 334 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 335 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 336 | absl::optional<int> stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 337 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 338 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 339 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 340 | bool active_reset_srtp_params; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 341 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 342 | bool offer_extmap_allow_mixed; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 343 | std::string turn_logging_id; |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 344 | bool enable_implicit_rollback; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 345 | absl::optional<bool> allow_codec_switching; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 346 | absl::optional<int> report_usage_pattern_delay_ms; |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 347 | absl::optional<int> stable_writable_connection_ping_interval_ms; |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 348 | webrtc::VpnPreference vpn_preference; |
Jonas Oreland | 2ee0e64 | 2021-08-25 13:43:02 | [diff] [blame] | 349 | std::vector<rtc::NetworkMask> vpn_list; |
Niels Möller | 73d0774 | 2021-12-02 12:58:01 | [diff] [blame] | 350 | PortAllocatorConfig port_allocator_config; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 351 | }; |
| 352 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 353 | "Did you add something to RTCConfiguration and forget to " |
| 354 | "update operator==?"); |
| 355 | return type == o.type && servers == o.servers && |
| 356 | bundle_policy == o.bundle_policy && |
| 357 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 358 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 359 | candidate_network_policy == o.candidate_network_policy && |
| 360 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 361 | audio_jitter_buffer_fast_accelerate == |
| 362 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 363 | audio_jitter_buffer_min_delay_ms == |
| 364 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 365 | audio_jitter_buffer_enable_rtx_handling == |
| 366 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 367 | ice_connection_receiving_timeout == |
| 368 | o.ice_connection_receiving_timeout && |
| 369 | ice_backup_candidate_pair_ping_interval == |
| 370 | o.ice_backup_candidate_pair_ping_interval && |
| 371 | continual_gathering_policy == o.continual_gathering_policy && |
| 372 | certificates == o.certificates && |
| 373 | prioritize_most_likely_ice_candidate_pairs == |
| 374 | o.prioritize_most_likely_ice_candidate_pairs && |
| 375 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 376 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 377 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 378 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 379 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 380 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 381 | #if defined(WEBRTC_FUCHSIA) |
Harald Alvestrand | 50b9552 | 2021-11-18 10:01:06 | [diff] [blame] | 382 | enable_dtls_srtp == o.enable_dtls_srtp && |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 383 | #endif |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 384 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 385 | prune_turn_ports == o.prune_turn_ports && |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 386 | turn_port_prune_policy == o.turn_port_prune_policy && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 387 | presume_writable_when_fully_relayed == |
| 388 | o.presume_writable_when_fully_relayed && |
| 389 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 19:50:14 | [diff] [blame] | 390 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 391 | surface_ice_candidates_on_ice_transport_type_changed == |
| 392 | o.surface_ice_candidates_on_ice_transport_type_changed && |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 393 | ice_check_interval_strong_connectivity == |
| 394 | o.ice_check_interval_strong_connectivity && |
| 395 | ice_check_interval_weak_connectivity == |
| 396 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 397 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 398 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 399 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 400 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 401 | stun_candidate_keepalive_interval == |
| 402 | o.stun_candidate_keepalive_interval && |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 403 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 404 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 405 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 16:43:21 | [diff] [blame] | 406 | active_reset_srtp_params == o.active_reset_srtp_params && |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 407 | crypto_options == o.crypto_options && |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 408 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed && |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 409 | turn_logging_id == o.turn_logging_id && |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 410 | enable_implicit_rollback == o.enable_implicit_rollback && |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 411 | allow_codec_switching == o.allow_codec_switching && |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 412 | report_usage_pattern_delay_ms == o.report_usage_pattern_delay_ms && |
| 413 | stable_writable_connection_ping_interval_ms == |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 414 | o.stable_writable_connection_ping_interval_ms && |
Niels Möller | 73d0774 | 2021-12-02 12:58:01 | [diff] [blame] | 415 | vpn_preference == o.vpn_preference && vpn_list == o.vpn_list && |
| 416 | port_allocator_config.min_port == o.port_allocator_config.min_port && |
| 417 | port_allocator_config.max_port == o.port_allocator_config.max_port && |
| 418 | port_allocator_config.flags == o.port_allocator_config.flags; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 422 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 423 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 19:44:26 | [diff] [blame] | 424 | } |
| 425 | |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 426 | RTCErrorOr<rtc::scoped_refptr<PeerConnection>> PeerConnection::Create( |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 427 | rtc::scoped_refptr<ConnectionContext> context, |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 428 | const PeerConnectionFactoryInterface::Options& options, |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 429 | std::unique_ptr<RtcEventLog> event_log, |
| 430 | std::unique_ptr<Call> call, |
| 431 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 432 | PeerConnectionDependencies dependencies) { |
Henrik Boström | 62995db | 2022-01-03 08:58:10 | [diff] [blame] | 433 | // TODO(https://crbug.com/webrtc/13528): Remove support for kPlanB. |
Henrik Boström | 6d2fe89 | 2022-01-21 08:51:07 | [diff] [blame] | 434 | if (configuration.sdp_semantics == SdpSemantics::kPlanB_DEPRECATED) { |
| 435 | RTC_LOG(LS_WARNING) |
| 436 | << "PeerConnection constructed with legacy SDP semantics!"; |
| 437 | } |
Henrik Boström | 62995db | 2022-01-03 08:58:10 | [diff] [blame] | 438 | |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 439 | RTCError config_error = cricket::P2PTransportChannel::ValidateIceConfig( |
| 440 | ParseIceConfig(configuration)); |
| 441 | if (!config_error.ok()) { |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 442 | RTC_LOG(LS_ERROR) << "Invalid ICE configuration: " |
| 443 | << config_error.message(); |
| 444 | return config_error; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (!dependencies.allocator) { |
| 448 | RTC_LOG(LS_ERROR) |
| 449 | << "PeerConnection initialized without a PortAllocator? " |
| 450 | "This shouldn't happen if using PeerConnectionFactory."; |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 451 | return RTCError( |
| 452 | RTCErrorType::INVALID_PARAMETER, |
| 453 | "Attempt to create a PeerConnection without a PortAllocatorFactory"); |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | if (!dependencies.observer) { |
| 457 | // TODO(deadbeef): Why do we do this? |
| 458 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
| 459 | "PeerConnectionObserver"; |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 460 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 461 | "Attempt to create a PeerConnection without an observer"); |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | bool is_unified_plan = |
| 465 | configuration.sdp_semantics == SdpSemantics::kUnifiedPlan; |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 466 | bool dtls_enabled = DtlsEnabled(configuration, options, dependencies); |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 467 | |
| 468 | // Interim code: If an AsyncResolverFactory is given, but not an |
| 469 | // AsyncDnsResolverFactory, wrap it in a WrappingAsyncDnsResolverFactory |
| 470 | // If neither is given, create a WrappingAsyncDnsResolverFactory wrapping |
| 471 | // a BasicAsyncResolver. |
| 472 | // TODO(bugs.webrtc.org/12598): Remove code once all callers pass a |
| 473 | // AsyncDnsResolverFactory. |
| 474 | if (dependencies.async_dns_resolver_factory && |
| 475 | dependencies.async_resolver_factory) { |
| 476 | RTC_LOG(LS_ERROR) |
| 477 | << "Attempt to set both old and new type of DNS resolver factory"; |
| 478 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 479 | "Both old and new type of DNS resolver given"); |
| 480 | } |
| 481 | if (dependencies.async_resolver_factory) { |
| 482 | dependencies.async_dns_resolver_factory = |
| 483 | std::make_unique<WrappingAsyncDnsResolverFactory>( |
| 484 | std::move(dependencies.async_resolver_factory)); |
| 485 | } else { |
| 486 | dependencies.async_dns_resolver_factory = |
| 487 | std::make_unique<WrappingAsyncDnsResolverFactory>( |
| 488 | std::make_unique<BasicAsyncResolverFactory>()); |
| 489 | } |
| 490 | |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 491 | // The PeerConnection constructor consumes some, but not all, dependencies. |
Tommi | 87f7090 | 2021-04-27 12:43:08 | [diff] [blame] | 492 | auto pc = rtc::make_ref_counted<PeerConnection>( |
| 493 | context, options, is_unified_plan, std::move(event_log), std::move(call), |
| 494 | dependencies, dtls_enabled); |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 495 | RTCError init_error = pc->Initialize(configuration, std::move(dependencies)); |
| 496 | if (!init_error.ok()) { |
| 497 | RTC_LOG(LS_ERROR) << "PeerConnection initialization failed"; |
| 498 | return init_error; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 499 | } |
| 500 | return pc; |
| 501 | } |
| 502 | |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 503 | PeerConnection::PeerConnection( |
| 504 | rtc::scoped_refptr<ConnectionContext> context, |
| 505 | const PeerConnectionFactoryInterface::Options& options, |
| 506 | bool is_unified_plan, |
| 507 | std::unique_ptr<RtcEventLog> event_log, |
| 508 | std::unique_ptr<Call> call, |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 509 | PeerConnectionDependencies& dependencies, |
| 510 | bool dtls_enabled) |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 511 | : context_(context), |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 512 | trials_(std::move(dependencies.trials), &context->field_trials()), |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 513 | options_(options), |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 514 | observer_(dependencies.observer), |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 515 | is_unified_plan_(is_unified_plan), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 516 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 517 | event_log_ptr_(event_log_.get()), |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 518 | async_dns_resolver_factory_( |
| 519 | std::move(dependencies.async_dns_resolver_factory)), |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 520 | port_allocator_(std::move(dependencies.allocator)), |
| 521 | ice_transport_factory_(std::move(dependencies.ice_transport_factory)), |
| 522 | tls_cert_verifier_(std::move(dependencies.tls_cert_verifier)), |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 523 | call_(std::move(call)), |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 524 | call_ptr_(call_.get()), |
Tomas Gunnarsson | 97a387d | 2021-03-29 19:04:29 | [diff] [blame] | 525 | // RFC 3264: The numeric value of the session id and version in the |
| 526 | // o line MUST be representable with a "64 bit signed integer". |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 527 | // Due to this constraint session id `session_id_` is max limited to |
Tomas Gunnarsson | 97a387d | 2021-03-29 19:04:29 | [diff] [blame] | 528 | // LLONG_MAX. |
| 529 | session_id_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)), |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 530 | dtls_enabled_(dtls_enabled), |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 531 | data_channel_controller_(this), |
Lahiru Ginnaliya Gamathige | 70f9e24 | 2021-01-28 07:32:46 | [diff] [blame] | 532 | message_handler_(signaling_thread()), |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 533 | weak_factory_(this) { |
| 534 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 535 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 536 | worker_thread_safety_ = PendingTaskSafetyFlag::Create(); |
| 537 | if (!call_) |
| 538 | worker_thread_safety_->SetNotAlive(); |
| 539 | }); |
| 540 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 541 | |
| 542 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 543 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 544 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 545 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 546 | if (sdp_handler_) { |
| 547 | sdp_handler_->PrepareForShutdown(); |
| 548 | } |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 549 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 550 | // Need to stop transceivers before destroying the stats collector because |
| 551 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 552 | // stopping. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 553 | if (rtp_manager()) { |
| 554 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 555 | transceiver->StopInternal(); |
| 556 | } |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 557 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 558 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 559 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 13:06:57 | [diff] [blame] | 560 | if (stats_collector_) { |
| 561 | stats_collector_->WaitForPendingRequest(); |
| 562 | stats_collector_ = nullptr; |
| 563 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 564 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 565 | if (sdp_handler_) { |
| 566 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 567 | // the last stats request can still read from the channels. |
| 568 | sdp_handler_->DestroyAllChannels(); |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 569 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 570 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 571 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 572 | sdp_handler_->ResetSessionDescFactory(); |
| 573 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 574 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 575 | // port_allocator_ and transport_controller_ live on the network thread and |
| 576 | // should be destroyed there. |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 577 | transport_controller_copy_ = nullptr; |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 578 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 579 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 580 | TeardownDataChannelTransport_n(); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 581 | transport_controller_.reset(); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 582 | port_allocator_.reset(); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 583 | if (network_thread_safety_) |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 584 | network_thread_safety_->SetNotAlive(); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 585 | }); |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 586 | |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 587 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 588 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 589 | RTC_DCHECK_RUN_ON(worker_thread()); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 590 | worker_thread_safety_->SetNotAlive(); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 591 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 592 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 593 | event_log_.reset(); |
| 594 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 595 | } |
| 596 | |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 597 | RTCError PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 598 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 599 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 600 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 601 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 602 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 603 | cricket::ServerAddresses stun_servers; |
| 604 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 605 | |
| 606 | RTCErrorType parse_error = |
| 607 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 608 | if (parse_error != RTCErrorType::NONE) { |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 609 | return RTCError(parse_error, "ICE server parse failed"); |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 610 | } |
| 611 | |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 612 | // Add the turn logging id to all turn servers |
| 613 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 614 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 615 | } |
| 616 | |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 617 | // Note if STUN or TURN servers were supplied. |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 618 | if (!stun_servers.empty()) { |
| 619 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 620 | } |
| 621 | if (!turn_servers.empty()) { |
| 622 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 623 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 624 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 625 | // Network thread initialization. |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 626 | transport_controller_copy_ = |
| 627 | network_thread()->Invoke<JsepTransportController*>(RTC_FROM_HERE, [&] { |
| 628 | RTC_DCHECK_RUN_ON(network_thread()); |
| 629 | network_thread_safety_ = PendingTaskSafetyFlag::Create(); |
| 630 | InitializePortAllocatorResult pa_result = InitializePortAllocator_n( |
| 631 | stun_servers, turn_servers, configuration); |
| 632 | // Send information about IPv4/IPv6 status. |
| 633 | PeerConnectionAddressFamilyCounter address_family = |
| 634 | pa_result.enable_ipv6 ? kPeerConnection_IPv6 : kPeerConnection_IPv4; |
| 635 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 636 | address_family, |
| 637 | kPeerConnectionAddressFamilyCounter_Max); |
| 638 | return InitializeTransportController_n(configuration, dependencies); |
| 639 | }); |
Lahiru Ginnaliya Gamathige | e99c68d | 2020-09-30 21:33:45 | [diff] [blame] | 640 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 641 | configuration_ = configuration; |
| 642 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 643 | stats_ = std::make_unique<StatsCollector>(this); |
| 644 | stats_collector_ = RTCStatsCollector::Create(this); |
| 645 | |
Harald Alvestrand | 66c4036 | 2022-01-28 17:41:30 | [diff] [blame] | 646 | sdp_handler_ = SdpOfferAnswerHandler::Create(this, configuration, |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 647 | dependencies, context_.get()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 648 | |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 649 | rtp_manager_ = std::make_unique<RtpTransmissionManager>( |
| 650 | IsUnifiedPlan(), signaling_thread(), worker_thread(), channel_manager(), |
| 651 | &usage_pattern_, observer_, stats_.get(), [this]() { |
| 652 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 653 | sdp_handler_->UpdateNegotiationNeeded(); |
| 654 | }); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 655 | |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 656 | // Add default audio/video transceivers for Plan B SDP. |
| 657 | if (!IsUnifiedPlan()) { |
| 658 | rtp_manager()->transceivers()->Add( |
| 659 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 660 | signaling_thread(), |
| 661 | rtc::make_ref_counted<RtpTransceiver>(cricket::MEDIA_TYPE_AUDIO, |
| 662 | channel_manager()))); |
| 663 | rtp_manager()->transceivers()->Add( |
| 664 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 665 | signaling_thread(), |
| 666 | rtc::make_ref_counted<RtpTransceiver>(cricket::MEDIA_TYPE_VIDEO, |
| 667 | channel_manager()))); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 668 | } |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 669 | |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 670 | int delay_ms = configuration.report_usage_pattern_delay_ms |
| 671 | ? *configuration.report_usage_pattern_delay_ms |
| 672 | : REPORT_USAGE_PATTERN_DELAY_MS; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 673 | message_handler_.RequestUsagePatternReport( |
| 674 | [this]() { |
| 675 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 676 | ReportUsagePattern(); |
| 677 | }, |
| 678 | delay_ms); |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 679 | |
Philipp Hancke | f59d9fb | 2021-10-13 09:34:15 | [diff] [blame] | 680 | // Record the number of configured ICE servers for all connections. |
| 681 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.PeerConnection.IceServers.Configured", |
| 682 | configuration_.servers.size(), 0, 31, 32); |
| 683 | |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 684 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 685 | } |
| 686 | |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 687 | JsepTransportController* PeerConnection::InitializeTransportController_n( |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 688 | const RTCConfiguration& configuration, |
| 689 | const PeerConnectionDependencies& dependencies) { |
| 690 | JsepTransportController::Config config; |
| 691 | config.redetermine_role_on_ice_restart = |
| 692 | configuration.redetermine_role_on_ice_restart; |
| 693 | config.ssl_max_version = options_.ssl_max_version; |
| 694 | config.disable_encryption = options_.disable_encryption; |
| 695 | config.bundle_policy = configuration.bundle_policy; |
| 696 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
| 697 | // TODO(bugs.webrtc.org/9891) - Remove options_.crypto_options then remove |
| 698 | // this stub. |
| 699 | config.crypto_options = configuration.crypto_options.has_value() |
| 700 | ? *configuration.crypto_options |
| 701 | : options_.crypto_options; |
| 702 | config.transport_observer = this; |
| 703 | config.rtcp_handler = InitializeRtcpCallback(); |
| 704 | config.event_log = event_log_ptr_; |
| 705 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 706 | config.enable_external_auth = true; |
| 707 | #endif |
| 708 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
| 709 | |
| 710 | // DTLS has to be enabled to use SCTP. |
Florent Castelli | 516e284 | 2021-04-19 13:29:50 | [diff] [blame] | 711 | if (dtls_enabled_) { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 712 | config.sctp_factory = context_->sctp_transport_factory(); |
| 713 | } |
| 714 | |
| 715 | config.ice_transport_factory = ice_transport_factory_.get(); |
| 716 | config.on_dtls_handshake_error_ = |
| 717 | [weak_ptr = weak_factory_.GetWeakPtr()](rtc::SSLHandshakeError s) { |
| 718 | if (weak_ptr) { |
| 719 | weak_ptr->OnTransportControllerDtlsHandshakeError(s); |
| 720 | } |
| 721 | }; |
| 722 | |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 723 | config.field_trials = trials_.get(); |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 724 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 725 | transport_controller_.reset( |
| 726 | new JsepTransportController(network_thread(), port_allocator_.get(), |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 727 | async_dns_resolver_factory_.get(), config)); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 728 | |
| 729 | transport_controller_->SubscribeIceConnectionState( |
| 730 | [this](cricket::IceConnectionState s) { |
| 731 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 732 | if (s == cricket::kIceConnectionConnected) { |
| 733 | ReportTransportStats(); |
| 734 | } |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 735 | signaling_thread()->PostTask( |
| 736 | ToQueuedTask(signaling_thread_safety_.flag(), [this, s]() { |
| 737 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 738 | OnTransportControllerConnectionState(s); |
| 739 | })); |
| 740 | }); |
| 741 | transport_controller_->SubscribeConnectionState( |
| 742 | [this](PeerConnectionInterface::PeerConnectionState s) { |
| 743 | RTC_DCHECK_RUN_ON(network_thread()); |
| 744 | signaling_thread()->PostTask( |
| 745 | ToQueuedTask(signaling_thread_safety_.flag(), [this, s]() { |
| 746 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 747 | SetConnectionState(s); |
| 748 | })); |
| 749 | }); |
| 750 | transport_controller_->SubscribeStandardizedIceConnectionState( |
| 751 | [this](PeerConnectionInterface::IceConnectionState s) { |
| 752 | RTC_DCHECK_RUN_ON(network_thread()); |
| 753 | signaling_thread()->PostTask( |
| 754 | ToQueuedTask(signaling_thread_safety_.flag(), [this, s]() { |
| 755 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 756 | SetStandardizedIceConnectionState(s); |
| 757 | })); |
| 758 | }); |
| 759 | transport_controller_->SubscribeIceGatheringState( |
| 760 | [this](cricket::IceGatheringState s) { |
| 761 | RTC_DCHECK_RUN_ON(network_thread()); |
| 762 | signaling_thread()->PostTask( |
| 763 | ToQueuedTask(signaling_thread_safety_.flag(), [this, s]() { |
| 764 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 765 | OnTransportControllerGatheringState(s); |
| 766 | })); |
| 767 | }); |
| 768 | transport_controller_->SubscribeIceCandidateGathered( |
| 769 | [this](const std::string& transport, |
| 770 | const std::vector<cricket::Candidate>& candidates) { |
| 771 | RTC_DCHECK_RUN_ON(network_thread()); |
| 772 | signaling_thread()->PostTask( |
| 773 | ToQueuedTask(signaling_thread_safety_.flag(), |
| 774 | [this, t = transport, c = candidates]() { |
| 775 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 776 | OnTransportControllerCandidatesGathered(t, c); |
| 777 | })); |
| 778 | }); |
| 779 | transport_controller_->SubscribeIceCandidateError( |
| 780 | [this](const cricket::IceCandidateErrorEvent& event) { |
| 781 | RTC_DCHECK_RUN_ON(network_thread()); |
| 782 | signaling_thread()->PostTask(ToQueuedTask( |
| 783 | signaling_thread_safety_.flag(), [this, event = event]() { |
| 784 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 785 | OnTransportControllerCandidateError(event); |
| 786 | })); |
| 787 | }); |
| 788 | transport_controller_->SubscribeIceCandidatesRemoved( |
| 789 | [this](const std::vector<cricket::Candidate>& c) { |
| 790 | RTC_DCHECK_RUN_ON(network_thread()); |
| 791 | signaling_thread()->PostTask( |
| 792 | ToQueuedTask(signaling_thread_safety_.flag(), [this, c = c]() { |
| 793 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 794 | OnTransportControllerCandidatesRemoved(c); |
| 795 | })); |
| 796 | }); |
| 797 | transport_controller_->SubscribeIceCandidatePairChanged( |
| 798 | [this](const cricket::CandidatePairChangeEvent& event) { |
| 799 | RTC_DCHECK_RUN_ON(network_thread()); |
| 800 | signaling_thread()->PostTask(ToQueuedTask( |
| 801 | signaling_thread_safety_.flag(), [this, event = event]() { |
| 802 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 803 | OnTransportControllerCandidateChanged(event); |
| 804 | })); |
| 805 | }); |
| 806 | |
| 807 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 808 | return transport_controller_.get(); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 809 | } |
| 810 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 811 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 812 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 813 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 814 | "Plan SdpSemantics. Please use GetSenders " |
| 815 | "instead."; |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 816 | return sdp_handler_->local_streams(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 817 | } |
| 818 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 819 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 820 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 821 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 822 | "Plan SdpSemantics. Please use GetReceivers " |
| 823 | "instead."; |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 824 | return sdp_handler_->remote_streams(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 825 | } |
| 826 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 | [diff] [blame] | 827 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 828 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 829 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 830 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 831 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 832 | if (!ConfiguredForMedia()) { |
| 833 | RTC_LOG(LS_ERROR) << "AddStream: Not configured for media"; |
| 834 | return false; |
| 835 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 836 | return sdp_handler_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 840 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 841 | RTC_DCHECK(ConfiguredForMedia()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 842 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 843 | "Plan SdpSemantics. Please use RemoveTrack " |
| 844 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 845 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 846 | sdp_handler_->RemoveStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 847 | } |
| 848 | |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 849 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 850 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 851 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 852 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 853 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 854 | if (!ConfiguredForMedia()) { |
| 855 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 856 | "Not configured for media"); |
| 857 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 858 | if (!track) { |
| 859 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 860 | } |
| 861 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 862 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 863 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 864 | "Track has invalid kind: " + track->kind()); |
| 865 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 866 | if (IsClosed()) { |
| 867 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 868 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 869 | } |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 870 | if (rtp_manager()->FindSenderForTrack(track.get())) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 871 | LOG_AND_RETURN_ERROR( |
| 872 | RTCErrorType::INVALID_PARAMETER, |
| 873 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 874 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 875 | auto sender_or_error = rtp_manager()->AddTrack(track, stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 876 | if (sender_or_error.ok()) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 877 | sdp_handler_->UpdateNegotiationNeeded(); |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 878 | stats_->AddTrack(track.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 879 | } |
| 880 | return sender_or_error; |
| 881 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 882 | |
Harald Alvestrand | 09a0d01 | 2022-01-04 19:42:07 | [diff] [blame] | 883 | RTCError PeerConnection::RemoveTrackOrError( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 884 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 885 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 886 | if (!ConfiguredForMedia()) { |
| 887 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 888 | "Not configured for media"); |
| 889 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 890 | if (!sender) { |
| 891 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 892 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 893 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 894 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 895 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 896 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 897 | if (IsUnifiedPlan()) { |
| 898 | auto transceiver = FindTransceiverBySender(sender); |
| 899 | if (!transceiver || !sender->track()) { |
| 900 | return RTCError::OK(); |
| 901 | } |
| 902 | sender->SetTrack(nullptr); |
| 903 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 904 | transceiver->internal()->set_direction( |
| 905 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 906 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 907 | transceiver->internal()->set_direction( |
| 908 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 909 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 910 | } else { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 911 | bool removed; |
| 912 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 913 | removed = rtp_manager()->GetAudioTransceiver()->internal()->RemoveSender( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 914 | sender.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 915 | } else { |
| 916 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 917 | removed = rtp_manager()->GetVideoTransceiver()->internal()->RemoveSender( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 918 | sender.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 919 | } |
| 920 | if (!removed) { |
| 921 | LOG_AND_RETURN_ERROR( |
| 922 | RTCErrorType::INVALID_PARAMETER, |
| 923 | "Couldn't find sender " + sender->id() + " to remove."); |
| 924 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 925 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 926 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 927 | return RTCError::OK(); |
| 928 | } |
| 929 | |
| 930 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 931 | PeerConnection::FindTransceiverBySender( |
| 932 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 933 | return rtp_manager()->transceivers()->FindBySender(sender); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 934 | } |
| 935 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 936 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 937 | PeerConnection::AddTransceiver( |
| 938 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 939 | if (!ConfiguredForMedia()) { |
| 940 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 941 | "Not configured for media"); |
| 942 | } |
| 943 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 944 | return AddTransceiver(track, RtpTransceiverInit()); |
| 945 | } |
| 946 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 947 | RtpTransportInternal* PeerConnection::GetRtpTransport(const std::string& mid) { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 948 | // TODO(bugs.webrtc.org/9987): Avoid the thread jump. |
| 949 | // This might be done by caching the value on the signaling thread. |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 950 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 951 | return network_thread()->Invoke<RtpTransportInternal*>( |
| 952 | RTC_FROM_HERE, [this, &mid] { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 953 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 954 | auto rtp_transport = transport_controller_->GetRtpTransport(mid); |
| 955 | RTC_DCHECK(rtp_transport); |
| 956 | return rtp_transport; |
| 957 | }); |
| 958 | } |
| 959 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 960 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 961 | PeerConnection::AddTransceiver( |
| 962 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 963 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 964 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 965 | if (!ConfiguredForMedia()) { |
| 966 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 967 | "Not configured for media"); |
| 968 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 969 | RTC_CHECK(IsUnifiedPlan()) |
| 970 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 971 | if (!track) { |
| 972 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 973 | } |
| 974 | cricket::MediaType media_type; |
| 975 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 976 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 977 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 978 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 979 | } else { |
| 980 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 981 | "Track kind is not audio or video"); |
| 982 | } |
| 983 | return AddTransceiver(media_type, track, init); |
| 984 | } |
| 985 | |
| 986 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 987 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 988 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 989 | } |
| 990 | |
| 991 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 992 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 993 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 994 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 995 | if (!ConfiguredForMedia()) { |
| 996 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 997 | "Not configured for media"); |
| 998 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 999 | RTC_CHECK(IsUnifiedPlan()) |
| 1000 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1001 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1002 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1003 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1004 | "media type is not audio or video"); |
| 1005 | } |
| 1006 | return AddTransceiver(media_type, nullptr, init); |
| 1007 | } |
| 1008 | |
| 1009 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1010 | PeerConnection::AddTransceiver( |
| 1011 | cricket::MediaType media_type, |
| 1012 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1013 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1014 | bool update_negotiation_needed) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1015 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1016 | if (!ConfiguredForMedia()) { |
| 1017 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1018 | "Not configured for media"); |
| 1019 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1020 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1021 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1022 | if (track) { |
| 1023 | RTC_DCHECK_EQ(media_type, |
| 1024 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1025 | ? cricket::MEDIA_TYPE_AUDIO |
| 1026 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1027 | } |
| 1028 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 1029 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1030 | init.send_encodings.size(), 0, 7, 8); |
| 1031 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1032 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1033 | [](const RtpEncodingParameters& encoding) { |
| 1034 | return !encoding.rid.empty(); |
| 1035 | }); |
| 1036 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-07 01:09:52 | [diff] [blame] | 1037 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1038 | RTCErrorType::INVALID_PARAMETER, |
| 1039 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 | [diff] [blame] | 1040 | } |
| 1041 | |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 1042 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1043 | [](const RtpEncodingParameters& encoding) { |
| 1044 | return !IsLegalRsidName(encoding.rid); |
| 1045 | })) { |
| 1046 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1047 | "Invalid RID value provided."); |
| 1048 | } |
| 1049 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1050 | if (absl::c_any_of(init.send_encodings, |
| 1051 | [](const RtpEncodingParameters& encoding) { |
| 1052 | return encoding.ssrc.has_value(); |
| 1053 | })) { |
| 1054 | LOG_AND_RETURN_ERROR( |
| 1055 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1056 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | RtpParameters parameters; |
| 1060 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1061 | |
| 1062 | // Encodings are dropped from the tail if too many are provided. |
Florent Castelli | e1b685a | 2021-04-30 17:11:37 | [diff] [blame] | 1063 | size_t max_simulcast_streams = |
| 1064 | media_type == cricket::MEDIA_TYPE_VIDEO ? kMaxSimulcastStreams : 1u; |
| 1065 | if (parameters.encodings.size() > max_simulcast_streams) { |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1066 | parameters.encodings.erase( |
Florent Castelli | e1b685a | 2021-04-30 17:11:37 | [diff] [blame] | 1067 | parameters.encodings.begin() + max_simulcast_streams, |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1068 | parameters.encodings.end()); |
| 1069 | } |
| 1070 | |
| 1071 | // Single RID should be removed. |
| 1072 | if (parameters.encodings.size() == 1 && |
| 1073 | !parameters.encodings[0].rid.empty()) { |
| 1074 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1075 | parameters.encodings[0].rid.clear(); |
| 1076 | } |
| 1077 | |
| 1078 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1079 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1080 | rtc::UniqueStringGenerator rid_generator; |
| 1081 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1082 | encoding.rid = rid_generator(); |
| 1083 | } |
| 1084 | } |
| 1085 | |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1086 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1087 | LOG_AND_RETURN_ERROR( |
| 1088 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1089 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1090 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1091 | |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1092 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1093 | if (!result.ok()) { |
| 1094 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1095 | } |
| 1096 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1097 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1098 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1099 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1100 | // that sender ID is already in use. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1101 | std::string sender_id = (track && !rtp_manager()->FindSenderById(track->id()) |
| 1102 | ? track->id() |
| 1103 | : rtc::CreateRandomUuid()); |
| 1104 | auto sender = rtp_manager()->CreateSender( |
| 1105 | media_type, sender_id, track, init.stream_ids, parameters.encodings); |
| 1106 | auto receiver = |
| 1107 | rtp_manager()->CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1108 | auto transceiver = rtp_manager()->CreateAndAddTransceiver(sender, receiver); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1109 | transceiver->internal()->set_direction(init.direction); |
| 1110 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1111 | if (update_negotiation_needed) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1112 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1113 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1114 | |
| 1115 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1116 | } |
| 1117 | |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1118 | void PeerConnection::OnNegotiationNeeded() { |
| 1119 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1120 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1121 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1122 | } |
| 1123 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1124 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-19 00:58:44 | [diff] [blame] | 1125 | const std::string& kind, |
| 1126 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1127 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1128 | if (!ConfiguredForMedia()) { |
| 1129 | RTC_LOG(LS_ERROR) << "Not configured for media"; |
| 1130 | return nullptr; |
| 1131 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1132 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1133 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1134 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1135 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1136 | if (IsClosed()) { |
| 1137 | return nullptr; |
| 1138 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1139 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1140 | // Internally we need to have one stream with Plan B semantics, so we |
| 1141 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1142 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1143 | if (stream_id.empty()) { |
| 1144 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1145 | RTC_LOG(LS_INFO) |
| 1146 | << "No stream_id specified for sender. Generated stream ID: " |
| 1147 | << stream_ids[0]; |
| 1148 | } else { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1149 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1150 | } |
| 1151 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1152 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1153 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1154 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1155 | auto audio_sender = AudioRtpSender::Create( |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1156 | worker_thread(), rtc::CreateRandomUuid(), stats_.get(), rtp_manager()); |
| 1157 | audio_sender->SetMediaChannel(rtp_manager()->voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1158 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1159 | signaling_thread(), audio_sender); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1160 | rtp_manager()->GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1161 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1162 | auto video_sender = VideoRtpSender::Create( |
| 1163 | worker_thread(), rtc::CreateRandomUuid(), rtp_manager()); |
| 1164 | video_sender->SetMediaChannel(rtp_manager()->video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1165 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1166 | signaling_thread(), video_sender); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1167 | rtp_manager()->GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1168 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1169 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1170 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1171 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1172 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1173 | |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1174 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1175 | } |
| 1176 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1177 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1178 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1179 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1180 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1181 | for (const auto& sender : rtp_manager()->GetSendersInternal()) { |
| 1182 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1183 | } |
| 1184 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1188 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1189 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1190 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1191 | for (const auto& receiver : rtp_manager()->GetReceiversInternal()) { |
| 1192 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1193 | } |
| 1194 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1195 | } |
| 1196 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1197 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1198 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1199 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1200 | RTC_CHECK(IsUnifiedPlan()) |
| 1201 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1202 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1203 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 1204 | all_transceivers.push_back(transceiver); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1205 | } |
| 1206 | return all_transceivers; |
| 1207 | } |
| 1208 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1209 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 | [diff] [blame] | 1210 | MediaStreamTrackInterface* track, |
| 1211 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1212 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1213 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 1214 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1215 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1216 | return false; |
| 1217 | } |
| 1218 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1219 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 1220 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 1221 | stats_->UpdateStats(level); |
Tommi | 1901551 | 2022-02-02 10:49:35 | [diff] [blame] | 1222 | |
| 1223 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(4); |
| 1224 | |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1225 | // The StatsCollector is used to tell if a track is valid because it may |
| 1226 | // remember tracks that the PeerConnection previously removed. |
| 1227 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1228 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1229 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1230 | return false; |
| 1231 | } |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 1232 | message_handler_.PostGetStats(observer, stats_.get(), track); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1233 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1234 | return true; |
| 1235 | } |
| 1236 | |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1237 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1238 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1239 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1240 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1241 | RTC_DCHECK(callback); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1242 | RTC_LOG_THREAD_BLOCK_COUNT(); |
Niels Möller | e7cc883 | 2022-01-04 14:20:03 | [diff] [blame] | 1243 | stats_collector_->GetStatsReport( |
| 1244 | rtc::scoped_refptr<RTCStatsCollectorCallback>(callback)); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1245 | } |
| 1246 | |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1247 | void PeerConnection::GetStats( |
| 1248 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1249 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1250 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1251 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1252 | RTC_DCHECK(callback); |
| 1253 | RTC_DCHECK(stats_collector_); |
| 1254 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1255 | if (selector) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1256 | for (const auto& proxy_transceiver : |
| 1257 | rtp_manager()->transceivers()->List()) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1258 | for (const auto& proxy_sender : |
| 1259 | proxy_transceiver->internal()->senders()) { |
| 1260 | if (proxy_sender == selector) { |
| 1261 | internal_sender = proxy_sender->internal(); |
| 1262 | break; |
| 1263 | } |
| 1264 | } |
| 1265 | if (internal_sender) |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 1269 | // If there is no `internal_sender` then `selector` is either null or does not |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1270 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1271 | // PeerConnection). This means that "all the stats objects representing the |
| 1272 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1273 | // produces an empty stats report. |
| 1274 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1275 | } |
| 1276 | |
| 1277 | void PeerConnection::GetStats( |
| 1278 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1279 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1280 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1281 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1282 | RTC_DCHECK(callback); |
| 1283 | RTC_DCHECK(stats_collector_); |
| 1284 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1285 | if (selector) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1286 | for (const auto& proxy_transceiver : |
| 1287 | rtp_manager()->transceivers()->List()) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1288 | for (const auto& proxy_receiver : |
| 1289 | proxy_transceiver->internal()->receivers()) { |
| 1290 | if (proxy_receiver == selector) { |
| 1291 | internal_receiver = proxy_receiver->internal(); |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | if (internal_receiver) |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 1299 | // If there is no `internal_receiver` then `selector` is either null or does |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1300 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1301 | // the PeerConnection). This means that "all the stats objects representing |
| 1302 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1303 | // selector produces an empty stats report. |
| 1304 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1305 | } |
| 1306 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1307 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1308 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1309 | return sdp_handler_->signaling_state(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1310 | } |
| 1311 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1312 | PeerConnectionInterface::IceConnectionState |
| 1313 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1314 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1315 | return ice_connection_state_; |
| 1316 | } |
| 1317 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1318 | PeerConnectionInterface::IceConnectionState |
| 1319 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1320 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1321 | return standardized_ice_connection_state_; |
| 1322 | } |
| 1323 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1324 | PeerConnectionInterface::PeerConnectionState |
| 1325 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1326 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1327 | return connection_state_; |
| 1328 | } |
| 1329 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1330 | PeerConnectionInterface::IceGatheringState |
| 1331 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1332 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1333 | return ice_gathering_state_; |
| 1334 | } |
| 1335 | |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1336 | absl::optional<bool> PeerConnection::can_trickle_ice_candidates() { |
| 1337 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1338 | const SessionDescriptionInterface* description = current_remote_description(); |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1339 | if (!description) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1340 | description = pending_remote_description(); |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1341 | } |
| 1342 | if (!description) { |
| 1343 | return absl::nullopt; |
| 1344 | } |
| 1345 | // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option. |
| 1346 | if (description->description()->transport_infos().size() < 1) { |
| 1347 | return absl::nullopt; |
| 1348 | } |
| 1349 | return description->description()->transport_infos()[0].description.HasOption( |
| 1350 | "trickle"); |
| 1351 | } |
| 1352 | |
Harald Alvestrand | a9af50f | 2021-05-21 13:33:51 | [diff] [blame] | 1353 | RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>> |
| 1354 | PeerConnection::CreateDataChannelOrError(const std::string& label, |
| 1355 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 09:17:47 | [diff] [blame] | 1356 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1357 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 18:14:50 | [diff] [blame] | 1358 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 1359 | bool first_datachannel = !data_channel_controller_.HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1360 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1361 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 1362 | if (config) { |
| 1363 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1364 | } |
Harald Alvestrand | a9af50f | 2021-05-21 13:33:51 | [diff] [blame] | 1365 | // TODO(bugs.webrtc.org/12796): Return a more specific error. |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 1366 | rtc::scoped_refptr<DataChannelInterface> channel( |
| 1367 | data_channel_controller_.InternalCreateDataChannelWithProxy( |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 1368 | label, internal_config.get())); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1369 | if (!channel.get()) { |
Harald Alvestrand | a9af50f | 2021-05-21 13:33:51 | [diff] [blame] | 1370 | return RTCError(RTCErrorType::INTERNAL_ERROR, |
| 1371 | "Data channel creation failed"); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1372 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1373 | |
Harald Alvestrand | 48171ec | 2021-04-20 15:06:03 | [diff] [blame] | 1374 | // Trigger the onRenegotiationNeeded event for |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1375 | // the first SCTP DataChannel. |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 1376 | if (first_datachannel) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1377 | sdp_handler_->UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1378 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1379 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 1380 | return channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1381 | } |
| 1382 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1383 | void PeerConnection::RestartIce() { |
| 1384 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1385 | sdp_handler_->RestartIce(); |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1386 | } |
| 1387 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1388 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 1389 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1390 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1391 | sdp_handler_->CreateOffer(observer, options); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1392 | } |
| 1393 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1394 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1395 | const RTCOfferAnswerOptions& options) { |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1396 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1397 | sdp_handler_->CreateAnswer(observer, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1398 | } |
| 1399 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1400 | void PeerConnection::SetLocalDescription( |
| 1401 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 1402 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1403 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1404 | sdp_handler_->SetLocalDescription(observer, desc_ptr); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | void PeerConnection::SetLocalDescription( |
| 1408 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1409 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1410 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1411 | sdp_handler_->SetLocalDescription(std::move(desc), observer); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1412 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 1413 | |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 1414 | void PeerConnection::SetLocalDescription( |
| 1415 | SetSessionDescriptionObserver* observer) { |
| 1416 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1417 | sdp_handler_->SetLocalDescription(observer); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | void PeerConnection::SetLocalDescription( |
| 1421 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1422 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1423 | sdp_handler_->SetLocalDescription(observer); |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 1424 | } |
| 1425 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1426 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 | [diff] [blame] | 1427 | SetSessionDescriptionObserver* observer, |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1428 | SessionDescriptionInterface* desc_ptr) { |
| 1429 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1430 | sdp_handler_->SetRemoteDescription(observer, desc_ptr); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | void PeerConnection::SetRemoteDescription( |
| 1434 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1435 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1436 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1437 | sdp_handler_->SetRemoteDescription(std::move(desc), observer); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 1438 | } |
| 1439 | |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 1440 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1441 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 1442 | return configuration_; |
| 1443 | } |
| 1444 | |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1445 | RTCError PeerConnection::SetConfiguration( |
| 1446 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1447 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1448 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 1449 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1450 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1451 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 1452 | } |
| 1453 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 1454 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1455 | // size is not allowed, and changing the set of ICE servers will not result |
| 1456 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1457 | if (local_description() && configuration.ice_candidate_pool_size != |
| 1458 | configuration_.ice_candidate_pool_size) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1459 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 1460 | "Can't change candidate pool size after calling " |
| 1461 | "SetLocalDescription."); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1462 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1463 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 1464 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1465 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1466 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 1467 | "Can't change crypto_options after calling " |
| 1468 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1469 | } |
| 1470 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1471 | // The simplest (and most future-compatible) way to tell if the config was |
| 1472 | // modified in an invalid way is to copy each property we do support |
| 1473 | // modifying, then use operator==. There are far more properties we don't |
| 1474 | // support modifying than those we do, and more could be added. |
| 1475 | RTCConfiguration modified_config = configuration_; |
| 1476 | modified_config.servers = configuration.servers; |
| 1477 | modified_config.type = configuration.type; |
| 1478 | modified_config.ice_candidate_pool_size = |
| 1479 | configuration.ice_candidate_pool_size; |
| 1480 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1481 | modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy; |
Qingsi Wang | bca1485 | 2019-06-26 21:56:02 | [diff] [blame] | 1482 | modified_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 1483 | configuration.surface_ice_candidates_on_ice_transport_type_changed; |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 1484 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 1485 | modified_config.ice_check_interval_strong_connectivity = |
| 1486 | configuration.ice_check_interval_strong_connectivity; |
| 1487 | modified_config.ice_check_interval_weak_connectivity = |
| 1488 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 1489 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 1490 | modified_config.ice_unwritable_min_checks = |
| 1491 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 1492 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 1493 | modified_config.stun_candidate_keepalive_interval = |
| 1494 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 1495 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 1496 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1497 | modified_config.active_reset_srtp_params = |
| 1498 | configuration.active_reset_srtp_params; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 1499 | modified_config.turn_logging_id = configuration.turn_logging_id; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 1500 | modified_config.allow_codec_switching = configuration.allow_codec_switching; |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 1501 | modified_config.stable_writable_connection_ping_interval_ms = |
| 1502 | configuration.stable_writable_connection_ping_interval_ms; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1503 | if (configuration != modified_config) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1504 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 1505 | "Modifying the configuration in an unsupported way."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1506 | } |
| 1507 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1508 | // Validate the modified configuration. |
| 1509 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 1510 | if (!validate_error.ok()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1511 | return validate_error; |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1512 | } |
| 1513 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1514 | // Note that this isn't possible through chromium, since it's an unsigned |
| 1515 | // short in WebIDL. |
| 1516 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 10:34:17 | [diff] [blame] | 1517 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1518 | return RTCError(RTCErrorType::INVALID_RANGE); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | // Parse ICE servers before hopping to network thread. |
| 1522 | cricket::ServerAddresses stun_servers; |
| 1523 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1524 | RTCErrorType parse_error = |
| 1525 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 1526 | if (parse_error != RTCErrorType::NONE) { |
Philipp Hancke | 05fadac | 2021-11-04 08:13:17 | [diff] [blame] | 1527 | return RTCError(parse_error, "ICE server parse failed"); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1528 | } |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 1529 | // Add the turn logging id to all turn servers |
| 1530 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 1531 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 1532 | } |
| 1533 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1534 | // Note if STUN or TURN servers were supplied. |
| 1535 | if (!stun_servers.empty()) { |
| 1536 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1537 | } |
| 1538 | if (!turn_servers.empty()) { |
| 1539 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1540 | } |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1541 | |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1542 | const bool has_local_description = local_description() != nullptr; |
| 1543 | |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1544 | const bool needs_ice_restart = |
| 1545 | modified_config.servers != configuration_.servers || |
| 1546 | NeedIceRestart( |
| 1547 | configuration_.surface_ice_candidates_on_ice_transport_type_changed, |
| 1548 | configuration_.type, modified_config.type) || |
| 1549 | modified_config.GetTurnPortPrunePolicy() != |
| 1550 | configuration_.GetTurnPortPrunePolicy(); |
| 1551 | cricket::IceConfig ice_config = ParseIceConfig(modified_config); |
| 1552 | |
| 1553 | // Apply part of the configuration on the network thread. In theory this |
| 1554 | // shouldn't fail. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1555 | if (!network_thread()->Invoke<bool>( |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1556 | RTC_FROM_HERE, |
| 1557 | [this, needs_ice_restart, &ice_config, &stun_servers, &turn_servers, |
| 1558 | &modified_config, has_local_description] { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1559 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1560 | // As described in JSEP, calling setConfiguration with new ICE |
| 1561 | // servers or candidate policy must set a "needs-ice-restart" bit so |
| 1562 | // that the next offer triggers an ICE restart which will pick up |
| 1563 | // the changes. |
| 1564 | if (needs_ice_restart) |
| 1565 | transport_controller_->SetNeedsIceRestartFlag(); |
| 1566 | |
| 1567 | transport_controller_->SetIceConfig(ice_config); |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1568 | return ReconfigurePortAllocator_n( |
| 1569 | stun_servers, turn_servers, modified_config.type, |
| 1570 | modified_config.ice_candidate_pool_size, |
| 1571 | modified_config.GetTurnPortPrunePolicy(), |
| 1572 | modified_config.turn_customizer, |
| 1573 | modified_config.stun_candidate_keepalive_interval, |
| 1574 | has_local_description); |
| 1575 | })) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1576 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 1577 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1578 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 1579 | |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1580 | if (configuration_.active_reset_srtp_params != |
| 1581 | modified_config.active_reset_srtp_params) { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1582 | // TODO(tommi): merge invokes |
| 1583 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this, &modified_config] { |
| 1584 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1585 | transport_controller_->SetActiveResetSrtpParams( |
| 1586 | modified_config.active_reset_srtp_params); |
| 1587 | }); |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1588 | } |
| 1589 | |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 1590 | if (modified_config.allow_codec_switching.has_value()) { |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1591 | std::vector<cricket::VideoMediaChannel*> channels; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1592 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1593 | if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) |
Taylor Brandstetter | 6b381c7 | 2020-08-24 20:28:47 | [diff] [blame] | 1594 | continue; |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1595 | |
Harald Alvestrand | 2c761b2 | 2022-04-30 16:27:40 | [diff] [blame] | 1596 | auto* video_channel = transceiver->internal()->channel(); |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1597 | if (video_channel) |
Harald Alvestrand | 2c761b2 | 2022-04-30 16:27:40 | [diff] [blame] | 1598 | channels.push_back(static_cast<cricket::VideoMediaChannel*>( |
| 1599 | video_channel->media_channel())); |
philipel | 01294f0 | 2019-11-14 12:03:25 | [diff] [blame] | 1600 | } |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1601 | |
| 1602 | worker_thread()->Invoke<void>( |
| 1603 | RTC_FROM_HERE, |
| 1604 | [channels = std::move(channels), |
| 1605 | allow_codec_switching = *modified_config.allow_codec_switching]() { |
| 1606 | for (auto* ch : channels) |
| 1607 | ch->SetVideoCodecSwitchingEnabled(allow_codec_switching); |
| 1608 | }); |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 1609 | } |
| 1610 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1611 | configuration_ = modified_config; |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1612 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1613 | } |
| 1614 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1615 | bool PeerConnection::AddIceCandidate( |
| 1616 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1617 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1618 | return sdp_handler_->AddIceCandidate(ice_candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1619 | } |
| 1620 | |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 1621 | void PeerConnection::AddIceCandidate( |
| 1622 | std::unique_ptr<IceCandidateInterface> candidate, |
| 1623 | std::function<void(RTCError)> callback) { |
| 1624 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1625 | sdp_handler_->AddIceCandidate(std::move(candidate), callback); |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 1626 | } |
| 1627 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 1628 | bool PeerConnection::RemoveIceCandidates( |
| 1629 | const std::vector<cricket::Candidate>& candidates) { |
| 1630 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 1631 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1632 | return sdp_handler_->RemoveIceCandidates(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 1633 | } |
| 1634 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1635 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 1636 | if (!worker_thread()->IsCurrent()) { |
| 1637 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1638 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1639 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1640 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1641 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1642 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 1643 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 1644 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1645 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 1646 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1647 | "min_bitrate_bps <= 0"); |
| 1648 | } |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1649 | if (has_start) { |
| 1650 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1651 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1652 | "start_bitrate_bps < min_bitrate_bps"); |
| 1653 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1654 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1655 | "curent_bitrate_bps < 0"); |
| 1656 | } |
| 1657 | } |
| 1658 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1659 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1660 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1661 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1662 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 1663 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1664 | "max_bitrate_bps < min_bitrate_bps"); |
| 1665 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 1666 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1667 | "max_bitrate_bps < 0"); |
| 1668 | } |
| 1669 | } |
| 1670 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1671 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 17:31:54 | [diff] [blame] | 1672 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1673 | |
| 1674 | return RTCError::OK(); |
| 1675 | } |
| 1676 | |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1677 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 1678 | if (!worker_thread()->IsCurrent()) { |
| 1679 | worker_thread()->Invoke<void>( |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1680 | RTC_FROM_HERE, [this, playout] { SetAudioPlayout(playout); }); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1681 | return; |
| 1682 | } |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1683 | auto audio_state = |
| 1684 | context_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1685 | audio_state->SetPlayout(playout); |
| 1686 | } |
| 1687 | |
| 1688 | void PeerConnection::SetAudioRecording(bool recording) { |
| 1689 | if (!worker_thread()->IsCurrent()) { |
| 1690 | worker_thread()->Invoke<void>( |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1691 | RTC_FROM_HERE, [this, recording] { SetAudioRecording(recording); }); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1692 | return; |
| 1693 | } |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1694 | auto audio_state = |
| 1695 | context_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1696 | audio_state->SetRecording(recording); |
| 1697 | } |
| 1698 | |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 1699 | void PeerConnection::AddAdaptationResource( |
| 1700 | rtc::scoped_refptr<Resource> resource) { |
| 1701 | if (!worker_thread()->IsCurrent()) { |
| 1702 | return worker_thread()->Invoke<void>(RTC_FROM_HERE, [this, resource]() { |
| 1703 | return AddAdaptationResource(resource); |
| 1704 | }); |
| 1705 | } |
| 1706 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1707 | if (!call_) { |
| 1708 | // The PeerConnection has been closed. |
| 1709 | return; |
| 1710 | } |
| 1711 | call_->AddAdaptationResource(resource); |
| 1712 | } |
| 1713 | |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1714 | bool PeerConnection::ConfiguredForMedia() const { |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1715 | return context_->channel_manager()->media_engine(); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1716 | } |
| 1717 | |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 1718 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 1719 | int64_t output_period_ms) { |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 1720 | return worker_thread()->Invoke<bool>( |
Danil Chapovalov | 116ffe7 | 2019-09-05 08:21:11 | [diff] [blame] | 1721 | RTC_FROM_HERE, |
| 1722 | [this, output = std::move(output), output_period_ms]() mutable { |
| 1723 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 1724 | }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1725 | } |
| 1726 | |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 1727 | bool PeerConnection::StartRtcEventLog( |
| 1728 | std::unique_ptr<RtcEventLogOutput> output) { |
Niels Möller | 695cf6a | 2019-05-13 10:27:23 | [diff] [blame] | 1729 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 1730 | if (trials().IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
Niels Möller | 695cf6a | 2019-05-13 10:27:23 | [diff] [blame] | 1731 | output_period_ms = 5000; |
| 1732 | } |
| 1733 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 1734 | } |
| 1735 | |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1736 | void PeerConnection::StopRtcEventLog() { |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1737 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { StopRtcEventLog_w(); }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1738 | } |
| 1739 | |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 1740 | rtc::scoped_refptr<DtlsTransportInterface> |
| 1741 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Tomas Gunnarsson | 2aeab5e | 2021-02-23 20:36:14 | [diff] [blame] | 1742 | RTC_DCHECK_RUN_ON(network_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 1743 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 1744 | } |
| 1745 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 1746 | rtc::scoped_refptr<DtlsTransport> |
| 1747 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 1748 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1749 | // TODO(bugs.webrtc.org/9987): Avoid the thread jump. |
| 1750 | // This might be done by caching the value on the signaling thread. |
| 1751 | return network_thread()->Invoke<rtc::scoped_refptr<DtlsTransport>>( |
| 1752 | RTC_FROM_HERE, [this, mid]() { |
| 1753 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1754 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 1755 | }); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 1756 | } |
| 1757 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1758 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 1759 | const { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1760 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1761 | if (!sctp_mid_n_) |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1762 | return nullptr; |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1763 | |
| 1764 | return transport_controller_->GetSctpTransport(*sctp_mid_n_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1765 | } |
| 1766 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1767 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1768 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1769 | return sdp_handler_->local_description(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1773 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1774 | return sdp_handler_->remote_description(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1775 | } |
| 1776 | |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1777 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 1778 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1779 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1780 | return sdp_handler_->current_local_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 1784 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1785 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1786 | return sdp_handler_->current_remote_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 1790 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1791 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1792 | return sdp_handler_->pending_local_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 1796 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1797 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1798 | return sdp_handler_->pending_remote_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1799 | } |
| 1800 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1801 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1802 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1803 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1804 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1805 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 1806 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1807 | if (IsClosed()) { |
| 1808 | return; |
| 1809 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1810 | // Update stats here so that we have the most recent stats for tracks and |
| 1811 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 1812 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1813 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1814 | ice_connection_state_ = PeerConnectionInterface::kIceConnectionClosed; |
| 1815 | Observer()->OnIceConnectionChange(ice_connection_state_); |
| 1816 | standardized_ice_connection_state_ = |
| 1817 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
| 1818 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 1819 | Observer()->OnConnectionChange(connection_state_); |
| 1820 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1821 | sdp_handler_->Close(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1822 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1823 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1824 | |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1825 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 1826 | transceiver->internal()->SetPeerConnectionClosed(); |
| 1827 | if (!transceiver->stopped()) |
| 1828 | transceiver->StopInternal(); |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1829 | } |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1830 | |
Steve Anton | 25cfeb9 | 2018-04-26 18:44:00 | [diff] [blame] | 1831 | // Ensure that all asynchronous stats requests are completed before destroying |
| 1832 | // the transport controller below. |
| 1833 | if (stats_collector_) { |
| 1834 | stats_collector_->WaitForPendingRequest(); |
| 1835 | } |
| 1836 | |
| 1837 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 1838 | // the last stats request can still read from the channels. |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1839 | sdp_handler_->DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1840 | |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1841 | // The event log is used in the transport controller, which must be outlived |
| 1842 | // by the former. CreateOffer by the peer connection is implemented |
| 1843 | // asynchronously and if the peer connection is closed without resetting the |
| 1844 | // WebRTC session description factory, the session description factory would |
| 1845 | // call the transport controller. |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1846 | sdp_handler_->ResetSessionDescFactory(); |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 1847 | rtp_manager_->Close(); |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1848 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1849 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 1850 | // Data channels will already have been unset via the DestroyAllChannels() |
| 1851 | // call above, which triggers a call to TeardownDataChannelTransport_n(). |
| 1852 | // TODO(tommi): ^^ That's not exactly optimal since this is yet another |
| 1853 | // blocking hop to the network thread during Close(). Further still, the |
| 1854 | // voice/video/data channels will be cleared on the worker thread. |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1855 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1856 | transport_controller_.reset(); |
| 1857 | port_allocator_->DiscardCandidatePool(); |
| 1858 | if (network_thread_safety_) { |
| 1859 | network_thread_safety_->SetNotAlive(); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1860 | } |
| 1861 | }); |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 1862 | |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 1863 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1864 | RTC_DCHECK_RUN_ON(worker_thread()); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 1865 | worker_thread_safety_->SetNotAlive(); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1866 | call_.reset(); |
| 1867 | // The event log must outlive call (and any other object that uses it). |
| 1868 | event_log_.reset(); |
| 1869 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1870 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 1871 | // The .h file says that observer can be discarded after close() returns. |
| 1872 | // Make sure this is true. |
| 1873 | observer_ = nullptr; |
Tomas Gunnarsson | 2efb8a5 | 2021-04-01 14:26:57 | [diff] [blame] | 1874 | |
| 1875 | // Signal shutdown to the sdp handler. This invalidates weak pointers for |
| 1876 | // internal pending callbacks. |
| 1877 | sdp_handler_->PrepareForShutdown(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1878 | } |
| 1879 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1880 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1881 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1882 | if (ice_connection_state_ == new_state) { |
| 1883 | return; |
| 1884 | } |
| 1885 | |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 1886 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1887 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1888 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 1889 | return; |
| 1890 | } |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1891 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1892 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 1893 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1894 | RTC_DCHECK(ice_connection_state_ != |
| 1895 | PeerConnectionInterface::kIceConnectionClosed); |
| 1896 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1897 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 1898 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1899 | } |
| 1900 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1901 | void PeerConnection::SetStandardizedIceConnectionState( |
| 1902 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1903 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1904 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1908 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 1912 | << standardized_ice_connection_state_ << " => " << new_state; |
| 1913 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1914 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 10:25:14 | [diff] [blame] | 1915 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1916 | } |
| 1917 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1918 | void PeerConnection::SetConnectionState( |
| 1919 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1920 | if (connection_state_ == new_state) |
| 1921 | return; |
| 1922 | if (IsClosed()) |
| 1923 | return; |
| 1924 | connection_state_ = new_state; |
| 1925 | Observer()->OnConnectionChange(new_state); |
Philipp Hancke | bb8f32f | 2021-02-04 20:50:50 | [diff] [blame] | 1926 | |
Philipp Hancke | cd0373f | 2021-02-24 10:04:30 | [diff] [blame] | 1927 | if (new_state == PeerConnectionState::kConnected && !was_ever_connected_) { |
| 1928 | was_ever_connected_ = true; |
| 1929 | |
| 1930 | // The first connection state change to connected happens once per |
| 1931 | // connection which makes it a good point to report metrics. |
Philipp Hancke | bb8f32f | 2021-02-04 20:50:50 | [diff] [blame] | 1932 | // Record bundle-policy from configuration. Done here from |
| 1933 | // connectionStateChange to limit to actually established connections. |
| 1934 | BundlePolicyUsage policy = kBundlePolicyUsageMax; |
| 1935 | switch (configuration_.bundle_policy) { |
| 1936 | case kBundlePolicyBalanced: |
| 1937 | policy = kBundlePolicyUsageBalanced; |
| 1938 | break; |
| 1939 | case kBundlePolicyMaxBundle: |
| 1940 | policy = kBundlePolicyUsageMaxBundle; |
| 1941 | break; |
| 1942 | case kBundlePolicyMaxCompat: |
| 1943 | policy = kBundlePolicyUsageMaxCompat; |
| 1944 | break; |
| 1945 | } |
| 1946 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.BundlePolicy", policy, |
| 1947 | kBundlePolicyUsageMax); |
Philipp Hancke | 8973655 | 2021-03-05 11:07:05 | [diff] [blame] | 1948 | |
| 1949 | // Record configured ice candidate pool size depending on the |
| 1950 | // BUNDLE policy. See |
| 1951 | // https://w3c.github.io/webrtc-pc/#dom-rtcconfiguration-icecandidatepoolsize |
| 1952 | // The ICE candidate pool size is an optimization and it may be desirable |
| 1953 | // to restrict the maximum size of the pre-gathered candidates. |
| 1954 | switch (configuration_.bundle_policy) { |
| 1955 | case kBundlePolicyBalanced: |
| 1956 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 1957 | "WebRTC.PeerConnection.CandidatePoolUsage.Balanced", |
| 1958 | configuration_.ice_candidate_pool_size, 0, 255, 256); |
| 1959 | break; |
| 1960 | case kBundlePolicyMaxBundle: |
| 1961 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 1962 | "WebRTC.PeerConnection.CandidatePoolUsage.MaxBundle", |
| 1963 | configuration_.ice_candidate_pool_size, 0, 255, 256); |
| 1964 | break; |
| 1965 | case kBundlePolicyMaxCompat: |
| 1966 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 1967 | "WebRTC.PeerConnection.CandidatePoolUsage.MaxCompat", |
| 1968 | configuration_.ice_candidate_pool_size, 0, 255, 256); |
| 1969 | break; |
| 1970 | } |
Philipp Hancke | 1b4807f | 2021-06-11 09:49:54 | [diff] [blame] | 1971 | |
| 1972 | // Record whether there was a local or remote provisional answer. |
| 1973 | ProvisionalAnswerUsage pranswer = kProvisionalAnswerNotUsed; |
| 1974 | if (local_description()->GetType() == SdpType::kPrAnswer) { |
| 1975 | pranswer = kProvisionalAnswerLocal; |
| 1976 | } else if (remote_description()->GetType() == SdpType::kPrAnswer) { |
| 1977 | pranswer = kProvisionalAnswerRemote; |
| 1978 | } |
| 1979 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.ProvisionalAnswer", |
| 1980 | pranswer, kProvisionalAnswerMax); |
Philipp Hancke | f59d9fb | 2021-10-13 09:34:15 | [diff] [blame] | 1981 | |
| 1982 | // Record the number of configured ICE servers for connected connections. |
| 1983 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.PeerConnection.IceServers.Connected", |
| 1984 | configuration_.servers.size(), 0, 31, 32); |
Philipp Hancke | bb8f32f | 2021-02-04 20:50:50 | [diff] [blame] | 1985 | } |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1986 | } |
| 1987 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1988 | void PeerConnection::OnIceGatheringChange( |
| 1989 | PeerConnectionInterface::IceGatheringState new_state) { |
| 1990 | if (IsClosed()) { |
| 1991 | return; |
| 1992 | } |
| 1993 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 1994 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1995 | } |
| 1996 | |
jbauch | 81bf7b0 | 2017-03-25 15:31:12 | [diff] [blame] | 1997 | void PeerConnection::OnIceCandidate( |
| 1998 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1999 | if (IsClosed()) { |
| 2000 | return; |
| 2001 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2002 | ReportIceCandidateCollected(candidate->candidate()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2003 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2004 | } |
| 2005 | |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2006 | void PeerConnection::OnIceCandidateError(const std::string& address, |
| 2007 | int port, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2008 | const std::string& url, |
| 2009 | int error_code, |
| 2010 | const std::string& error_text) { |
| 2011 | if (IsClosed()) { |
| 2012 | return; |
| 2013 | } |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2014 | Observer()->OnIceCandidateError(address, port, url, error_code, error_text); |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2015 | } |
| 2016 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 2017 | void PeerConnection::OnIceCandidatesRemoved( |
| 2018 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 2019 | if (IsClosed()) { |
| 2020 | return; |
| 2021 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2022 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 2023 | } |
| 2024 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2025 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 2026 | const cricket::CandidatePairChangeEvent& event) { |
| 2027 | if (IsClosed()) { |
| 2028 | return; |
| 2029 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2030 | |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 2031 | if (event.selected_candidate_pair.local_candidate().type() == |
| 2032 | LOCAL_PORT_TYPE && |
| 2033 | event.selected_candidate_pair.remote_candidate().type() == |
| 2034 | LOCAL_PORT_TYPE) { |
| 2035 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 2036 | } |
| 2037 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2038 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 2039 | } |
| 2040 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2041 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2042 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Florent Castelli | 516e284 | 2021-04-19 13:29:50 | [diff] [blame] | 2043 | return sctp_mid_s_; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 2044 | } |
| 2045 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2046 | void PeerConnection::SetSctpDataMid(const std::string& mid) { |
| 2047 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2048 | sctp_mid_s_ = mid; |
| 2049 | } |
| 2050 | |
| 2051 | void PeerConnection::ResetSctpDataMid() { |
| 2052 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2053 | sctp_mid_s_.reset(); |
| 2054 | sctp_transport_name_s_.clear(); |
| 2055 | } |
| 2056 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2057 | void PeerConnection::OnSctpDataChannelClosed(DataChannelInterface* channel) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2058 | // Since data_channel_controller doesn't do signals, this |
| 2059 | // signal is relayed here. |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2060 | data_channel_controller_.OnSctpDataChannelClosed( |
| 2061 | static_cast<SctpDataChannel*>(channel)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2062 | } |
| 2063 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2064 | PeerConnection::InitializePortAllocatorResult |
| 2065 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 2066 | const cricket::ServerAddresses& stun_servers, |
| 2067 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2068 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2069 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2070 | |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 2071 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2072 | // To handle both internal and externally created port allocator, we will |
| 2073 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2074 | int port_allocator_flags = port_allocator_->flags(); |
| 2075 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 2076 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 2077 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2078 | // If the disable-IPv6 flag was specified, we'll not override it |
| 2079 | // by experiment. |
| 2080 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2081 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 2082 | } else if (trials().IsDisabled("WebRTC-IPv6Default")) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2083 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2084 | } |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 2085 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2086 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2087 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 2088 | } |
| 2089 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2090 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2091 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2092 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2093 | } |
| 2094 | |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 2095 | if (configuration.candidate_network_policy == |
| 2096 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2097 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2098 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 2099 | } |
| 2100 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 2101 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2102 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 2103 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 2104 | } |
| 2105 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2106 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2107 | // No step delay is used while allocating ports. |
| 2108 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2109 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2110 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 2111 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2112 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 2113 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2114 | for (auto& turn_server : turn_servers_copy) { |
| 2115 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 2116 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2117 | // Call this last since it may create pooled allocator sessions using the |
| 2118 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2119 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2120 | stun_servers, std::move(turn_servers_copy), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2121 | configuration.ice_candidate_pool_size, |
| 2122 | configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2123 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2124 | |
| 2125 | InitializePortAllocatorResult res; |
| 2126 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 2127 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2128 | } |
| 2129 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 2130 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 2131 | const cricket::ServerAddresses& stun_servers, |
| 2132 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 2133 | IceTransportsType type, |
| 2134 | int candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2135 | PortPrunePolicy turn_port_prune_policy, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2136 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 2137 | absl::optional<int> stun_candidate_keepalive_interval, |
| 2138 | bool have_local_description) { |
Harald Alvestrand | f598e49 | 2020-11-04 05:54:10 | [diff] [blame] | 2139 | RTC_DCHECK_RUN_ON(network_thread()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2140 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 2141 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 2142 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 2143 | // size is not allowed, and changing the set of ICE servers will not result |
| 2144 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 2145 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 2146 | port_allocator_->FreezeCandidatePool(); |
| 2147 | } |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2148 | // Add the custom tls turn servers if they exist. |
| 2149 | auto turn_servers_copy = turn_servers; |
| 2150 | for (auto& turn_server : turn_servers_copy) { |
| 2151 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 2152 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2153 | // Call this last since it may create pooled allocator sessions using the |
| 2154 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 2155 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2156 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2157 | turn_port_prune_policy, turn_customizer, |
| 2158 | stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2159 | } |
| 2160 | |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 2161 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 2162 | std::unique_ptr<RtcEventLogOutput> output, |
| 2163 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 2164 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 2165 | if (!event_log_) { |
| 2166 | return false; |
| 2167 | } |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 2168 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 2172 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 2173 | if (event_log_) { |
| 2174 | event_log_->StopLogging(); |
| 2175 | } |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 2176 | } |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 2177 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2178 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2179 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2180 | if (!local_description() || !remote_description()) { |
Harald Alvestrand | 977b265 | 2019-12-12 12:40:50 | [diff] [blame] | 2181 | RTC_LOG(LS_VERBOSE) |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2182 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2183 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2184 | return false; |
| 2185 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2186 | if (!data_channel_controller_.data_channel_transport()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2187 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2188 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2189 | return false; |
| 2190 | } |
| 2191 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2192 | absl::optional<rtc::SSLRole> dtls_role; |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 2193 | if (sctp_mid_s_) { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2194 | dtls_role = network_thread()->Invoke<absl::optional<rtc::SSLRole>>( |
| 2195 | RTC_FROM_HERE, [this] { |
| 2196 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2197 | return transport_controller_->GetDtlsRole(*sctp_mid_n_); |
| 2198 | }); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2199 | if (!dtls_role && sdp_handler_->is_caller().has_value()) { |
Harald Alvestrand | 321ec3b | 2022-02-10 13:42:18 | [diff] [blame] | 2200 | // This works fine if we are the offerer, but can be a mistake if |
Harald Alvestrand | 06c87a1 | 2022-02-11 13:12:16 | [diff] [blame] | 2201 | // we are the answerer and the remote offer is ACTIVE. In that |
Harald Alvestrand | 321ec3b | 2022-02-10 13:42:18 | [diff] [blame] | 2202 | // case, we will guess the role wrong. |
| 2203 | // TODO(bugs.webrtc.org/13668): Check if this actually happens. |
Harald Alvestrand | 06c87a1 | 2022-02-11 13:12:16 | [diff] [blame] | 2204 | RTC_LOG(LS_ERROR) |
| 2205 | << "Possible risk: DTLS role guesser is active, is_caller is " |
| 2206 | << *sdp_handler_->is_caller(); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2207 | dtls_role = |
| 2208 | *sdp_handler_->is_caller() ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2209 | } |
Harald Alvestrand | 321ec3b | 2022-02-10 13:42:18 | [diff] [blame] | 2210 | if (dtls_role) { |
| 2211 | *role = *dtls_role; |
| 2212 | return true; |
| 2213 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2214 | } |
| 2215 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 2219 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2220 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2221 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2222 | RTC_LOG(LS_INFO) |
| 2223 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2224 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2225 | return false; |
| 2226 | } |
| 2227 | |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2228 | auto dtls_role = network_thread()->Invoke<absl::optional<rtc::SSLRole>>( |
| 2229 | RTC_FROM_HERE, [this, content_name]() { |
| 2230 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2231 | return transport_controller_->GetDtlsRole(content_name); |
| 2232 | }); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2233 | if (dtls_role) { |
| 2234 | *role = *dtls_role; |
| 2235 | return true; |
| 2236 | } |
| 2237 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2238 | } |
| 2239 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2240 | bool PeerConnection::GetTransportDescription( |
| 2241 | const SessionDescription* description, |
| 2242 | const std::string& content_name, |
| 2243 | cricket::TransportDescription* tdesc) { |
| 2244 | if (!description || !tdesc) { |
| 2245 | return false; |
| 2246 | } |
| 2247 | const TransportInfo* transport_info = |
| 2248 | description->GetTransportInfoByName(content_name); |
| 2249 | if (!transport_info) { |
| 2250 | return false; |
| 2251 | } |
| 2252 | *tdesc = transport_info->description; |
| 2253 | return true; |
| 2254 | } |
| 2255 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2256 | std::vector<DataChannelStats> PeerConnection::GetDataChannelStats() const { |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 2257 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2258 | return data_channel_controller_.GetDataChannelStats(); |
| 2259 | } |
| 2260 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2261 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2262 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2263 | if (sctp_mid_s_ && transport_controller_copy_) |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2264 | return sctp_transport_name_s_; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2265 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2266 | } |
| 2267 | |
Tomas Gunnarsson | bfd9ba8 | 2021-04-18 09:55:57 | [diff] [blame] | 2268 | absl::optional<std::string> PeerConnection::sctp_mid() const { |
| 2269 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2270 | return sctp_mid_s_; |
| 2271 | } |
| 2272 | |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 2273 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2274 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2275 | if (!network_thread_safety_->alive()) |
| 2276 | return {}; |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 2277 | cricket::CandidateStatsList candidate_states_list; |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2278 | port_allocator_->GetCandidateStatsFromPooledSessions(&candidate_states_list); |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 2279 | return candidate_states_list; |
| 2280 | } |
| 2281 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 2282 | std::map<std::string, cricket::TransportStats> |
| 2283 | PeerConnection::GetTransportStatsByNames( |
| 2284 | const std::set<std::string>& transport_names) { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 2285 | TRACE_EVENT0("webrtc", "PeerConnection::GetTransportStatsByNames"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2286 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2287 | if (!network_thread_safety_->alive()) |
| 2288 | return {}; |
| 2289 | |
| 2290 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 2291 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 2292 | for (const std::string& transport_name : transport_names) { |
| 2293 | cricket::TransportStats transport_stats; |
| 2294 | bool success = |
| 2295 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 2296 | if (success) { |
| 2297 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 2298 | } else { |
| 2299 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 2300 | << transport_name; |
| 2301 | } |
| 2302 | } |
| 2303 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | bool PeerConnection::GetLocalCertificate( |
| 2307 | const std::string& transport_name, |
| 2308 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2309 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2310 | if (!network_thread_safety_->alive() || !certificate) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2311 | return false; |
| 2312 | } |
| 2313 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 2314 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2315 | } |
| 2316 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 2317 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2318 | const std::string& transport_name) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2319 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 2320 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2321 | } |
| 2322 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2323 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 13:36:53 | [diff] [blame] | 2324 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2325 | return sdp_handler_->IceRestartPending(content_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2326 | } |
| 2327 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2328 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2329 | return network_thread()->Invoke<bool>(RTC_FROM_HERE, [this, &content_name] { |
| 2330 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2331 | return transport_controller_->NeedsIceRestart(content_name); |
| 2332 | }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2333 | } |
| 2334 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2335 | void PeerConnection::OnTransportControllerConnectionState( |
| 2336 | cricket::IceConnectionState state) { |
| 2337 | switch (state) { |
| 2338 | case cricket::kIceConnectionConnecting: |
| 2339 | // If the current state is Connected or Completed, then there were |
| 2340 | // writable channels but now there are not, so the next state must |
| 2341 | // be Disconnected. |
| 2342 | // kIceConnectionConnecting is currently used as the default, |
| 2343 | // un-connected state by the TransportController, so its only use is |
| 2344 | // detecting disconnections. |
| 2345 | if (ice_connection_state_ == |
| 2346 | PeerConnectionInterface::kIceConnectionConnected || |
| 2347 | ice_connection_state_ == |
| 2348 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 2349 | SetIceConnectionState( |
| 2350 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 2351 | } |
| 2352 | break; |
| 2353 | case cricket::kIceConnectionFailed: |
| 2354 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 2355 | break; |
| 2356 | case cricket::kIceConnectionConnected: |
| 2357 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 2358 | "all transports are writable."; |
| 2359 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 2360 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 2361 | break; |
| 2362 | case cricket::kIceConnectionCompleted: |
| 2363 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 2364 | "all transports are complete."; |
| 2365 | if (ice_connection_state_ != |
| 2366 | PeerConnectionInterface::kIceConnectionConnected) { |
| 2367 | // If jumping directly from "checking" to "connected", |
| 2368 | // signal "connected" first. |
| 2369 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 2370 | } |
| 2371 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2372 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2373 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2374 | break; |
| 2375 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2376 | RTC_DCHECK_NOTREACHED(); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2377 | } |
| 2378 | } |
| 2379 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2380 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 2381 | const std::string& transport_name, |
| 2382 | const cricket::Candidates& candidates) { |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 2383 | // TODO(bugs.webrtc.org/12427): Expect this to come in on the network thread |
| 2384 | // (not signaling as it currently does), handle appropriately. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2385 | int sdp_mline_index; |
| 2386 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2387 | RTC_LOG(LS_ERROR) |
| 2388 | << "OnTransportControllerCandidatesGathered: content name " |
| 2389 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2390 | return; |
| 2391 | } |
| 2392 | |
| 2393 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 2394 | citer != candidates.end(); ++citer) { |
| 2395 | // Use transport_name as the candidate media id. |
| 2396 | std::unique_ptr<JsepIceCandidate> candidate( |
| 2397 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2398 | sdp_handler_->AddLocalIceCandidate(candidate.get()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2399 | OnIceCandidate(std::move(candidate)); |
| 2400 | } |
| 2401 | } |
| 2402 | |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2403 | void PeerConnection::OnTransportControllerCandidateError( |
| 2404 | const cricket::IceCandidateErrorEvent& event) { |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2405 | OnIceCandidateError(event.address, event.port, event.url, event.error_code, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2406 | event.error_text); |
| 2407 | } |
| 2408 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2409 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 2410 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2411 | // Sanity check. |
| 2412 | for (const cricket::Candidate& candidate : candidates) { |
| 2413 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2414 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2415 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2416 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2417 | return; |
| 2418 | } |
| 2419 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2420 | sdp_handler_->RemoveLocalIceCandidates(candidates); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2421 | OnIceCandidatesRemoved(candidates); |
| 2422 | } |
| 2423 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2424 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 2425 | const cricket::CandidatePairChangeEvent& event) { |
| 2426 | OnSelectedCandidatePairChanged(event); |
| 2427 | } |
| 2428 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2429 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 2430 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2431 | RTC_HISTOGRAM_ENUMERATION( |
| 2432 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 2433 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2434 | } |
| 2435 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2436 | // Returns the media index for a local ice candidate given the content name. |
| 2437 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 2438 | const std::string& content_name, |
| 2439 | int* sdp_mline_index) { |
| 2440 | if (!local_description() || !sdp_mline_index) { |
| 2441 | return false; |
| 2442 | } |
| 2443 | |
| 2444 | bool content_found = false; |
| 2445 | const ContentInfos& contents = local_description()->description()->contents(); |
| 2446 | for (size_t index = 0; index < contents.size(); ++index) { |
| 2447 | if (contents[index].name == content_name) { |
| 2448 | *sdp_mline_index = static_cast<int>(index); |
| 2449 | content_found = true; |
| 2450 | break; |
| 2451 | } |
| 2452 | } |
| 2453 | return content_found; |
| 2454 | } |
| 2455 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2456 | Call::Stats PeerConnection::GetCallStats() { |
| 2457 | if (!worker_thread()->IsCurrent()) { |
| 2458 | return worker_thread()->Invoke<Call::Stats>( |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 2459 | RTC_FROM_HERE, [this] { return GetCallStats(); }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2460 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2461 | RTC_DCHECK_RUN_ON(worker_thread()); |
Henrik Boström | e88c95e | 2020-07-08 09:18:50 | [diff] [blame] | 2462 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2463 | if (call_) { |
| 2464 | return call_->GetStats(); |
| 2465 | } else { |
| 2466 | return Call::Stats(); |
| 2467 | } |
| 2468 | } |
| 2469 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2470 | bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2471 | DataChannelTransportInterface* transport = |
| 2472 | transport_controller_->GetDataChannelTransport(mid); |
| 2473 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 2474 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2475 | << "Data channel transport is not available for data channels, mid=" |
| 2476 | << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2477 | return false; |
| 2478 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2479 | RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2480 | |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2481 | data_channel_controller_.set_data_channel_transport(transport); |
| 2482 | data_channel_controller_.SetupDataChannelTransport_n(); |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 2483 | sctp_mid_n_ = mid; |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2484 | cricket::DtlsTransportInternal* dtls_transport = |
| 2485 | transport_controller_->GetDtlsTransport(mid); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2486 | if (dtls_transport) { |
| 2487 | signaling_thread()->PostTask( |
| 2488 | ToQueuedTask(signaling_thread_safety_.flag(), |
| 2489 | [this, name = dtls_transport->transport_name()] { |
| 2490 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2491 | sctp_transport_name_s_ = std::move(name); |
| 2492 | })); |
| 2493 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2494 | |
| 2495 | // Note: setting the data sink and checking initial state must be done last, |
| 2496 | // after setting up the data channel. Setting the data sink may trigger |
| 2497 | // callbacks to PeerConnection which require the transport to be completely |
| 2498 | // set up (eg. OnReadyToSend()). |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2499 | transport->SetDataSink(&data_channel_controller_); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2500 | return true; |
| 2501 | } |
| 2502 | |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 2503 | void PeerConnection::TeardownDataChannelTransport_n() { |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 2504 | if (sctp_mid_n_) { |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 2505 | // `sctp_mid_` may still be active through an SCTP transport. If not, unset |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 2506 | // it. |
| 2507 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
| 2508 | << *sctp_mid_n_; |
| 2509 | sctp_mid_n_.reset(); |
| 2510 | } |
| 2511 | |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2512 | data_channel_controller_.TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2513 | } |
| 2514 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2515 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
Henrik Boström | f8187e0 | 2021-04-26 19:04:26 | [diff] [blame] | 2516 | bool PeerConnection::ValidateBundleSettings( |
| 2517 | const SessionDescription* desc, |
| 2518 | const std::map<std::string, const cricket::ContentGroup*>& |
| 2519 | bundle_groups_by_mid) { |
| 2520 | if (bundle_groups_by_mid.empty()) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2521 | return true; |
| 2522 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2523 | const cricket::ContentInfos& contents = desc->contents(); |
| 2524 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 2525 | citer != contents.end(); ++citer) { |
| 2526 | const cricket::ContentInfo* content = (&*citer); |
| 2527 | RTC_DCHECK(content != NULL); |
Henrik Boström | f8187e0 | 2021-04-26 19:04:26 | [diff] [blame] | 2528 | auto it = bundle_groups_by_mid.find(content->name); |
| 2529 | if (it != bundle_groups_by_mid.end() && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 2530 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2531 | if (!HasRtcpMuxEnabled(content)) |
| 2532 | return false; |
| 2533 | } |
| 2534 | } |
| 2535 | // RTCP-MUX is enabled in all the contents. |
| 2536 | return true; |
| 2537 | } |
| 2538 | |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 2539 | void PeerConnection::ReportSdpFormatReceived( |
Philipp Hancke | 844c759 | 2021-01-18 10:25:31 | [diff] [blame] | 2540 | const SessionDescriptionInterface& remote_description) { |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 2541 | int num_audio_mlines = 0; |
| 2542 | int num_video_mlines = 0; |
| 2543 | int num_audio_tracks = 0; |
| 2544 | int num_video_tracks = 0; |
Philipp Hancke | 844c759 | 2021-01-18 10:25:31 | [diff] [blame] | 2545 | for (const ContentInfo& content : |
| 2546 | remote_description.description()->contents()) { |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 2547 | cricket::MediaType media_type = content.media_description()->type(); |
| 2548 | int num_tracks = std::max( |
| 2549 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 2550 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 2551 | num_audio_mlines += 1; |
| 2552 | num_audio_tracks += num_tracks; |
| 2553 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 2554 | num_video_mlines += 1; |
| 2555 | num_video_tracks += num_tracks; |
| 2556 | } |
| 2557 | } |
| 2558 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 2559 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 2560 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 2561 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 2562 | format = kSdpFormatReceivedComplexPlanB; |
| 2563 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 2564 | format = kSdpFormatReceivedSimple; |
| 2565 | } |
Philipp Hancke | 844c759 | 2021-01-18 10:25:31 | [diff] [blame] | 2566 | switch (remote_description.GetType()) { |
Philipp Hancke | b8ca2a1 | 2020-10-07 10:47:10 | [diff] [blame] | 2567 | case SdpType::kOffer: |
| 2568 | // Historically only offers were counted. |
| 2569 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", |
| 2570 | format, kSdpFormatReceivedMax); |
| 2571 | break; |
| 2572 | case SdpType::kAnswer: |
| 2573 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceivedAnswer", |
| 2574 | format, kSdpFormatReceivedMax); |
| 2575 | break; |
| 2576 | default: |
| 2577 | RTC_LOG(LS_ERROR) << "Can not report SdpFormatReceived for " |
Philipp Hancke | 844c759 | 2021-01-18 10:25:31 | [diff] [blame] | 2578 | << SdpTypeToString(remote_description.GetType()); |
Philipp Hancke | b8ca2a1 | 2020-10-07 10:47:10 | [diff] [blame] | 2579 | break; |
| 2580 | } |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 2581 | } |
| 2582 | |
Philipp Hancke | 54b925c | 2021-01-28 08:56:39 | [diff] [blame] | 2583 | void PeerConnection::ReportSdpBundleUsage( |
| 2584 | const SessionDescriptionInterface& remote_description) { |
| 2585 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2586 | |
| 2587 | bool using_bundle = |
| 2588 | remote_description.description()->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 2589 | int num_audio_mlines = 0; |
| 2590 | int num_video_mlines = 0; |
| 2591 | int num_data_mlines = 0; |
| 2592 | for (const ContentInfo& content : |
| 2593 | remote_description.description()->contents()) { |
| 2594 | cricket::MediaType media_type = content.media_description()->type(); |
| 2595 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 2596 | num_audio_mlines += 1; |
| 2597 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 2598 | num_video_mlines += 1; |
| 2599 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
| 2600 | num_data_mlines += 1; |
| 2601 | } |
| 2602 | } |
| 2603 | bool simple = num_audio_mlines <= 1 && num_video_mlines <= 1; |
| 2604 | BundleUsage usage = kBundleUsageMax; |
| 2605 | if (num_audio_mlines == 0 && num_video_mlines == 0) { |
| 2606 | if (num_data_mlines > 0) { |
| 2607 | usage = using_bundle ? kBundleUsageBundleDatachannelOnly |
| 2608 | : kBundleUsageNoBundleDatachannelOnly; |
| 2609 | } else { |
| 2610 | usage = kBundleUsageEmpty; |
| 2611 | } |
Harald Alvestrand | fa67aef | 2021-12-08 14:30:55 | [diff] [blame] | 2612 | } else if (configuration_.sdp_semantics == SdpSemantics::kPlanB_DEPRECATED) { |
Philipp Hancke | 54b925c | 2021-01-28 08:56:39 | [diff] [blame] | 2613 | // In plan-b, simple/complex usage will not show up in the number of |
| 2614 | // m-lines or BUNDLE. |
| 2615 | usage = using_bundle ? kBundleUsageBundlePlanB : kBundleUsageNoBundlePlanB; |
| 2616 | } else { |
| 2617 | if (simple) { |
| 2618 | usage = |
| 2619 | using_bundle ? kBundleUsageBundleSimple : kBundleUsageNoBundleSimple; |
| 2620 | } else { |
| 2621 | usage = using_bundle ? kBundleUsageBundleComplex |
| 2622 | : kBundleUsageNoBundleComplex; |
| 2623 | } |
| 2624 | } |
| 2625 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.BundleUsage", usage, |
| 2626 | kBundleUsageMax); |
| 2627 | } |
| 2628 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2629 | void PeerConnection::ReportIceCandidateCollected( |
| 2630 | const cricket::Candidate& candidate) { |
| 2631 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 2632 | if (candidate.address().IsPrivateIP()) { |
| 2633 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 2634 | } |
| 2635 | if (candidate.address().IsUnresolvedIP()) { |
| 2636 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 2637 | } |
| 2638 | if (candidate.address().family() == AF_INET6) { |
| 2639 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 2640 | } |
| 2641 | } |
| 2642 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2643 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 2644 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2645 | usage_pattern_.NoteUsageEvent(event); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2646 | } |
| 2647 | |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2648 | // Asynchronously adds remote candidates on the network thread. |
| 2649 | void PeerConnection::AddRemoteCandidate(const std::string& mid, |
| 2650 | const cricket::Candidate& candidate) { |
| 2651 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2652 | |
| 2653 | network_thread()->PostTask(ToQueuedTask( |
| 2654 | network_thread_safety_, [this, mid = mid, candidate = candidate] { |
| 2655 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2656 | std::vector<cricket::Candidate> candidates = {candidate}; |
| 2657 | RTCError error = |
| 2658 | transport_controller_->AddRemoteCandidates(mid, candidates); |
| 2659 | if (error.ok()) { |
| 2660 | signaling_thread()->PostTask(ToQueuedTask( |
| 2661 | signaling_thread_safety_.flag(), |
| 2662 | [this, candidate = std::move(candidate)] { |
| 2663 | ReportRemoteIceCandidateAdded(candidate); |
| 2664 | // Candidates successfully submitted for checking. |
| 2665 | if (ice_connection_state() == |
| 2666 | PeerConnectionInterface::kIceConnectionNew || |
| 2667 | ice_connection_state() == |
| 2668 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 2669 | // If state is New, then the session has just gotten its first |
| 2670 | // remote ICE candidates, so go to Checking. If state is |
| 2671 | // Disconnected, the session is re-using old candidates or |
| 2672 | // receiving additional ones, so go to Checking. If state is |
| 2673 | // Connected, stay Connected. |
| 2674 | // TODO(bemasc): If state is Connected, and the new candidates |
| 2675 | // are for a newly added transport, then the state actually |
| 2676 | // _should_ move to checking. Add a way to distinguish that |
| 2677 | // case. |
| 2678 | SetIceConnectionState( |
| 2679 | PeerConnectionInterface::kIceConnectionChecking); |
| 2680 | } |
| 2681 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 2682 | })); |
| 2683 | } else { |
| 2684 | RTC_LOG(LS_WARNING) << error.message(); |
| 2685 | } |
| 2686 | })); |
| 2687 | } |
| 2688 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2689 | void PeerConnection::ReportUsagePattern() const { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2690 | usage_pattern_.ReportUsagePattern(observer_); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2691 | } |
| 2692 | |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2693 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 2694 | const cricket::Candidate& candidate) { |
| 2695 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2696 | |
| 2697 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 2698 | |
| 2699 | if (candidate.address().IsPrivateIP()) { |
| 2700 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 2701 | } |
| 2702 | if (candidate.address().IsUnresolvedIP()) { |
| 2703 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 2704 | } |
| 2705 | if (candidate.address().family() == AF_INET6) { |
| 2706 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 2707 | } |
| 2708 | } |
| 2709 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2710 | bool PeerConnection::SrtpRequired() const { |
Tomas Gunnarsson | 3278a71 | 2021-03-30 15:23:42 | [diff] [blame] | 2711 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 | [diff] [blame] | 2712 | return (dtls_enabled_ || |
| 2713 | sdp_handler_->webrtc_session_desc_factory()->SdesPolicy() == |
| 2714 | cricket::SEC_REQUIRED); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | void PeerConnection::OnTransportControllerGatheringState( |
| 2718 | cricket::IceGatheringState state) { |
| 2719 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 2720 | if (state == cricket::kIceGatheringGathering) { |
| 2721 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 2722 | } else if (state == cricket::kIceGatheringComplete) { |
| 2723 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
Harald Alvestrand | bedb605 | 2020-08-20 12:50:10 | [diff] [blame] | 2724 | } else if (state == cricket::kIceGatheringNew) { |
| 2725 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringNew); |
| 2726 | } else { |
| 2727 | RTC_LOG(LS_ERROR) << "Unknown state received: " << state; |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2728 | RTC_DCHECK_NOTREACHED(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2732 | // Runs on network_thread(). |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2733 | void PeerConnection::ReportTransportStats() { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 2734 | TRACE_EVENT0("webrtc", "PeerConnection::ReportTransportStats"); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2735 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2736 | std::map<std::string, std::set<cricket::MediaType>> |
| 2737 | media_types_by_transport_name; |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 2738 | for (const auto& transceiver : rtp_manager()->transceivers()->UnsafeList()) { |
| 2739 | if (transceiver->internal()->channel()) { |
| 2740 | std::string transport_name( |
| 2741 | transceiver->internal()->channel()->transport_name()); |
| 2742 | media_types_by_transport_name[transport_name].insert( |
| 2743 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2744 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2745 | } |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2746 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2747 | if (sctp_mid_n_) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2748 | cricket::DtlsTransportInternal* dtls_transport = |
| 2749 | transport_controller_->GetDtlsTransport(*sctp_mid_n_); |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2750 | if (dtls_transport) { |
| 2751 | media_types_by_transport_name[dtls_transport->transport_name()].insert( |
| 2752 | cricket::MEDIA_TYPE_DATA); |
| 2753 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2754 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2755 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2756 | for (const auto& entry : media_types_by_transport_name) { |
| 2757 | const std::string& transport_name = entry.first; |
| 2758 | const std::set<cricket::MediaType> media_types = entry.second; |
| 2759 | cricket::TransportStats stats; |
| 2760 | if (transport_controller_->GetStats(transport_name, &stats)) { |
| 2761 | ReportBestConnectionState(stats); |
| 2762 | ReportNegotiatedCiphers(dtls_enabled_, stats, media_types); |
| 2763 | } |
| 2764 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2765 | } |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2766 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2767 | // Walk through the ConnectionInfos to gather best connection usage |
| 2768 | // for IPv4 and IPv6. |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2769 | // static (no member state required) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2770 | void PeerConnection::ReportBestConnectionState( |
| 2771 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2772 | for (const cricket::TransportChannelStats& channel_stats : |
| 2773 | stats.channel_stats) { |
| 2774 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 06:10:27 | [diff] [blame] | 2775 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2776 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2777 | continue; |
| 2778 | } |
| 2779 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2780 | const cricket::Candidate& local = connection_info.local_candidate; |
| 2781 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2782 | |
| 2783 | // Increment the counter for IceCandidatePairType. |
| 2784 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 2785 | (local.type() == RELAY_PORT_TYPE && |
| 2786 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2787 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 2788 | GetIceCandidatePairCounter(local, remote), |
| 2789 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2790 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2791 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 2792 | GetIceCandidatePairCounter(local, remote), |
| 2793 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2794 | } else { |
Karl Wiberg | c95b939 | 2020-11-07 23:49:37 | [diff] [blame] | 2795 | RTC_CHECK_NOTREACHED(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2796 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2797 | |
| 2798 | // Increment the counter for IP type. |
| 2799 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2800 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 2801 | kBestConnections_IPv4, |
| 2802 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2803 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2804 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 2805 | kBestConnections_IPv6, |
| 2806 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2807 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 2808 | RTC_CHECK(!local.address().hostname().empty() && |
| 2809 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | return; |
| 2813 | } |
| 2814 | } |
| 2815 | } |
| 2816 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2817 | // static |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2818 | void PeerConnection::ReportNegotiatedCiphers( |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2819 | bool dtls_enabled, |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2820 | const cricket::TransportStats& stats, |
| 2821 | const std::set<cricket::MediaType>& media_types) { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2822 | if (!dtls_enabled || stats.channel_stats.empty()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2823 | return; |
| 2824 | } |
| 2825 | |
| 2826 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 2827 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2828 | if (srtp_crypto_suite == rtc::kSrtpInvalidCryptoSuite && |
| 2829 | ssl_cipher_suite == rtc::kTlsNullWithNullNull) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2830 | return; |
| 2831 | } |
| 2832 | |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2833 | if (srtp_crypto_suite != rtc::kSrtpInvalidCryptoSuite) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2834 | for (cricket::MediaType media_type : media_types) { |
| 2835 | switch (media_type) { |
| 2836 | case cricket::MEDIA_TYPE_AUDIO: |
| 2837 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2838 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2839 | rtc::kSrtpCryptoSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2840 | break; |
| 2841 | case cricket::MEDIA_TYPE_VIDEO: |
| 2842 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2843 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2844 | rtc::kSrtpCryptoSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2845 | break; |
| 2846 | case cricket::MEDIA_TYPE_DATA: |
| 2847 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2848 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2849 | rtc::kSrtpCryptoSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2850 | break; |
| 2851 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2852 | RTC_DCHECK_NOTREACHED(); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2853 | continue; |
| 2854 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2855 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2856 | } |
| 2857 | |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2858 | if (ssl_cipher_suite != rtc::kTlsNullWithNullNull) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2859 | for (cricket::MediaType media_type : media_types) { |
| 2860 | switch (media_type) { |
| 2861 | case cricket::MEDIA_TYPE_AUDIO: |
| 2862 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2863 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2864 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2865 | break; |
| 2866 | case cricket::MEDIA_TYPE_VIDEO: |
| 2867 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2868 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2869 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2870 | break; |
| 2871 | case cricket::MEDIA_TYPE_DATA: |
| 2872 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2873 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2874 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2875 | break; |
| 2876 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2877 | RTC_DCHECK_NOTREACHED(); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2878 | continue; |
| 2879 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2880 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2884 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2885 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2886 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 2887 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2888 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 12:08:37 | [diff] [blame] | 2889 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2890 | bool ret = true; |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 2891 | for (const auto& transceiver : rtp_manager()->transceivers()->UnsafeList()) { |
| 2892 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2893 | if (channel && channel->mid() == mid) { |
| 2894 | ret = channel->SetRtpTransport(rtp_transport); |
Harald Alvestrand | 19ebabc | 2022-04-28 13:31:17 | [diff] [blame] | 2895 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2896 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2897 | |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 2898 | if (mid == sctp_mid_n_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2899 | data_channel_controller_.OnTransportChanged(data_channel_transport); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2900 | if (dtls_transport) { |
| 2901 | signaling_thread()->PostTask(ToQueuedTask( |
| 2902 | signaling_thread_safety_.flag(), |
Tomas Gunnarsson | 94f0194 | 2022-01-03 14:59:12 | [diff] [blame] | 2903 | [this, |
| 2904 | name = std::string(dtls_transport->internal()->transport_name())] { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2905 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2906 | sctp_transport_name_s_ = std::move(name); |
| 2907 | })); |
| 2908 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2909 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2910 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2911 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2912 | } |
| 2913 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2914 | PeerConnectionObserver* PeerConnection::Observer() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 2915 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2916 | RTC_DCHECK(observer_); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2917 | return observer_; |
| 2918 | } |
| 2919 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2920 | void PeerConnection::StartSctpTransport(int local_port, |
| 2921 | int remote_port, |
| 2922 | int max_message_size) { |
| 2923 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2924 | if (!sctp_mid_s_) |
| 2925 | return; |
| 2926 | |
| 2927 | network_thread()->PostTask(ToQueuedTask( |
| 2928 | network_thread_safety_, |
| 2929 | [this, mid = *sctp_mid_s_, local_port, remote_port, max_message_size] { |
| 2930 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2931 | transport_controller_n()->GetSctpTransport(mid); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2932 | if (sctp_transport) |
| 2933 | sctp_transport->Start(local_port, remote_port, max_message_size); |
| 2934 | })); |
| 2935 | } |
| 2936 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2937 | CryptoOptions PeerConnection::GetCryptoOptions() { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 2938 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2939 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 2940 | // after it has been removed. |
| 2941 | return configuration_.crypto_options.has_value() |
| 2942 | ? *configuration_.crypto_options |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 2943 | : options_.crypto_options; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2944 | } |
| 2945 | |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 2946 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2947 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 2948 | if (stats_collector_) { |
| 2949 | stats_collector_->ClearCachedStatsReport(); |
| 2950 | } |
| 2951 | } |
| 2952 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 2953 | bool PeerConnection::ShouldFireNegotiationNeededEvent(uint32_t event_id) { |
| 2954 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2955 | return sdp_handler_->ShouldFireNegotiationNeededEvent(event_id); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2956 | } |
| 2957 | |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2958 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Harald Alvestrand | 00c62ed | 2021-10-20 08:52:12 | [diff] [blame] | 2959 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2960 | message_handler_.RequestUsagePatternReport( |
| 2961 | [this]() { |
| 2962 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2963 | ReportUsagePattern(); |
| 2964 | }, |
| 2965 | /* delay_ms= */ 0); |
| 2966 | } |
| 2967 | |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 2968 | std::function<void(const rtc::CopyOnWriteBuffer& packet, |
| 2969 | int64_t packet_time_us)> |
| 2970 | PeerConnection::InitializeRtcpCallback() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2971 | RTC_DCHECK_RUN_ON(network_thread()); |
Tommi | cae1f1d | 2021-05-31 08:51:09 | [diff] [blame] | 2972 | return [this](const rtc::CopyOnWriteBuffer& packet, int64_t packet_time_us) { |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 2973 | RTC_DCHECK_RUN_ON(network_thread()); |
Tommi | cae1f1d | 2021-05-31 08:51:09 | [diff] [blame] | 2974 | call_ptr_->Receiver()->DeliverPacket(MediaType::ANY, packet, |
| 2975 | packet_time_us); |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 2976 | }; |
| 2977 | } |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2978 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2979 | } // namespace webrtc |