henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/peerconnection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 14 | #include <utility> |
| 15 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/jsepicecandidate.h" |
| 18 | #include "api/jsepsessiondescription.h" |
| 19 | #include "api/mediaconstraintsinterface.h" |
| 20 | #include "api/mediastreamproxy.h" |
| 21 | #include "api/mediastreamtrackproxy.h" |
| 22 | #include "call/call.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 23 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 25 | #include "media/sctp/sctptransport.h" |
| 26 | #include "pc/audiotrack.h" |
| 27 | #include "pc/channelmanager.h" |
| 28 | #include "pc/dtmfsender.h" |
| 29 | #include "pc/mediastream.h" |
| 30 | #include "pc/mediastreamobserver.h" |
| 31 | #include "pc/remoteaudiosource.h" |
| 32 | #include "pc/rtpreceiver.h" |
| 33 | #include "pc/rtpsender.h" |
| 34 | #include "pc/streamcollection.h" |
| 35 | #include "pc/videocapturertracksource.h" |
| 36 | #include "pc/videotrack.h" |
| 37 | #include "rtc_base/bind.h" |
| 38 | #include "rtc_base/checks.h" |
| 39 | #include "rtc_base/logging.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 40 | #include "rtc_base/ptr_util.h" |
| 41 | #include "rtc_base/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 42 | #include "rtc_base/stringencode.h" |
| 43 | #include "rtc_base/stringutils.h" |
| 44 | #include "rtc_base/trace_event.h" |
| 45 | #include "system_wrappers/include/clock.h" |
| 46 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | |
| 48 | namespace { |
| 49 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 50 | using webrtc::DataChannel; |
| 51 | using webrtc::MediaConstraintsInterface; |
| 52 | using webrtc::MediaStreamInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | using webrtc::PeerConnectionInterface; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 54 | using webrtc::RTCError; |
| 55 | using webrtc::RTCErrorType; |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 56 | using webrtc::RtpSenderInternal; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 57 | using webrtc::RtpSenderInterface; |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 58 | using webrtc::RtpSenderProxy; |
| 59 | using webrtc::RtpSenderProxyWithInternal; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 60 | using webrtc::StreamCollection; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 62 | static const char kDefaultStreamLabel[] = "default"; |
| 63 | static const char kDefaultAudioTrackLabel[] = "defaulta0"; |
| 64 | static const char kDefaultVideoTrackLabel[] = "defaultv0"; |
| 65 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 66 | // The length of RTCP CNAMEs. |
| 67 | static const int kRtcpCnameLength = 16; |
| 68 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 70 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 72 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 74 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 77 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | explicit SetSessionDescriptionMsg( |
| 79 | webrtc::SetSessionDescriptionObserver* observer) |
| 80 | : observer(observer) { |
| 81 | } |
| 82 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 83 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | std::string error; |
| 85 | }; |
| 86 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 87 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 88 | explicit CreateSessionDescriptionMsg( |
| 89 | webrtc::CreateSessionDescriptionObserver* observer) |
| 90 | : observer(observer) {} |
| 91 | |
| 92 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 93 | std::string error; |
| 94 | }; |
| 95 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 96 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 97 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 98 | webrtc::MediaStreamTrackInterface* track) |
| 99 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 101 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 102 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 105 | // Check if we can send |new_stream| on a PeerConnection. |
| 106 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 107 | webrtc::MediaStreamInterface* new_stream) { |
| 108 | if (!new_stream || !current_streams) { |
| 109 | return false; |
| 110 | } |
| 111 | if (current_streams->find(new_stream->label()) != nullptr) { |
| 112 | LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 113 | << " is already added."; |
| 114 | return false; |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool MediaContentDirectionHasSend(cricket::MediaContentDirection dir) { |
| 120 | return dir == cricket::MD_SENDONLY || dir == cricket::MD_SENDRECV; |
| 121 | } |
| 122 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 123 | // If the direction is "recvonly" or "inactive", treat the description |
| 124 | // as containing no streams. |
| 125 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 126 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 127 | const cricket::MediaContentDescription* desc) { |
| 128 | return MediaContentDirectionHasSend(desc->direction()) |
| 129 | ? desc->streams() |
| 130 | : std::vector<cricket::StreamParams>(); |
| 131 | } |
| 132 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 133 | bool IsValidOfferToReceiveMedia(int value) { |
| 134 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 135 | return (value >= Options::kUndefined) && |
| 136 | (value <= Options::kMaxOfferToReceiveMedia); |
| 137 | } |
| 138 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 139 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 140 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 141 | const std::vector<rtc::scoped_refptr< |
| 142 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 143 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 144 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 145 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 146 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 147 | if (audio_media_description_options) { |
| 148 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 149 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 150 | } |
| 151 | } else { |
| 152 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 153 | if (video_media_description_options) { |
| 154 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 155 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 158 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 159 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 160 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 161 | // Add options to |session_options| from |rtp_data_channels|. |
| 162 | void AddRtpDataChannelOptions( |
| 163 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 164 | rtp_data_channels, |
| 165 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 166 | if (!data_media_description_options) { |
| 167 | return; |
| 168 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 169 | // Check for data channels. |
| 170 | for (const auto& kv : rtp_data_channels) { |
| 171 | const DataChannel* channel = kv.second; |
| 172 | if (channel->state() == DataChannel::kConnecting || |
| 173 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 174 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 175 | // the data channel's label. |
| 176 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 177 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 182 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 183 | PeerConnectionInterface::IceTransportsType type) { |
| 184 | switch (type) { |
| 185 | case PeerConnectionInterface::kNone: |
| 186 | return cricket::CF_NONE; |
| 187 | case PeerConnectionInterface::kRelay: |
| 188 | return cricket::CF_RELAY; |
| 189 | case PeerConnectionInterface::kNoHost: |
| 190 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 191 | case PeerConnectionInterface::kAll: |
| 192 | return cricket::CF_ALL; |
| 193 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 194 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 195 | } |
| 196 | return cricket::CF_NONE; |
| 197 | } |
| 198 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 199 | // Helper method to set a voice/video channel on all applicable senders |
| 200 | // and receivers when one is created/destroyed by WebRtcSession. |
| 201 | // |
| 202 | // Used by On(Voice|Video)Channel(Created|Destroyed) |
| 203 | template <class SENDER, |
| 204 | class RECEIVER, |
| 205 | class CHANNEL, |
| 206 | class SENDERS, |
| 207 | class RECEIVERS> |
| 208 | void SetChannelOnSendersAndReceivers(CHANNEL* channel, |
| 209 | SENDERS& senders, |
| 210 | RECEIVERS& receivers, |
| 211 | cricket::MediaType media_type) { |
| 212 | for (auto& sender : senders) { |
| 213 | if (sender->media_type() == media_type) { |
| 214 | static_cast<SENDER*>(sender->internal())->SetChannel(channel); |
| 215 | } |
| 216 | } |
| 217 | for (auto& receiver : receivers) { |
| 218 | if (receiver->media_type() == media_type) { |
| 219 | if (!channel) { |
| 220 | receiver->internal()->Stop(); |
| 221 | } |
| 222 | static_cast<RECEIVER*>(receiver->internal())->SetChannel(channel); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 227 | // Helper to set an error and return from a method. |
| 228 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 229 | if (error) { |
| 230 | error->set_type(type); |
| 231 | } |
| 232 | return type == webrtc::RTCErrorType::NONE; |
| 233 | } |
| 234 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 235 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 236 | if (error_out) { |
| 237 | *error_out = std::move(error); |
| 238 | } |
| 239 | return error.ok(); |
| 240 | } |
| 241 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 242 | } // namespace |
| 243 | |
| 244 | namespace webrtc { |
| 245 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 246 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 247 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 248 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 249 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 250 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 251 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 252 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 253 | BundlePolicy bundle_policy; |
| 254 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 255 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 256 | int ice_candidate_pool_size; |
| 257 | bool disable_ipv6; |
| 258 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 259 | int max_ipv6_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 260 | bool enable_rtp_data_channel; |
| 261 | rtc::Optional<int> screencast_min_bitrate; |
| 262 | rtc::Optional<bool> combined_audio_video_bwe; |
| 263 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 264 | TcpCandidatePolicy tcp_candidate_policy; |
| 265 | CandidateNetworkPolicy candidate_network_policy; |
| 266 | int audio_jitter_buffer_max_packets; |
| 267 | bool audio_jitter_buffer_fast_accelerate; |
| 268 | int ice_connection_receiving_timeout; |
| 269 | int ice_backup_candidate_pair_ping_interval; |
| 270 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 271 | bool prioritize_most_likely_ice_candidate_pairs; |
| 272 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 273 | bool enable_quic; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 274 | bool prune_turn_ports; |
| 275 | bool presume_writable_when_fully_relayed; |
| 276 | bool enable_ice_renomination; |
| 277 | bool redetermine_role_on_ice_restart; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 278 | rtc::Optional<int> ice_check_min_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 279 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 280 | webrtc::TurnCustomizer* turn_customizer; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 281 | }; |
| 282 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 283 | "Did you add something to RTCConfiguration and forget to " |
| 284 | "update operator==?"); |
| 285 | return type == o.type && servers == o.servers && |
| 286 | bundle_policy == o.bundle_policy && |
| 287 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 288 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 289 | candidate_network_policy == o.candidate_network_policy && |
| 290 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 291 | audio_jitter_buffer_fast_accelerate == |
| 292 | o.audio_jitter_buffer_fast_accelerate && |
| 293 | ice_connection_receiving_timeout == |
| 294 | o.ice_connection_receiving_timeout && |
| 295 | ice_backup_candidate_pair_ping_interval == |
| 296 | o.ice_backup_candidate_pair_ping_interval && |
| 297 | continual_gathering_policy == o.continual_gathering_policy && |
| 298 | certificates == o.certificates && |
| 299 | prioritize_most_likely_ice_candidate_pairs == |
| 300 | o.prioritize_most_likely_ice_candidate_pairs && |
| 301 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 302 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 303 | max_ipv6_networks == o.max_ipv6_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 304 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
| 305 | enable_quic == o.enable_quic && |
| 306 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 307 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 308 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 309 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 310 | prune_turn_ports == o.prune_turn_ports && |
| 311 | presume_writable_when_fully_relayed == |
| 312 | o.presume_writable_when_fully_relayed && |
| 313 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 314 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 315 | ice_check_min_interval == o.ice_check_min_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 316 | ice_regather_interval_range == o.ice_regather_interval_range && |
| 317 | turn_customizer == o.turn_customizer; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 321 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 322 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 323 | } |
| 324 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 325 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 326 | std::string GenerateRtcpCname() { |
| 327 | std::string cname; |
| 328 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
| 329 | LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 330 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 331 | } |
| 332 | return cname; |
| 333 | } |
| 334 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 335 | bool ValidateOfferAnswerOptions( |
| 336 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 337 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 338 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 339 | } |
| 340 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 341 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 342 | // m= sections (in other words, nothing that involves a map/array). |
| 343 | void ExtractSharedMediaSessionOptions( |
| 344 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 345 | cricket::MediaSessionOptions* session_options) { |
| 346 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 347 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 348 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 349 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 350 | bool ConvertConstraintsToOfferAnswerOptions( |
| 351 | const MediaConstraintsInterface* constraints, |
| 352 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 353 | if (!constraints) { |
| 354 | return true; |
| 355 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 356 | |
| 357 | bool value = false; |
| 358 | size_t mandatory_constraints_satisfied = 0; |
| 359 | |
| 360 | if (FindConstraint(constraints, |
| 361 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 362 | &mandatory_constraints_satisfied)) { |
| 363 | offer_answer_options->offer_to_receive_audio = |
| 364 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 365 | kOfferToReceiveMediaTrue |
| 366 | : 0; |
| 367 | } |
| 368 | |
| 369 | if (FindConstraint(constraints, |
| 370 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 371 | &mandatory_constraints_satisfied)) { |
| 372 | offer_answer_options->offer_to_receive_video = |
| 373 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 374 | kOfferToReceiveMediaTrue |
| 375 | : 0; |
| 376 | } |
| 377 | if (FindConstraint(constraints, |
| 378 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 379 | &mandatory_constraints_satisfied)) { |
| 380 | offer_answer_options->voice_activity_detection = value; |
| 381 | } |
| 382 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 383 | &mandatory_constraints_satisfied)) { |
| 384 | offer_answer_options->use_rtp_mux = value; |
| 385 | } |
| 386 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 387 | &value, &mandatory_constraints_satisfied)) { |
| 388 | offer_answer_options->ice_restart = value; |
| 389 | } |
| 390 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 391 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 392 | } |
| 393 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 394 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 395 | std::unique_ptr<RtcEventLog> event_log, |
| 396 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 397 | : factory_(factory), |
| 398 | observer_(NULL), |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 399 | uma_observer_(NULL), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 400 | event_log_(std::move(event_log)), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 401 | signaling_state_(kStable), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 402 | ice_connection_state_(kIceConnectionNew), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 403 | ice_gathering_state_(kIceGatheringNew), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 404 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 405 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 406 | remote_streams_(StreamCollection::Create()), |
| 407 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 408 | |
| 409 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 410 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 411 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 412 | // Need to detach RTP senders/receivers from WebRtcSession, |
| 413 | // since it's about to be destroyed. |
| 414 | for (const auto& sender : senders_) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 415 | sender->internal()->Stop(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 416 | } |
| 417 | for (const auto& receiver : receivers_) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 418 | receiver->internal()->Stop(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 419 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 420 | // Destroy stats_ because it depends on session_. |
| 421 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 422 | if (stats_collector_) { |
| 423 | stats_collector_->WaitForPendingRequest(); |
| 424 | stats_collector_ = nullptr; |
| 425 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 426 | // Now destroy session_ before destroying other members, |
| 427 | // because its destruction fires signals (such as VoiceChannelDestroyed) |
| 428 | // which will trigger some final actions in PeerConnection... |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 429 | owned_session_.reset(nullptr); |
| 430 | session_ = nullptr; |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 431 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 432 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 433 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 434 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 435 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 436 | call_.reset(); |
| 437 | event_log_.reset(); |
| 438 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 442 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 443 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 444 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 445 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 446 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 447 | |
| 448 | RTCError config_error = ValidateConfiguration(configuration); |
| 449 | if (!config_error.ok()) { |
| 450 | LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
| 451 | return false; |
| 452 | } |
| 453 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 454 | if (!allocator) { |
| 455 | LOG(LS_ERROR) << "PeerConnection initialized without a PortAllocator? " |
| 456 | << "This shouldn't happen if using PeerConnectionFactory."; |
| 457 | return false; |
| 458 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 459 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 460 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 461 | // TODO(deadbeef): Why do we do this? |
| 462 | LOG(LS_ERROR) << "PeerConnection initialized without a " |
| 463 | << "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 464 | return false; |
| 465 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 466 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 467 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 468 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 469 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 470 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 471 | if (!network_thread()->Invoke<bool>( |
| 472 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 473 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 474 | return false; |
| 475 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 476 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 477 | owned_session_.reset(new WebRtcSession( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 478 | call_.get(), factory_->channel_manager(), configuration.media_config, |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 479 | event_log_.get(), network_thread(), worker_thread(), signaling_thread(), |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 480 | port_allocator_.get(), |
| 481 | std::unique_ptr<cricket::TransportController>( |
Honghai Zhang | bfd398c | 2016-08-30 22:07:42 -0700 | [diff] [blame] | 482 | factory_->CreateTransportController( |
| 483 | port_allocator_.get(), |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 484 | configuration.redetermine_role_on_ice_restart)), |
| 485 | #ifdef HAVE_SCTP |
| 486 | std::unique_ptr<cricket::SctpTransportInternalFactory>( |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 487 | new cricket::SctpTransportFactory(network_thread())) |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 488 | #else |
| 489 | nullptr |
| 490 | #endif |
| 491 | )); |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 492 | session_ = owned_session_.get(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 493 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 494 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 495 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 496 | |
| 497 | // Initialize the WebRtcSession. It creates transport channels etc. |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 498 | if (!session_->Initialize(factory_->options(), std::move(cert_generator), |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 499 | configuration)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 500 | return false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 501 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 502 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 503 | // Register PeerConnection as receiver of local ice candidates. |
| 504 | // All the callbacks will be posted to the application from PeerConnection. |
| 505 | session_->RegisterIceObserver(this); |
| 506 | session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 507 | session_->SignalVoiceChannelCreated.connect( |
| 508 | this, &PeerConnection::OnVoiceChannelCreated); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 509 | session_->SignalVoiceChannelDestroyed.connect( |
| 510 | this, &PeerConnection::OnVoiceChannelDestroyed); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 511 | session_->SignalVideoChannelCreated.connect( |
| 512 | this, &PeerConnection::OnVideoChannelCreated); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 513 | session_->SignalVideoChannelDestroyed.connect( |
| 514 | this, &PeerConnection::OnVideoChannelDestroyed); |
| 515 | session_->SignalDataChannelCreated.connect( |
| 516 | this, &PeerConnection::OnDataChannelCreated); |
| 517 | session_->SignalDataChannelDestroyed.connect( |
| 518 | this, &PeerConnection::OnDataChannelDestroyed); |
| 519 | session_->SignalDataChannelOpenMessage.connect( |
| 520 | this, &PeerConnection::OnDataChannelOpenMessage); |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 521 | |
| 522 | configuration_ = configuration; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 523 | return true; |
| 524 | } |
| 525 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 526 | RTCError PeerConnection::ValidateConfiguration( |
| 527 | const RTCConfiguration& config) const { |
| 528 | if (config.ice_regather_interval_range && |
| 529 | config.continual_gathering_policy == GATHER_ONCE) { |
| 530 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 531 | "ice_regather_interval_range specified but continual " |
| 532 | "gathering policy is GATHER_ONCE"); |
| 533 | } |
| 534 | return RTCError::OK(); |
| 535 | } |
| 536 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 537 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 538 | PeerConnection::local_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 539 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 540 | } |
| 541 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 542 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 543 | PeerConnection::remote_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 544 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 545 | } |
| 546 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 547 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 548 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 549 | if (IsClosed()) { |
| 550 | return false; |
| 551 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 552 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 553 | return false; |
| 554 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 555 | |
| 556 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 557 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 558 | observer->SignalAudioTrackAdded.connect(this, |
| 559 | &PeerConnection::OnAudioTrackAdded); |
| 560 | observer->SignalAudioTrackRemoved.connect( |
| 561 | this, &PeerConnection::OnAudioTrackRemoved); |
| 562 | observer->SignalVideoTrackAdded.connect(this, |
| 563 | &PeerConnection::OnVideoTrackAdded); |
| 564 | observer->SignalVideoTrackRemoved.connect( |
| 565 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 566 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 567 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 568 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 569 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 570 | } |
| 571 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 572 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 573 | } |
| 574 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 575 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 576 | observer_->OnRenegotiationNeeded(); |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 581 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 582 | if (!IsClosed()) { |
| 583 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 584 | RemoveAudioTrack(track.get(), local_stream); |
| 585 | } |
| 586 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 587 | RemoveVideoTrack(track.get(), local_stream); |
| 588 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 589 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 590 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 591 | stream_observers_.erase( |
| 592 | std::remove_if( |
| 593 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 594 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 595 | return observer->stream()->label().compare(local_stream->label()) == |
| 596 | 0; |
| 597 | }), |
| 598 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 599 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 600 | if (IsClosed()) { |
| 601 | return; |
| 602 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 603 | observer_->OnRenegotiationNeeded(); |
| 604 | } |
| 605 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 606 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 607 | MediaStreamTrackInterface* track, |
| 608 | std::vector<MediaStreamInterface*> streams) { |
| 609 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
| 610 | if (IsClosed()) { |
| 611 | return nullptr; |
| 612 | } |
| 613 | if (streams.size() >= 2) { |
| 614 | LOG(LS_ERROR) |
| 615 | << "Adding a track with two streams is not currently supported."; |
| 616 | return nullptr; |
| 617 | } |
| 618 | // TODO(deadbeef): Support adding a track to two different senders. |
| 619 | if (FindSenderForTrack(track) != senders_.end()) { |
| 620 | LOG(LS_ERROR) << "Sender for track " << track->id() << " already exists."; |
| 621 | return nullptr; |
| 622 | } |
| 623 | |
| 624 | // TODO(deadbeef): Support adding a track to multiple streams. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 625 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 626 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 627 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 628 | signaling_thread(), |
| 629 | new AudioRtpSender(static_cast<AudioTrackInterface*>(track), |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 630 | session_->voice_channel(), stats_.get())); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 631 | if (!streams.empty()) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 632 | new_sender->internal()->set_stream_id(streams[0]->label()); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 633 | } |
| 634 | const TrackInfo* track_info = FindTrackInfo( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 635 | local_audio_tracks_, new_sender->internal()->stream_id(), track->id()); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 636 | if (track_info) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 637 | new_sender->internal()->SetSsrc(track_info->ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 638 | } |
| 639 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 640 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 641 | signaling_thread(), |
| 642 | new VideoRtpSender(static_cast<VideoTrackInterface*>(track), |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 643 | session_->video_channel())); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 644 | if (!streams.empty()) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 645 | new_sender->internal()->set_stream_id(streams[0]->label()); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 646 | } |
| 647 | const TrackInfo* track_info = FindTrackInfo( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 648 | local_video_tracks_, new_sender->internal()->stream_id(), track->id()); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 649 | if (track_info) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 650 | new_sender->internal()->SetSsrc(track_info->ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 651 | } |
| 652 | } else { |
| 653 | LOG(LS_ERROR) << "CreateSender called with invalid kind: " << track->kind(); |
| 654 | return rtc::scoped_refptr<RtpSenderInterface>(); |
| 655 | } |
| 656 | |
| 657 | senders_.push_back(new_sender); |
| 658 | observer_->OnRenegotiationNeeded(); |
| 659 | return new_sender; |
| 660 | } |
| 661 | |
| 662 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 663 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
| 664 | if (IsClosed()) { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | auto it = std::find(senders_.begin(), senders_.end(), sender); |
| 669 | if (it == senders_.end()) { |
| 670 | LOG(LS_ERROR) << "Couldn't find sender " << sender->id() << " to remove."; |
| 671 | return false; |
| 672 | } |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 673 | (*it)->internal()->Stop(); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 674 | senders_.erase(it); |
| 675 | |
| 676 | observer_->OnRenegotiationNeeded(); |
| 677 | return true; |
| 678 | } |
| 679 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 680 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 681 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 682 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 683 | if (IsClosed()) { |
| 684 | return nullptr; |
| 685 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 686 | if (!track) { |
| 687 | LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 688 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 689 | } |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 690 | auto it = FindSenderForTrack(track); |
| 691 | if (it == senders_.end()) { |
| 692 | LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
| 693 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 694 | } |
| 695 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 696 | return (*it)->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 697 | } |
| 698 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 699 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 700 | const std::string& kind, |
| 701 | const std::string& stream_id) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 702 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 703 | if (IsClosed()) { |
| 704 | return nullptr; |
| 705 | } |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 706 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 707 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 708 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 709 | signaling_thread(), |
| 710 | new AudioRtpSender(session_->voice_channel(), stats_.get())); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 711 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 712 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 713 | signaling_thread(), new VideoRtpSender(session_->video_channel())); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 714 | } else { |
| 715 | LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 716 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 717 | } |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 718 | if (!stream_id.empty()) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 719 | new_sender->internal()->set_stream_id(stream_id); |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 720 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 721 | senders_.push_back(new_sender); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 722 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 723 | } |
| 724 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 725 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 726 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 727 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
| 728 | for (const auto& sender : senders_) { |
| 729 | ret.push_back(sender.get()); |
| 730 | } |
| 731 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 735 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 736 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
| 737 | for (const auto& receiver : receivers_) { |
| 738 | ret.push_back(receiver.get()); |
| 739 | } |
| 740 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 741 | } |
| 742 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 743 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 744 | MediaStreamTrackInterface* track, |
| 745 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 746 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 747 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 748 | if (!observer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 749 | LOG(LS_ERROR) << "GetStats - observer is NULL."; |
| 750 | return false; |
| 751 | } |
| 752 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 753 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 754 | // The StatsCollector is used to tell if a track is valid because it may |
| 755 | // remember tracks that the PeerConnection previously removed. |
| 756 | if (track && !stats_->IsValidTrack(track->id())) { |
| 757 | LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 758 | << track->id(); |
| 759 | return false; |
| 760 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 761 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 762 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 763 | return true; |
| 764 | } |
| 765 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 766 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
| 767 | RTC_DCHECK(stats_collector_); |
| 768 | stats_collector_->GetStatsReport(callback); |
| 769 | } |
| 770 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 771 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 772 | return signaling_state_; |
| 773 | } |
| 774 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 775 | PeerConnectionInterface::IceConnectionState |
| 776 | PeerConnection::ice_connection_state() { |
| 777 | return ice_connection_state_; |
| 778 | } |
| 779 | |
| 780 | PeerConnectionInterface::IceGatheringState |
| 781 | PeerConnection::ice_gathering_state() { |
| 782 | return ice_gathering_state_; |
| 783 | } |
| 784 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 785 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 786 | PeerConnection::CreateDataChannel( |
| 787 | const std::string& label, |
| 788 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 789 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 790 | #ifdef HAVE_QUIC |
| 791 | if (session_->data_channel_type() == cricket::DCT_QUIC) { |
| 792 | // TODO(zhihuang): Handle case when config is NULL. |
| 793 | if (!config) { |
| 794 | LOG(LS_ERROR) << "Missing config for QUIC data channel."; |
| 795 | return nullptr; |
| 796 | } |
| 797 | // TODO(zhihuang): Allow unreliable or ordered QUIC data channels. |
| 798 | if (!config->reliable || config->ordered) { |
| 799 | LOG(LS_ERROR) << "QUIC data channel does not implement unreliable or " |
| 800 | "ordered delivery."; |
| 801 | return nullptr; |
| 802 | } |
| 803 | return session_->quic_data_transport()->CreateDataChannel(label, config); |
| 804 | } |
| 805 | #endif // HAVE_QUIC |
| 806 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 807 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 808 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 809 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 810 | if (config) { |
| 811 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 812 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 813 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 814 | InternalCreateDataChannel(label, internal_config.get())); |
| 815 | if (!channel.get()) { |
| 816 | return nullptr; |
| 817 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 818 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 819 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 820 | // the first SCTP DataChannel. |
| 821 | if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
| 822 | observer_->OnRenegotiationNeeded(); |
| 823 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 824 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 825 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 826 | } |
| 827 | |
| 828 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 829 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 830 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 831 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 832 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 833 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 834 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 835 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 836 | // |constraints| and some downstream applications might create offers with |
| 837 | // mandatory fields which would not be parsed in the helper method. For |
| 838 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 839 | // |constraints| as a mandatory field but it is not parsed. |
| 840 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 841 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 842 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 846 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 847 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 848 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 849 | if (!observer) { |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 850 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 851 | return; |
| 852 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 853 | |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 854 | if (IsClosed()) { |
| 855 | std::string error = "CreateOffer called when PeerConnection is closed."; |
| 856 | LOG(LS_ERROR) << error; |
| 857 | PostCreateSessionDescriptionFailure(observer, error); |
| 858 | return; |
| 859 | } |
| 860 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 861 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 862 | std::string error = "CreateOffer called with invalid options."; |
| 863 | LOG(LS_ERROR) << error; |
| 864 | PostCreateSessionDescriptionFailure(observer, error); |
| 865 | return; |
| 866 | } |
| 867 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 868 | cricket::MediaSessionOptions session_options; |
| 869 | GetOptionsForOffer(options, &session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 870 | session_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | void PeerConnection::CreateAnswer( |
| 874 | CreateSessionDescriptionObserver* observer, |
| 875 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 876 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 877 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 878 | if (!observer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 879 | LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 880 | return; |
| 881 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 882 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 883 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 884 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 885 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 886 | std::string error = "CreateAnswer called with invalid constraints."; |
| 887 | LOG(LS_ERROR) << error; |
| 888 | PostCreateSessionDescriptionFailure(observer, error); |
| 889 | return; |
| 890 | } |
| 891 | |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 892 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 896 | const RTCOfferAnswerOptions& options) { |
| 897 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 898 | if (!observer) { |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 899 | LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 900 | return; |
| 901 | } |
| 902 | |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 903 | if (IsClosed()) { |
| 904 | std::string error = "CreateAnswer called when PeerConnection is closed."; |
| 905 | LOG(LS_ERROR) << error; |
| 906 | PostCreateSessionDescriptionFailure(observer, error); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | if (!session_->remote_description() || |
| 911 | session_->remote_description()->type() != |
| 912 | SessionDescriptionInterface::kOffer) { |
| 913 | std::string error = "CreateAnswer called without remote offer."; |
| 914 | LOG(LS_ERROR) << error; |
| 915 | PostCreateSessionDescriptionFailure(observer, error); |
| 916 | return; |
| 917 | } |
| 918 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 919 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 920 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 921 | |
| 922 | session_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | void PeerConnection::SetLocalDescription( |
| 926 | SetSessionDescriptionObserver* observer, |
| 927 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 928 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 929 | if (!observer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 930 | LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
| 931 | return; |
| 932 | } |
| 933 | if (!desc) { |
| 934 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 935 | return; |
| 936 | } |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 937 | |
| 938 | // Takes the ownership of |desc| regardless of the result. |
| 939 | std::unique_ptr<SessionDescriptionInterface> desc_temp(desc); |
| 940 | |
| 941 | if (IsClosed()) { |
| 942 | std::string error = "Failed to set local " + desc->type() + |
| 943 | " SDP: Called in wrong state: STATE_CLOSED"; |
| 944 | LOG(LS_ERROR) << error; |
| 945 | PostSetSessionDescriptionFailure(observer, error); |
| 946 | return; |
| 947 | } |
| 948 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 949 | // Update stats here so that we have the most recent stats for tracks and |
| 950 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 951 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 952 | std::string error; |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 953 | if (!session_->SetLocalDescription(std::move(desc_temp), &error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 954 | PostSetSessionDescriptionFailure(observer, error); |
| 955 | return; |
| 956 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 957 | |
| 958 | // If setting the description decided our SSL role, allocate any necessary |
| 959 | // SCTP sids. |
| 960 | rtc::SSLRole role; |
| 961 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 962 | session_->GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 963 | AllocateSctpSids(role); |
| 964 | } |
| 965 | |
| 966 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 967 | // local session description. |
| 968 | const cricket::ContentInfo* audio_content = |
| 969 | GetFirstAudioContent(desc->description()); |
| 970 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 971 | if (audio_content->rejected) { |
| 972 | RemoveTracks(cricket::MEDIA_TYPE_AUDIO); |
| 973 | } else { |
| 974 | const cricket::AudioContentDescription* audio_desc = |
| 975 | static_cast<const cricket::AudioContentDescription*>( |
| 976 | audio_content->description); |
| 977 | UpdateLocalTracks(audio_desc->streams(), audio_desc->type()); |
| 978 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | const cricket::ContentInfo* video_content = |
| 982 | GetFirstVideoContent(desc->description()); |
| 983 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 984 | if (video_content->rejected) { |
| 985 | RemoveTracks(cricket::MEDIA_TYPE_VIDEO); |
| 986 | } else { |
| 987 | const cricket::VideoContentDescription* video_desc = |
| 988 | static_cast<const cricket::VideoContentDescription*>( |
| 989 | video_content->description); |
| 990 | UpdateLocalTracks(video_desc->streams(), video_desc->type()); |
| 991 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | const cricket::ContentInfo* data_content = |
| 995 | GetFirstDataContent(desc->description()); |
| 996 | if (data_content) { |
| 997 | const cricket::DataContentDescription* data_desc = |
| 998 | static_cast<const cricket::DataContentDescription*>( |
| 999 | data_content->description); |
| 1000 | if (rtc::starts_with(data_desc->protocol().data(), |
| 1001 | cricket::kMediaProtocolRtpPrefix)) { |
| 1002 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1007 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 1008 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1009 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1010 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1011 | // size is not allowed, and changing the set of ICE servers will not result |
| 1012 | // in new candidates being gathered. |
| 1013 | port_allocator_->FreezeCandidatePool(); |
| 1014 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1015 | // MaybeStartGathering needs to be called after posting |
| 1016 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1017 | // before signaling that SetLocalDescription completed. |
| 1018 | session_->MaybeStartGathering(); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1019 | |
| 1020 | if (desc->type() == SessionDescriptionInterface::kAnswer) { |
| 1021 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1022 | // MaybeStartGathering... |
| 1023 | network_thread()->Invoke<void>( |
| 1024 | RTC_FROM_HERE, |
| 1025 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1026 | port_allocator_.get())); |
| 1027 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | void PeerConnection::SetRemoteDescription( |
| 1031 | SetSessionDescriptionObserver* observer, |
| 1032 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1033 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1034 | if (!observer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1035 | LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
| 1036 | return; |
| 1037 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1038 | if (!desc) { |
| 1039 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1040 | return; |
| 1041 | } |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 1042 | |
| 1043 | // Takes the ownership of |desc| regardless of the result. |
| 1044 | std::unique_ptr<SessionDescriptionInterface> desc_temp(desc); |
| 1045 | |
| 1046 | if (IsClosed()) { |
| 1047 | std::string error = "Failed to set remote " + desc->type() + |
| 1048 | " SDP: Called in wrong state: STATE_CLOSED"; |
| 1049 | LOG(LS_ERROR) << error; |
| 1050 | PostSetSessionDescriptionFailure(observer, error); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1054 | // Update stats here so that we have the most recent stats for tracks and |
| 1055 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1056 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1057 | std::string error; |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 1058 | if (!session_->SetRemoteDescription(std::move(desc_temp), &error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1059 | PostSetSessionDescriptionFailure(observer, error); |
| 1060 | return; |
| 1061 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1062 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1063 | // If setting the description decided our SSL role, allocate any necessary |
| 1064 | // SCTP sids. |
| 1065 | rtc::SSLRole role; |
| 1066 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1067 | session_->GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1068 | AllocateSctpSids(role); |
| 1069 | } |
| 1070 | |
| 1071 | const cricket::SessionDescription* remote_desc = desc->description(); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1072 | const cricket::ContentInfo* audio_content = GetFirstAudioContent(remote_desc); |
| 1073 | const cricket::ContentInfo* video_content = GetFirstVideoContent(remote_desc); |
| 1074 | const cricket::AudioContentDescription* audio_desc = |
| 1075 | GetFirstAudioContentDescription(remote_desc); |
| 1076 | const cricket::VideoContentDescription* video_desc = |
| 1077 | GetFirstVideoContentDescription(remote_desc); |
| 1078 | const cricket::DataContentDescription* data_desc = |
| 1079 | GetFirstDataContentDescription(remote_desc); |
| 1080 | |
| 1081 | // Check if the descriptions include streams, just in case the peer supports |
| 1082 | // MSID, but doesn't indicate so with "a=msid-semantic". |
| 1083 | if (remote_desc->msid_supported() || |
| 1084 | (audio_desc && !audio_desc->streams().empty()) || |
| 1085 | (video_desc && !video_desc->streams().empty())) { |
| 1086 | remote_peer_supports_msid_ = true; |
| 1087 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1088 | |
| 1089 | // We wait to signal new streams until we finish processing the description, |
| 1090 | // since only at that point will new streams have all their tracks. |
| 1091 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 1092 | |
Steve Anton | 3df5dca | 2017-10-19 16:11:30 -0700 | [diff] [blame] | 1093 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 1094 | // rejected media section, there is some cleanup logic that expects the voice/ |
| 1095 | // video channel to still be set. But in this method the voice/video channel |
| 1096 | // would have been destroyed by WebRtcSession's SetRemoteDescription method |
| 1097 | // above, so the cleanup that relies on them fails to run. This is hard to fix |
| 1098 | // with WebRtcSession and PeerConnection separated, but once the classes are |
| 1099 | // merged it will be easy to call RemoveTracks right before destroying the |
| 1100 | // voice/video channels. |
| 1101 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1102 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 1103 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1104 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1105 | if (audio_content->rejected) { |
| 1106 | RemoveTracks(cricket::MEDIA_TYPE_AUDIO); |
| 1107 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1108 | bool default_audio_track_needed = |
| 1109 | !remote_peer_supports_msid_ && |
| 1110 | MediaContentDirectionHasSend(audio_desc->direction()); |
| 1111 | UpdateRemoteStreamsList(GetActiveStreams(audio_desc), |
| 1112 | default_audio_track_needed, audio_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1113 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1114 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 1118 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1119 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1120 | if (video_content->rejected) { |
| 1121 | RemoveTracks(cricket::MEDIA_TYPE_VIDEO); |
| 1122 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1123 | bool default_video_track_needed = |
| 1124 | !remote_peer_supports_msid_ && |
| 1125 | MediaContentDirectionHasSend(video_desc->direction()); |
| 1126 | UpdateRemoteStreamsList(GetActiveStreams(video_desc), |
| 1127 | default_video_track_needed, video_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1128 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1129 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | // Update the DataChannels with the information from the remote peer. |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1133 | if (data_desc) { |
| 1134 | if (rtc::starts_with(data_desc->protocol().data(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1135 | cricket::kMediaProtocolRtpPrefix)) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1136 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 1141 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 1142 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 1143 | stats_->AddStream(new_stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 1144 | observer_->OnAddStream( |
| 1145 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1148 | UpdateEndedRemoteMediaStreams(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1149 | |
| 1150 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1151 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 1152 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1153 | |
| 1154 | if (desc->type() == SessionDescriptionInterface::kAnswer) { |
| 1155 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1156 | // MaybeStartGathering... |
| 1157 | network_thread()->Invoke<void>( |
| 1158 | RTC_FROM_HERE, |
| 1159 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1160 | port_allocator_.get())); |
| 1161 | } |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 1164 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 1165 | return configuration_; |
| 1166 | } |
| 1167 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1168 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 1169 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1170 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 1171 | |
| 1172 | if (session_->local_description() && |
| 1173 | configuration.ice_candidate_pool_size != |
| 1174 | configuration_.ice_candidate_pool_size) { |
| 1175 | LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 1176 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1177 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1178 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1179 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1180 | // The simplest (and most future-compatible) way to tell if the config was |
| 1181 | // modified in an invalid way is to copy each property we do support |
| 1182 | // modifying, then use operator==. There are far more properties we don't |
| 1183 | // support modifying than those we do, and more could be added. |
| 1184 | RTCConfiguration modified_config = configuration_; |
| 1185 | modified_config.servers = configuration.servers; |
| 1186 | modified_config.type = configuration.type; |
| 1187 | modified_config.ice_candidate_pool_size = |
| 1188 | configuration.ice_candidate_pool_size; |
| 1189 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 1190 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 1191 | modified_config.turn_customizer = configuration.turn_customizer; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1192 | if (configuration != modified_config) { |
| 1193 | LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
| 1194 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 1195 | } |
| 1196 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1197 | // Validate the modified configuration. |
| 1198 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 1199 | if (!validate_error.ok()) { |
| 1200 | return SafeSetError(std::move(validate_error), error); |
| 1201 | } |
| 1202 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1203 | // Note that this isn't possible through chromium, since it's an unsigned |
| 1204 | // short in WebIDL. |
| 1205 | if (configuration.ice_candidate_pool_size < 0 || |
| 1206 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 1207 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 1208 | } |
| 1209 | |
| 1210 | // Parse ICE servers before hopping to network thread. |
| 1211 | cricket::ServerAddresses stun_servers; |
| 1212 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1213 | RTCErrorType parse_error = |
| 1214 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 1215 | if (parse_error != RTCErrorType::NONE) { |
| 1216 | return SafeSetError(parse_error, error); |
| 1217 | } |
| 1218 | |
| 1219 | // In theory this shouldn't fail. |
| 1220 | if (!network_thread()->Invoke<bool>( |
| 1221 | RTC_FROM_HERE, |
| 1222 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 1223 | stun_servers, turn_servers, modified_config.type, |
| 1224 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 1225 | modified_config.prune_turn_ports, |
| 1226 | modified_config.turn_customizer))) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1227 | LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
| 1228 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 1229 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1230 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 1231 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 1232 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 1233 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1234 | if (modified_config.servers != configuration_.servers || |
| 1235 | modified_config.type != configuration_.type || |
| 1236 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 1237 | session_->SetNeedsIceRestartFlag(); |
| 1238 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 1239 | |
| 1240 | if (modified_config.ice_check_min_interval != |
| 1241 | configuration_.ice_check_min_interval) { |
| 1242 | session_->SetIceConfig(session_->ParseIceConfig(modified_config)); |
| 1243 | } |
| 1244 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1245 | configuration_ = modified_config; |
| 1246 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1249 | bool PeerConnection::AddIceCandidate( |
| 1250 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1251 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1252 | if (IsClosed()) { |
| 1253 | return false; |
| 1254 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1255 | return session_->ProcessIceMessage(ice_candidate); |
| 1256 | } |
| 1257 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1258 | bool PeerConnection::RemoveIceCandidates( |
| 1259 | const std::vector<cricket::Candidate>& candidates) { |
| 1260 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
| 1261 | return session_->RemoveRemoteIceCandidates(candidates); |
| 1262 | } |
| 1263 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1264 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1265 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1266 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1267 | |
| 1268 | if (session_) { |
| 1269 | session_->set_metrics_observer(uma_observer_); |
| 1270 | } |
| 1271 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1272 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 1273 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1274 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1275 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1276 | uma_observer_->IncrementEnumCounter( |
| 1277 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 1278 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 1279 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1280 | uma_observer_->IncrementEnumCounter( |
| 1281 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 1282 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1283 | } |
| 1284 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 1287 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 1288 | if (!worker_thread()->IsCurrent()) { |
| 1289 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 1290 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 1291 | } |
| 1292 | |
| 1293 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 1294 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 1295 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 1296 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 1297 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1298 | "min_bitrate_bps <= 0"); |
| 1299 | } |
| 1300 | if (has_current) { |
| 1301 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 1302 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1303 | "current_bitrate_bps < min_bitrate_bps"); |
| 1304 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 1305 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1306 | "curent_bitrate_bps < 0"); |
| 1307 | } |
| 1308 | } |
| 1309 | if (has_max) { |
| 1310 | if (has_current && |
| 1311 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 1312 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1313 | "max_bitrate_bps < current_bitrate_bps"); |
| 1314 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 1315 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1316 | "max_bitrate_bps < min_bitrate_bps"); |
| 1317 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 1318 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1319 | "max_bitrate_bps < 0"); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | Call::Config::BitrateConfigMask mask; |
| 1324 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 1325 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 1326 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 1327 | |
| 1328 | RTC_DCHECK(call_.get()); |
| 1329 | call_->SetBitrateConfigMask(mask); |
| 1330 | |
| 1331 | return RTCError::OK(); |
| 1332 | } |
| 1333 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame^] | 1334 | void PeerConnection::SetBitrateAllocationStrategy( |
| 1335 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 1336 | bitrate_allocation_strategy) { |
| 1337 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 1338 | if (!worker_thread->IsCurrent()) { |
| 1339 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 1340 | bitrate_allocation_strategy.release(); |
| 1341 | auto functor = [this, strategy_raw]() { |
| 1342 | call_->SetBitrateAllocationStrategy( |
| 1343 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 1344 | }; |
| 1345 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 1346 | return; |
| 1347 | } |
| 1348 | RTC_DCHECK(call_.get()); |
| 1349 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 1350 | } |
| 1351 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 1352 | std::unique_ptr<rtc::SSLCertificate> |
| 1353 | PeerConnection::GetRemoteAudioSSLCertificate() { |
| 1354 | if (!session_) { |
| 1355 | return nullptr; |
| 1356 | } |
| 1357 | auto* voice_channel = session_->voice_channel(); |
| 1358 | if (!voice_channel) { |
| 1359 | return nullptr; |
| 1360 | } |
| 1361 | return GetRemoteSSLCertificate(voice_channel->transport_name()); |
| 1362 | } |
| 1363 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 1364 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 1365 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 1366 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 1367 | const size_t max_size = (max_size_bytes < 0) |
| 1368 | ? RtcEventLog::kUnlimitedOutput |
| 1369 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 1370 | return StartRtcEventLog( |
| 1371 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size)); |
| 1372 | } |
| 1373 | |
| 1374 | bool PeerConnection::StartRtcEventLog( |
| 1375 | std::unique_ptr<RtcEventLogOutput> output) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 1376 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 1377 | struct Functor { |
| 1378 | bool operator()() { return pc->StartRtcEventLog_w(std::move(output)); } |
| 1379 | PeerConnection* const pc; |
| 1380 | std::unique_ptr<RtcEventLogOutput> output; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 1381 | }; |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 1382 | return worker_thread()->Invoke<bool>(RTC_FROM_HERE, |
| 1383 | Functor{this, std::move(output)}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 1387 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 1388 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 1389 | } |
| 1390 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1391 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
| 1392 | return session_->local_description(); |
| 1393 | } |
| 1394 | |
| 1395 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
| 1396 | return session_->remote_description(); |
| 1397 | } |
| 1398 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1399 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 1400 | const { |
| 1401 | return session_->current_local_description(); |
| 1402 | } |
| 1403 | |
| 1404 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 1405 | const { |
| 1406 | return session_->current_remote_description(); |
| 1407 | } |
| 1408 | |
| 1409 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 1410 | const { |
| 1411 | return session_->pending_local_description(); |
| 1412 | } |
| 1413 | |
| 1414 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 1415 | const { |
| 1416 | return session_->pending_remote_description(); |
| 1417 | } |
| 1418 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1419 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1420 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1421 | // Update stats here so that we have the most recent stats for tracks and |
| 1422 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1423 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1424 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1425 | session_->Close(); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 1426 | network_thread()->Invoke<void>( |
| 1427 | RTC_FROM_HERE, |
| 1428 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1429 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 1430 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 1431 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 1432 | call_.reset(); |
| 1433 | // The event log must outlive call (and any other object that uses it). |
| 1434 | event_log_.reset(); |
| 1435 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1438 | void PeerConnection::OnSessionStateChange(WebRtcSession* /*session*/, |
| 1439 | WebRtcSession::State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1440 | switch (state) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1441 | case WebRtcSession::STATE_INIT: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1442 | ChangeSignalingState(PeerConnectionInterface::kStable); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1443 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1444 | case WebRtcSession::STATE_SENTOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1445 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer); |
| 1446 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1447 | case WebRtcSession::STATE_SENTPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1448 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer); |
| 1449 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1450 | case WebRtcSession::STATE_RECEIVEDOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1451 | ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer); |
| 1452 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1453 | case WebRtcSession::STATE_RECEIVEDPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1454 | ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer); |
| 1455 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1456 | case WebRtcSession::STATE_INPROGRESS: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1457 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 1458 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1459 | case WebRtcSession::STATE_CLOSED: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1460 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 1461 | break; |
| 1462 | default: |
| 1463 | break; |
| 1464 | } |
| 1465 | } |
| 1466 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1467 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1468 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1469 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 1470 | SetSessionDescriptionMsg* param = |
| 1471 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1472 | param->observer->OnSuccess(); |
| 1473 | delete param; |
| 1474 | break; |
| 1475 | } |
| 1476 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 1477 | SetSessionDescriptionMsg* param = |
| 1478 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1479 | param->observer->OnFailure(param->error); |
| 1480 | delete param; |
| 1481 | break; |
| 1482 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1483 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 1484 | CreateSessionDescriptionMsg* param = |
| 1485 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 1486 | param->observer->OnFailure(param->error); |
| 1487 | delete param; |
| 1488 | break; |
| 1489 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1490 | case MSG_GETSTATS: { |
| 1491 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 1492 | StatsReports reports; |
| 1493 | stats_->GetStats(param->track, &reports); |
| 1494 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1495 | delete param; |
| 1496 | break; |
| 1497 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1498 | case MSG_FREE_DATACHANNELS: { |
| 1499 | sctp_data_channels_to_free_.clear(); |
| 1500 | break; |
| 1501 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1502 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 1503 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1504 | break; |
| 1505 | } |
| 1506 | } |
| 1507 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1508 | void PeerConnection::CreateAudioReceiver(MediaStreamInterface* stream, |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 1509 | const std::string& track_id, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1510 | uint32_t ssrc) { |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 1511 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1512 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 1513 | signaling_thread(), |
| 1514 | new AudioRtpReceiver(track_id, ssrc, session_->voice_channel())); |
| 1515 | stream->AddTrack( |
| 1516 | static_cast<AudioTrackInterface*>(receiver->internal()->track().get())); |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 1517 | receivers_.push_back(receiver); |
| 1518 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 1519 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
| 1520 | observer_->OnAddTrack(receiver, streams); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1523 | void PeerConnection::CreateVideoReceiver(MediaStreamInterface* stream, |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 1524 | const std::string& track_id, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1525 | uint32_t ssrc) { |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 1526 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1527 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1528 | signaling_thread(), |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 1529 | new VideoRtpReceiver(track_id, worker_thread(), ssrc, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 1530 | session_->video_channel())); |
| 1531 | stream->AddTrack( |
| 1532 | static_cast<VideoTrackInterface*>(receiver->internal()->track().get())); |
zhihuang | 81c3a03 | 2016-11-17 12:06:24 -0800 | [diff] [blame] | 1533 | receivers_.push_back(receiver); |
| 1534 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 1535 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
| 1536 | observer_->OnAddTrack(receiver, streams); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1539 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 1540 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 1541 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
| 1542 | const std::string& track_id) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 1543 | auto it = FindReceiverForTrack(track_id); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1544 | if (it == receivers_.end()) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 1545 | LOG(LS_WARNING) << "RtpReceiver for track with id " << track_id |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1546 | << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 1547 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1548 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 1549 | (*it)->internal()->Stop(); |
| 1550 | rtc::scoped_refptr<RtpReceiverInterface> receiver = *it; |
| 1551 | receivers_.erase(it); |
| 1552 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1555 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 1556 | MediaStreamInterface* stream) { |
| 1557 | RTC_DCHECK(!IsClosed()); |
| 1558 | auto sender = FindSenderForTrack(track); |
| 1559 | if (sender != senders_.end()) { |
| 1560 | // We already have a sender for this track, so just change the stream_id |
| 1561 | // so that it's correct in the next call to CreateOffer. |
| 1562 | (*sender)->internal()->set_stream_id(stream->label()); |
| 1563 | return; |
| 1564 | } |
| 1565 | |
| 1566 | // Normal case; we've never seen this track before. |
| 1567 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender = |
| 1568 | RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1569 | signaling_thread(), |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1570 | new AudioRtpSender(track, {stream->label()}, |
| 1571 | session_->voice_channel(), stats_.get())); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1572 | senders_.push_back(new_sender); |
| 1573 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 1574 | // which will connect the sender to the underlying transport. This can |
| 1575 | // occur if a local session description that contains the ID of the sender |
| 1576 | // is set before AddStream is called. It can also occur if the local |
| 1577 | // session description is not changed and RemoveStream is called, and |
| 1578 | // later AddStream is called again with the same stream. |
| 1579 | const TrackInfo* track_info = |
| 1580 | FindTrackInfo(local_audio_tracks_, stream->label(), track->id()); |
| 1581 | if (track_info) { |
| 1582 | new_sender->internal()->SetSsrc(track_info->ssrc); |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 1587 | // indefinitely, when we have unified plan SDP. |
| 1588 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 1589 | MediaStreamInterface* stream) { |
| 1590 | RTC_DCHECK(!IsClosed()); |
| 1591 | auto sender = FindSenderForTrack(track); |
| 1592 | if (sender == senders_.end()) { |
| 1593 | LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 1594 | << " doesn't exist."; |
| 1595 | return; |
| 1596 | } |
| 1597 | (*sender)->internal()->Stop(); |
| 1598 | senders_.erase(sender); |
| 1599 | } |
| 1600 | |
| 1601 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 1602 | MediaStreamInterface* stream) { |
| 1603 | RTC_DCHECK(!IsClosed()); |
| 1604 | auto sender = FindSenderForTrack(track); |
| 1605 | if (sender != senders_.end()) { |
| 1606 | // We already have a sender for this track, so just change the stream_id |
| 1607 | // so that it's correct in the next call to CreateOffer. |
| 1608 | (*sender)->internal()->set_stream_id(stream->label()); |
| 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | // Normal case; we've never seen this track before. |
| 1613 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender = |
| 1614 | RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1615 | signaling_thread(), new VideoRtpSender(track, {stream->label()}, |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1616 | session_->video_channel())); |
| 1617 | senders_.push_back(new_sender); |
| 1618 | const TrackInfo* track_info = |
| 1619 | FindTrackInfo(local_video_tracks_, stream->label(), track->id()); |
| 1620 | if (track_info) { |
| 1621 | new_sender->internal()->SetSsrc(track_info->ssrc); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 1626 | MediaStreamInterface* stream) { |
| 1627 | RTC_DCHECK(!IsClosed()); |
| 1628 | auto sender = FindSenderForTrack(track); |
| 1629 | if (sender == senders_.end()) { |
| 1630 | LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 1631 | << " doesn't exist."; |
| 1632 | return; |
| 1633 | } |
| 1634 | (*sender)->internal()->Stop(); |
| 1635 | senders_.erase(sender); |
| 1636 | } |
| 1637 | |
zstein | 6dfd53a | 2017-03-06 13:49:03 -0800 | [diff] [blame] | 1638 | void PeerConnection::OnIceConnectionStateChange( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1639 | PeerConnectionInterface::IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1640 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1641 | // After transitioning to "closed", ignore any additional states from |
| 1642 | // WebRtcSession (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1643 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1644 | return; |
| 1645 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1646 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1647 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | void PeerConnection::OnIceGatheringChange( |
| 1651 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1652 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1653 | if (IsClosed()) { |
| 1654 | return; |
| 1655 | } |
| 1656 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1657 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 1660 | void PeerConnection::OnIceCandidate( |
| 1661 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1662 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1663 | if (IsClosed()) { |
| 1664 | return; |
| 1665 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 1666 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1669 | void PeerConnection::OnIceCandidatesRemoved( |
| 1670 | const std::vector<cricket::Candidate>& candidates) { |
| 1671 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1672 | if (IsClosed()) { |
| 1673 | return; |
| 1674 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1675 | observer_->OnIceCandidatesRemoved(candidates); |
| 1676 | } |
| 1677 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1678 | void PeerConnection::OnIceConnectionReceivingChange(bool receiving) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1679 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1680 | if (IsClosed()) { |
| 1681 | return; |
| 1682 | } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1683 | observer_->OnIceConnectionReceivingChange(receiving); |
| 1684 | } |
| 1685 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1686 | void PeerConnection::ChangeSignalingState( |
| 1687 | PeerConnectionInterface::SignalingState signaling_state) { |
| 1688 | signaling_state_ = signaling_state; |
| 1689 | if (signaling_state == kClosed) { |
| 1690 | ice_connection_state_ = kIceConnectionClosed; |
| 1691 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 1692 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 1693 | ice_gathering_state_ = kIceGatheringComplete; |
| 1694 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 1695 | } |
| 1696 | } |
| 1697 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1700 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 1701 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1702 | if (IsClosed()) { |
| 1703 | return; |
| 1704 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1705 | AddAudioTrack(track, stream); |
| 1706 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1709 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 1710 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1711 | if (IsClosed()) { |
| 1712 | return; |
| 1713 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1714 | RemoveAudioTrack(track, stream); |
| 1715 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 1719 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1720 | if (IsClosed()) { |
| 1721 | return; |
| 1722 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1723 | AddVideoTrack(track, stream); |
| 1724 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 1728 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1729 | if (IsClosed()) { |
| 1730 | return; |
| 1731 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1732 | RemoveVideoTrack(track, stream); |
| 1733 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1736 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 1737 | SetSessionDescriptionObserver* observer, |
| 1738 | const std::string& error) { |
| 1739 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 1740 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1741 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 1742 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 1746 | CreateSessionDescriptionObserver* observer, |
| 1747 | const std::string& error) { |
| 1748 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 1749 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1750 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 1751 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1754 | void PeerConnection::GetOptionsForOffer( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1755 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 1756 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1757 | ExtractSharedMediaSessionOptions(rtc_options, session_options); |
| 1758 | |
| 1759 | // Figure out transceiver directional preferences. |
| 1760 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 1761 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 1762 | |
| 1763 | // By default, generate sendrecv/recvonly m= sections. |
| 1764 | bool recv_audio = true; |
| 1765 | bool recv_video = true; |
| 1766 | |
| 1767 | // By default, only offer a new m= section if we have media to send with it. |
| 1768 | bool offer_new_audio_description = send_audio; |
| 1769 | bool offer_new_video_description = send_video; |
| 1770 | bool offer_new_data_description = HasDataChannels(); |
| 1771 | |
| 1772 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
| 1773 | if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1774 | recv_audio = (rtc_options.offer_to_receive_audio > 0); |
| 1775 | offer_new_audio_description = |
| 1776 | offer_new_audio_description || (rtc_options.offer_to_receive_audio > 0); |
| 1777 | } |
| 1778 | if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1779 | recv_video = (rtc_options.offer_to_receive_video > 0); |
| 1780 | offer_new_video_description = |
| 1781 | offer_new_video_description || (rtc_options.offer_to_receive_video > 0); |
| 1782 | } |
| 1783 | |
| 1784 | rtc::Optional<size_t> audio_index; |
| 1785 | rtc::Optional<size_t> video_index; |
| 1786 | rtc::Optional<size_t> data_index; |
| 1787 | // If a current description exists, generate m= sections in the same order, |
| 1788 | // using the first audio/video/data section that appears and rejecting |
| 1789 | // extraneous ones. |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 1790 | if (session_->local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1791 | GenerateMediaDescriptionOptions( |
| 1792 | session_->local_description(), |
| 1793 | cricket::RtpTransceiverDirection(send_audio, recv_audio), |
| 1794 | cricket::RtpTransceiverDirection(send_video, recv_video), &audio_index, |
| 1795 | &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1798 | // Add audio/video/data m= sections to the end if needed. |
| 1799 | if (!audio_index && offer_new_audio_description) { |
| 1800 | session_options->media_description_options.push_back( |
| 1801 | cricket::MediaDescriptionOptions( |
| 1802 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
| 1803 | cricket::RtpTransceiverDirection(send_audio, recv_audio), false)); |
| 1804 | audio_index = rtc::Optional<size_t>( |
| 1805 | session_options->media_description_options.size() - 1); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1806 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1807 | if (!video_index && offer_new_video_description) { |
| 1808 | session_options->media_description_options.push_back( |
| 1809 | cricket::MediaDescriptionOptions( |
| 1810 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
| 1811 | cricket::RtpTransceiverDirection(send_video, recv_video), false)); |
| 1812 | video_index = rtc::Optional<size_t>( |
| 1813 | session_options->media_description_options.size() - 1); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1814 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1815 | if (!data_index && offer_new_data_description) { |
| 1816 | session_options->media_description_options.push_back( |
| 1817 | cricket::MediaDescriptionOptions( |
| 1818 | cricket::MEDIA_TYPE_DATA, cricket::CN_DATA, |
| 1819 | cricket::RtpTransceiverDirection(true, true), false)); |
| 1820 | data_index = rtc::Optional<size_t>( |
| 1821 | session_options->media_description_options.size() - 1); |
| 1822 | } |
| 1823 | |
| 1824 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 1825 | !audio_index ? nullptr |
| 1826 | : &session_options->media_description_options[*audio_index]; |
| 1827 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 1828 | !video_index ? nullptr |
| 1829 | : &session_options->media_description_options[*video_index]; |
| 1830 | cricket::MediaDescriptionOptions* data_media_description_options = |
| 1831 | !data_index ? nullptr |
| 1832 | : &session_options->media_description_options[*data_index]; |
| 1833 | |
| 1834 | // Apply ICE restart flag and renomination flag. |
| 1835 | for (auto& options : session_options->media_description_options) { |
| 1836 | options.transport_options.ice_restart = rtc_options.ice_restart; |
| 1837 | options.transport_options.enable_ice_renomination = |
| 1838 | configuration_.enable_ice_renomination; |
| 1839 | } |
| 1840 | |
| 1841 | AddRtpSenderOptions(senders_, audio_media_description_options, |
| 1842 | video_media_description_options); |
| 1843 | AddRtpDataChannelOptions(rtp_data_channels_, data_media_description_options); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1844 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1845 | // Intentionally unset the data channel type for RTP data channel with the |
| 1846 | // second condition. Otherwise the RTP data channels would be successfully |
| 1847 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 1848 | // when building with chromium. We want to leave RTP data channels broken, so |
| 1849 | // people won't try to use them. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1850 | if (!rtp_data_channels_.empty() || |
| 1851 | session_->data_channel_type() != cricket::DCT_RTP) { |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1852 | session_options->data_channel_type = session_->data_channel_type(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1853 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 1854 | |
| 1855 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1856 | session_options->crypto_options = factory_->options().crypto_options; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1859 | void PeerConnection::GetOptionsForAnswer( |
| 1860 | const RTCOfferAnswerOptions& rtc_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1861 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1862 | ExtractSharedMediaSessionOptions(rtc_options, session_options); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1863 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1864 | // Figure out transceiver directional preferences. |
| 1865 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 1866 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 1867 | |
| 1868 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 1869 | // restricted by the direction in the offer. |
| 1870 | bool recv_audio = true; |
| 1871 | bool recv_video = true; |
| 1872 | |
| 1873 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
| 1874 | if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1875 | recv_audio = (rtc_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 1876 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1877 | if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1878 | recv_video = (rtc_options.offer_to_receive_video > 0); |
| 1879 | } |
| 1880 | |
| 1881 | rtc::Optional<size_t> audio_index; |
| 1882 | rtc::Optional<size_t> video_index; |
| 1883 | rtc::Optional<size_t> data_index; |
zhihuang | 141aacb | 2017-08-29 13:23:53 -0700 | [diff] [blame] | 1884 | if (session_->remote_description()) { |
| 1885 | // The pending remote description should be an offer. |
| 1886 | RTC_DCHECK(session_->remote_description()->type() == |
| 1887 | SessionDescriptionInterface::kOffer); |
| 1888 | // Generate m= sections that match those in the offer. |
| 1889 | // Note that mediasession.cc will handle intersection our preferred |
| 1890 | // direction with the offered direction. |
| 1891 | GenerateMediaDescriptionOptions( |
| 1892 | session_->remote_description(), |
| 1893 | cricket::RtpTransceiverDirection(send_audio, recv_audio), |
| 1894 | cricket::RtpTransceiverDirection(send_video, recv_video), &audio_index, |
| 1895 | &video_index, &data_index, session_options); |
| 1896 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1897 | |
| 1898 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 1899 | !audio_index ? nullptr |
| 1900 | : &session_options->media_description_options[*audio_index]; |
| 1901 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 1902 | !video_index ? nullptr |
| 1903 | : &session_options->media_description_options[*video_index]; |
| 1904 | cricket::MediaDescriptionOptions* data_media_description_options = |
| 1905 | !data_index ? nullptr |
| 1906 | : &session_options->media_description_options[*data_index]; |
| 1907 | |
| 1908 | // Apply ICE renomination flag. |
| 1909 | for (auto& options : session_options->media_description_options) { |
| 1910 | options.transport_options.enable_ice_renomination = |
| 1911 | configuration_.enable_ice_renomination; |
| 1912 | } |
| 1913 | |
| 1914 | AddRtpSenderOptions(senders_, audio_media_description_options, |
| 1915 | video_media_description_options); |
| 1916 | AddRtpDataChannelOptions(rtp_data_channels_, data_media_description_options); |
| 1917 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1918 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 1919 | // the RTP data channels would be successfully negotiated by default and the |
| 1920 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 1921 | // We want to leave RTP data channels broken, so people won't try to use them. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1922 | if (!rtp_data_channels_.empty() || |
| 1923 | session_->data_channel_type() != cricket::DCT_RTP) { |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1924 | session_options->data_channel_type = session_->data_channel_type(); |
deadbeef | 907abe4 | 2016-08-04 12:22:18 -0700 | [diff] [blame] | 1925 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 1926 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1927 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1928 | session_options->crypto_options = factory_->options().crypto_options; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1929 | } |
| 1930 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1931 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 1932 | const SessionDescriptionInterface* session_desc, |
| 1933 | cricket::RtpTransceiverDirection audio_direction, |
| 1934 | cricket::RtpTransceiverDirection video_direction, |
| 1935 | rtc::Optional<size_t>* audio_index, |
| 1936 | rtc::Optional<size_t>* video_index, |
| 1937 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1938 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1939 | for (const cricket::ContentInfo& content : |
| 1940 | session_desc->description()->contents()) { |
| 1941 | if (IsAudioContent(&content)) { |
| 1942 | // If we already have an audio m= section, reject this extra one. |
| 1943 | if (*audio_index) { |
| 1944 | session_options->media_description_options.push_back( |
| 1945 | cricket::MediaDescriptionOptions( |
| 1946 | cricket::MEDIA_TYPE_AUDIO, content.name, |
| 1947 | cricket::RtpTransceiverDirection(false, false), true)); |
| 1948 | } else { |
| 1949 | session_options->media_description_options.push_back( |
| 1950 | cricket::MediaDescriptionOptions( |
| 1951 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
| 1952 | !audio_direction.send && !audio_direction.recv)); |
| 1953 | *audio_index = rtc::Optional<size_t>( |
| 1954 | session_options->media_description_options.size() - 1); |
| 1955 | } |
| 1956 | } else if (IsVideoContent(&content)) { |
| 1957 | // If we already have an video m= section, reject this extra one. |
| 1958 | if (*video_index) { |
| 1959 | session_options->media_description_options.push_back( |
| 1960 | cricket::MediaDescriptionOptions( |
| 1961 | cricket::MEDIA_TYPE_VIDEO, content.name, |
| 1962 | cricket::RtpTransceiverDirection(false, false), true)); |
| 1963 | } else { |
| 1964 | session_options->media_description_options.push_back( |
| 1965 | cricket::MediaDescriptionOptions( |
| 1966 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
| 1967 | !video_direction.send && !video_direction.recv)); |
| 1968 | *video_index = rtc::Optional<size_t>( |
| 1969 | session_options->media_description_options.size() - 1); |
| 1970 | } |
| 1971 | } else { |
| 1972 | RTC_DCHECK(IsDataContent(&content)); |
| 1973 | // If we already have an data m= section, reject this extra one. |
| 1974 | if (*data_index) { |
| 1975 | session_options->media_description_options.push_back( |
| 1976 | cricket::MediaDescriptionOptions( |
| 1977 | cricket::MEDIA_TYPE_DATA, content.name, |
| 1978 | cricket::RtpTransceiverDirection(false, false), true)); |
| 1979 | } else { |
| 1980 | session_options->media_description_options.push_back( |
| 1981 | cricket::MediaDescriptionOptions( |
| 1982 | cricket::MEDIA_TYPE_DATA, content.name, |
| 1983 | // Direction for data sections is meaningless, but legacy |
| 1984 | // endpoints might expect sendrecv. |
| 1985 | cricket::RtpTransceiverDirection(true, true), false)); |
| 1986 | *data_index = rtc::Optional<size_t>( |
| 1987 | session_options->media_description_options.size() - 1); |
| 1988 | } |
| 1989 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1990 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1993 | void PeerConnection::RemoveTracks(cricket::MediaType media_type) { |
| 1994 | UpdateLocalTracks(std::vector<cricket::StreamParams>(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1995 | UpdateRemoteStreamsList(std::vector<cricket::StreamParams>(), false, |
| 1996 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1999 | void PeerConnection::UpdateRemoteStreamsList( |
| 2000 | const cricket::StreamParamsVec& streams, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2001 | bool default_track_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2002 | cricket::MediaType media_type, |
| 2003 | StreamCollection* new_streams) { |
| 2004 | TrackInfos* current_tracks = GetRemoteTracks(media_type); |
| 2005 | |
| 2006 | // Find removed tracks. I.e., tracks where the track id or ssrc don't match |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2007 | // the new StreamParam. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2008 | auto track_it = current_tracks->begin(); |
| 2009 | while (track_it != current_tracks->end()) { |
| 2010 | const TrackInfo& info = *track_it; |
| 2011 | const cricket::StreamParams* params = |
| 2012 | cricket::GetStreamBySsrc(streams, info.ssrc); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2013 | bool track_exists = params && params->id == info.track_id; |
| 2014 | // If this is a default track, and we still need it, don't remove it. |
| 2015 | if ((info.stream_label == kDefaultStreamLabel && default_track_needed) || |
| 2016 | track_exists) { |
| 2017 | ++track_it; |
| 2018 | } else { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2019 | OnRemoteTrackRemoved(info.stream_label, info.track_id, media_type); |
| 2020 | track_it = current_tracks->erase(track_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | // Find new and active tracks. |
| 2025 | for (const cricket::StreamParams& params : streams) { |
| 2026 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 2027 | // track id. |
| 2028 | const std::string& stream_label = params.sync_label; |
| 2029 | const std::string& track_id = params.id; |
| 2030 | uint32_t ssrc = params.first_ssrc(); |
| 2031 | |
| 2032 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2033 | remote_streams_->find(stream_label); |
| 2034 | if (!stream) { |
| 2035 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2036 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2037 | MediaStream::Create(stream_label)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2038 | remote_streams_->AddStream(stream); |
| 2039 | new_streams->AddStream(stream); |
| 2040 | } |
| 2041 | |
| 2042 | const TrackInfo* track_info = |
| 2043 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 2044 | if (!track_info) { |
| 2045 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 2046 | OnRemoteTrackSeen(stream_label, track_id, ssrc, media_type); |
| 2047 | } |
| 2048 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2049 | |
| 2050 | // Add default track if necessary. |
| 2051 | if (default_track_needed) { |
| 2052 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
| 2053 | remote_streams_->find(kDefaultStreamLabel); |
| 2054 | if (!default_stream) { |
| 2055 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2056 | default_stream = MediaStreamProxy::Create( |
| 2057 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamLabel)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2058 | remote_streams_->AddStream(default_stream); |
| 2059 | new_streams->AddStream(default_stream); |
| 2060 | } |
| 2061 | std::string default_track_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 2062 | ? kDefaultAudioTrackLabel |
| 2063 | : kDefaultVideoTrackLabel; |
| 2064 | const TrackInfo* default_track_info = |
| 2065 | FindTrackInfo(*current_tracks, kDefaultStreamLabel, default_track_id); |
| 2066 | if (!default_track_info) { |
| 2067 | current_tracks->push_back( |
| 2068 | TrackInfo(kDefaultStreamLabel, default_track_id, 0)); |
| 2069 | OnRemoteTrackSeen(kDefaultStreamLabel, default_track_id, 0, media_type); |
| 2070 | } |
| 2071 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | void PeerConnection::OnRemoteTrackSeen(const std::string& stream_label, |
| 2075 | const std::string& track_id, |
| 2076 | uint32_t ssrc, |
| 2077 | cricket::MediaType media_type) { |
| 2078 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 2079 | |
| 2080 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2081 | CreateAudioReceiver(stream, track_id, ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2082 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 2083 | CreateVideoReceiver(stream, track_id, ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2084 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 2085 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | void PeerConnection::OnRemoteTrackRemoved(const std::string& stream_label, |
| 2090 | const std::string& track_id, |
| 2091 | cricket::MediaType media_type) { |
| 2092 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 2093 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2094 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2095 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2096 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 2097 | // will be notified which will end the AudioRtpReceiver::track(). |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2098 | receiver = RemoveAndStopReceiver(track_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2099 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 2100 | stream->FindAudioTrack(track_id); |
| 2101 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2102 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2103 | } |
| 2104 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2105 | // Stopping or destroying a VideoRtpReceiver will end the |
| 2106 | // VideoRtpReceiver::track(). |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2107 | receiver = RemoveAndStopReceiver(track_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2108 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 2109 | stream->FindVideoTrack(track_id); |
| 2110 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2111 | // There's no guarantee the track is still available, e.g. the track may |
| 2112 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2113 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2114 | } |
| 2115 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 2116 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2117 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 2118 | if (receiver) { |
| 2119 | observer_->OnRemoveTrack(receiver); |
| 2120 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 2124 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 2125 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 2126 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 2127 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 2128 | streams_to_remove.push_back(stream); |
| 2129 | } |
| 2130 | } |
| 2131 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 2132 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2133 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 2134 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2135 | } |
| 2136 | } |
| 2137 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2138 | void PeerConnection::UpdateLocalTracks( |
| 2139 | const std::vector<cricket::StreamParams>& streams, |
| 2140 | cricket::MediaType media_type) { |
| 2141 | TrackInfos* current_tracks = GetLocalTracks(media_type); |
| 2142 | |
| 2143 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 2144 | // don't match the new StreamParam. |
| 2145 | TrackInfos::iterator track_it = current_tracks->begin(); |
| 2146 | while (track_it != current_tracks->end()) { |
| 2147 | const TrackInfo& info = *track_it; |
| 2148 | const cricket::StreamParams* params = |
| 2149 | cricket::GetStreamBySsrc(streams, info.ssrc); |
| 2150 | if (!params || params->id != info.track_id || |
| 2151 | params->sync_label != info.stream_label) { |
| 2152 | OnLocalTrackRemoved(info.stream_label, info.track_id, info.ssrc, |
| 2153 | media_type); |
| 2154 | track_it = current_tracks->erase(track_it); |
| 2155 | } else { |
| 2156 | ++track_it; |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | // Find new and active tracks. |
| 2161 | for (const cricket::StreamParams& params : streams) { |
| 2162 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 2163 | // track id. |
| 2164 | const std::string& stream_label = params.sync_label; |
| 2165 | const std::string& track_id = params.id; |
| 2166 | uint32_t ssrc = params.first_ssrc(); |
| 2167 | const TrackInfo* track_info = |
| 2168 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 2169 | if (!track_info) { |
| 2170 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 2171 | OnLocalTrackSeen(stream_label, track_id, params.first_ssrc(), media_type); |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | void PeerConnection::OnLocalTrackSeen(const std::string& stream_label, |
| 2177 | const std::string& track_id, |
| 2178 | uint32_t ssrc, |
| 2179 | cricket::MediaType media_type) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2180 | RtpSenderInternal* sender = FindSenderById(track_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2181 | if (!sender) { |
| 2182 | LOG(LS_WARNING) << "An unknown RtpSender with id " << track_id |
| 2183 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2184 | return; |
| 2185 | } |
| 2186 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2187 | if (sender->media_type() != media_type) { |
| 2188 | LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 2189 | << " description with an unexpected media type."; |
| 2190 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2191 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2192 | |
| 2193 | sender->set_stream_id(stream_label); |
| 2194 | sender->SetSsrc(ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | void PeerConnection::OnLocalTrackRemoved(const std::string& stream_label, |
| 2198 | const std::string& track_id, |
| 2199 | uint32_t ssrc, |
| 2200 | cricket::MediaType media_type) { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2201 | RtpSenderInternal* sender = FindSenderById(track_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2202 | if (!sender) { |
| 2203 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2204 | // SessionDescriptions has been renegotiated. |
| 2205 | return; |
| 2206 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2207 | |
| 2208 | // A sender has been removed from the SessionDescription but it's still |
| 2209 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 2210 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 2211 | if (sender->media_type() != media_type) { |
| 2212 | LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 2213 | << " description with an unexpected media type."; |
| 2214 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2215 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2216 | |
| 2217 | sender->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 2221 | const cricket::StreamParamsVec& streams) { |
| 2222 | std::vector<std::string> existing_channels; |
| 2223 | |
| 2224 | // Find new and active data channels. |
| 2225 | for (const cricket::StreamParams& params : streams) { |
| 2226 | // |it->sync_label| is actually the data channel label. The reason is that |
| 2227 | // we use the same naming of data channels as we do for |
| 2228 | // MediaStreams and Tracks. |
| 2229 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 2230 | // track label is the same as |streamid|. |
| 2231 | const std::string& channel_label = params.sync_label; |
| 2232 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2233 | if (data_channel_it == rtp_data_channels_.end()) { |
| 2234 | LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2235 | continue; |
| 2236 | } |
| 2237 | // Set the SSRC the data channel should use for sending. |
| 2238 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 2239 | existing_channels.push_back(data_channel_it->first); |
| 2240 | } |
| 2241 | |
| 2242 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 2243 | } |
| 2244 | |
| 2245 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 2246 | const cricket::StreamParamsVec& streams) { |
| 2247 | std::vector<std::string> existing_channels; |
| 2248 | |
| 2249 | // Find new and active data channels. |
| 2250 | for (const cricket::StreamParams& params : streams) { |
| 2251 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 2252 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 2253 | std::string label = params.sync_label.empty() |
| 2254 | ? rtc::ToString(params.first_ssrc()) |
| 2255 | : params.sync_label; |
| 2256 | auto data_channel_it = rtp_data_channels_.find(label); |
| 2257 | if (data_channel_it == rtp_data_channels_.end()) { |
| 2258 | // This is a new data channel. |
| 2259 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 2260 | } else { |
| 2261 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 2262 | } |
| 2263 | existing_channels.push_back(label); |
| 2264 | } |
| 2265 | |
| 2266 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 2267 | } |
| 2268 | |
| 2269 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 2270 | const std::vector<std::string>& active_channels, |
| 2271 | bool is_local_update) { |
| 2272 | auto it = rtp_data_channels_.begin(); |
| 2273 | while (it != rtp_data_channels_.end()) { |
| 2274 | DataChannel* data_channel = it->second; |
| 2275 | if (std::find(active_channels.begin(), active_channels.end(), |
| 2276 | data_channel->label()) != active_channels.end()) { |
| 2277 | ++it; |
| 2278 | continue; |
| 2279 | } |
| 2280 | |
| 2281 | if (is_local_update) { |
| 2282 | data_channel->SetSendSsrc(0); |
| 2283 | } else { |
| 2284 | data_channel->RemotePeerRequestClose(); |
| 2285 | } |
| 2286 | |
| 2287 | if (data_channel->state() == DataChannel::kClosed) { |
| 2288 | rtp_data_channels_.erase(it); |
| 2289 | it = rtp_data_channels_.begin(); |
| 2290 | } else { |
| 2291 | ++it; |
| 2292 | } |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 2297 | uint32_t remote_ssrc) { |
| 2298 | rtc::scoped_refptr<DataChannel> channel( |
| 2299 | InternalCreateDataChannel(label, nullptr)); |
| 2300 | if (!channel.get()) { |
| 2301 | LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 2302 | << "CreateDataChannel failed."; |
| 2303 | return; |
| 2304 | } |
| 2305 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2306 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 2307 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 2308 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 2312 | const std::string& label, |
| 2313 | const InternalDataChannelInit* config) { |
| 2314 | if (IsClosed()) { |
| 2315 | return nullptr; |
| 2316 | } |
| 2317 | if (session_->data_channel_type() == cricket::DCT_NONE) { |
| 2318 | LOG(LS_ERROR) |
| 2319 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 2320 | return nullptr; |
| 2321 | } |
| 2322 | InternalDataChannelInit new_config = |
| 2323 | config ? (*config) : InternalDataChannelInit(); |
| 2324 | if (session_->data_channel_type() == cricket::DCT_SCTP) { |
| 2325 | if (new_config.id < 0) { |
| 2326 | rtc::SSLRole role; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 2327 | if ((session_->GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2328 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
| 2329 | LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel."; |
| 2330 | return nullptr; |
| 2331 | } |
| 2332 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
| 2333 | LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 2334 | << "because the id is already in use or out of range."; |
| 2335 | return nullptr; |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | rtc::scoped_refptr<DataChannel> channel(DataChannel::Create( |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2340 | session_, session_->data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2341 | if (!channel) { |
| 2342 | sid_allocator_.ReleaseSid(new_config.id); |
| 2343 | return nullptr; |
| 2344 | } |
| 2345 | |
| 2346 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 2347 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
| 2348 | LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 2349 | << " already exists."; |
| 2350 | return nullptr; |
| 2351 | } |
| 2352 | rtp_data_channels_[channel->label()] = channel; |
| 2353 | } else { |
| 2354 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 2355 | sctp_data_channels_.push_back(channel); |
| 2356 | channel->SignalClosed.connect(this, |
| 2357 | &PeerConnection::OnSctpDataChannelClosed); |
| 2358 | } |
| 2359 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 2360 | SignalDataChannelCreated(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2361 | return channel; |
| 2362 | } |
| 2363 | |
| 2364 | bool PeerConnection::HasDataChannels() const { |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2365 | #ifdef HAVE_QUIC |
| 2366 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty() || |
| 2367 | (session_->quic_data_transport() && |
| 2368 | session_->quic_data_transport()->HasDataChannels()); |
| 2369 | #else |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2370 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2371 | #endif // HAVE_QUIC |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 2375 | for (const auto& channel : sctp_data_channels_) { |
| 2376 | if (channel->id() < 0) { |
| 2377 | int sid; |
| 2378 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
| 2379 | LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
| 2380 | continue; |
| 2381 | } |
| 2382 | channel->SetSctpSid(sid); |
| 2383 | } |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 2388 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2389 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 2390 | ++it) { |
| 2391 | if (it->get() == channel) { |
| 2392 | if (channel->id() >= 0) { |
| 2393 | sid_allocator_.ReleaseSid(channel->id()); |
| 2394 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 2395 | // Since this method is triggered by a signal from the DataChannel, |
| 2396 | // we can't free it directly here; we need to free it asynchronously. |
| 2397 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2398 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2399 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 2400 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2401 | return; |
| 2402 | } |
| 2403 | } |
| 2404 | } |
| 2405 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 2406 | void PeerConnection::OnVoiceChannelCreated() { |
| 2407 | SetChannelOnSendersAndReceivers<AudioRtpSender, AudioRtpReceiver>( |
| 2408 | session_->voice_channel(), senders_, receivers_, |
| 2409 | cricket::MEDIA_TYPE_AUDIO); |
| 2410 | } |
| 2411 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2412 | void PeerConnection::OnVoiceChannelDestroyed() { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 2413 | SetChannelOnSendersAndReceivers<AudioRtpSender, AudioRtpReceiver, |
| 2414 | cricket::VoiceChannel>( |
| 2415 | nullptr, senders_, receivers_, cricket::MEDIA_TYPE_AUDIO); |
| 2416 | } |
| 2417 | |
| 2418 | void PeerConnection::OnVideoChannelCreated() { |
| 2419 | SetChannelOnSendersAndReceivers<VideoRtpSender, VideoRtpReceiver>( |
| 2420 | session_->video_channel(), senders_, receivers_, |
| 2421 | cricket::MEDIA_TYPE_VIDEO); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | void PeerConnection::OnVideoChannelDestroyed() { |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 2425 | SetChannelOnSendersAndReceivers<VideoRtpSender, VideoRtpReceiver, |
| 2426 | cricket::VideoChannel>( |
| 2427 | nullptr, senders_, receivers_, cricket::MEDIA_TYPE_VIDEO); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | void PeerConnection::OnDataChannelCreated() { |
| 2431 | for (const auto& channel : sctp_data_channels_) { |
| 2432 | channel->OnTransportChannelCreated(); |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | void PeerConnection::OnDataChannelDestroyed() { |
| 2437 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 2438 | // DataChannel may callback to us and try to modify the list. |
| 2439 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 2440 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 2441 | for (const auto& kv : temp_rtp_dcs) { |
| 2442 | kv.second->OnTransportChannelDestroyed(); |
| 2443 | } |
| 2444 | |
| 2445 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 2446 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 2447 | for (const auto& channel : temp_sctp_dcs) { |
| 2448 | channel->OnTransportChannelDestroyed(); |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | void PeerConnection::OnDataChannelOpenMessage( |
| 2453 | const std::string& label, |
| 2454 | const InternalDataChannelInit& config) { |
| 2455 | rtc::scoped_refptr<DataChannel> channel( |
| 2456 | InternalCreateDataChannel(label, &config)); |
| 2457 | if (!channel.get()) { |
| 2458 | LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
| 2459 | return; |
| 2460 | } |
| 2461 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2462 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 2463 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 2464 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2467 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
| 2468 | return std::find_if( |
| 2469 | senders_.begin(), senders_.end(), |
| 2470 | [type](const rtc::scoped_refptr< |
| 2471 | RtpSenderProxyWithInternal<RtpSenderInternal>>& sender) { |
| 2472 | return sender->media_type() == type; |
| 2473 | }) != senders_.end(); |
| 2474 | } |
| 2475 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2476 | RtpSenderInternal* PeerConnection::FindSenderById(const std::string& id) { |
| 2477 | auto it = std::find_if( |
| 2478 | senders_.begin(), senders_.end(), |
| 2479 | [id](const rtc::scoped_refptr< |
| 2480 | RtpSenderProxyWithInternal<RtpSenderInternal>>& sender) { |
| 2481 | return sender->id() == id; |
| 2482 | }); |
| 2483 | return it != senders_.end() ? (*it)->internal() : nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2484 | } |
| 2485 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2486 | std::vector< |
| 2487 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>::iterator |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2488 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) { |
| 2489 | return std::find_if( |
| 2490 | senders_.begin(), senders_.end(), |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2491 | [track](const rtc::scoped_refptr< |
| 2492 | RtpSenderProxyWithInternal<RtpSenderInternal>>& sender) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2493 | return sender->track() == track; |
| 2494 | }); |
| 2495 | } |
| 2496 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2497 | std::vector<rtc::scoped_refptr< |
| 2498 | RtpReceiverProxyWithInternal<RtpReceiverInternal>>>::iterator |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2499 | PeerConnection::FindReceiverForTrack(const std::string& track_id) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2500 | return std::find_if( |
| 2501 | receivers_.begin(), receivers_.end(), |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 2502 | [track_id](const rtc::scoped_refptr< |
| 2503 | RtpReceiverProxyWithInternal<RtpReceiverInternal>>& receiver) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 2504 | return receiver->id() == track_id; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2505 | }); |
| 2506 | } |
| 2507 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2508 | PeerConnection::TrackInfos* PeerConnection::GetRemoteTracks( |
| 2509 | cricket::MediaType media_type) { |
| 2510 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2511 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 2512 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &remote_audio_tracks_ |
| 2513 | : &remote_video_tracks_; |
| 2514 | } |
| 2515 | |
| 2516 | PeerConnection::TrackInfos* PeerConnection::GetLocalTracks( |
| 2517 | cricket::MediaType media_type) { |
| 2518 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2519 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 2520 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_tracks_ |
| 2521 | : &local_video_tracks_; |
| 2522 | } |
| 2523 | |
| 2524 | const PeerConnection::TrackInfo* PeerConnection::FindTrackInfo( |
| 2525 | const PeerConnection::TrackInfos& infos, |
| 2526 | const std::string& stream_label, |
| 2527 | const std::string track_id) const { |
| 2528 | for (const TrackInfo& track_info : infos) { |
| 2529 | if (track_info.stream_label == stream_label && |
| 2530 | track_info.track_id == track_id) { |
| 2531 | return &track_info; |
| 2532 | } |
| 2533 | } |
| 2534 | return nullptr; |
| 2535 | } |
| 2536 | |
| 2537 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 2538 | for (const auto& channel : sctp_data_channels_) { |
| 2539 | if (channel->id() == sid) { |
| 2540 | return channel; |
| 2541 | } |
| 2542 | } |
| 2543 | return nullptr; |
| 2544 | } |
| 2545 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 2546 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2547 | const RTCConfiguration& configuration) { |
| 2548 | cricket::ServerAddresses stun_servers; |
| 2549 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2550 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 2551 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2552 | return false; |
| 2553 | } |
| 2554 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 2555 | port_allocator_->Initialize(); |
| 2556 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2557 | // To handle both internal and externally created port allocator, we will |
| 2558 | // enable BUNDLE here. |
| 2559 | int portallocator_flags = port_allocator_->flags(); |
| 2560 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 2561 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 2562 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2563 | // If the disable-IPv6 flag was specified, we'll not override it |
| 2564 | // by experiment. |
| 2565 | if (configuration.disable_ipv6) { |
| 2566 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 2567 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 2568 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2569 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 2570 | } |
| 2571 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 2572 | if (configuration.disable_ipv6_on_wifi) { |
| 2573 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 2574 | LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
| 2575 | } |
| 2576 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2577 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 2578 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
| 2579 | LOG(LS_INFO) << "TCP candidates are disabled."; |
| 2580 | } |
| 2581 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 2582 | if (configuration.candidate_network_policy == |
| 2583 | kCandidateNetworkPolicyLowCost) { |
| 2584 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
| 2585 | LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
| 2586 | } |
| 2587 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2588 | port_allocator_->set_flags(portallocator_flags); |
| 2589 | // No step delay is used while allocating ports. |
| 2590 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 2591 | port_allocator_->set_candidate_filter( |
| 2592 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 2593 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2594 | |
| 2595 | // Call this last since it may create pooled allocator sessions using the |
| 2596 | // properties set above. |
| 2597 | port_allocator_->SetConfiguration(stun_servers, turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 2598 | configuration.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2599 | configuration.prune_turn_ports, |
| 2600 | configuration.turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2601 | return true; |
| 2602 | } |
| 2603 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 2604 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2605 | const cricket::ServerAddresses& stun_servers, |
| 2606 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 2607 | IceTransportsType type, |
| 2608 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2609 | bool prune_turn_ports, |
| 2610 | webrtc::TurnCustomizer* turn_customizer) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2611 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2612 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2613 | // Call this last since it may create pooled allocator sessions using the |
| 2614 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2615 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2616 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
| 2617 | turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2620 | bool PeerConnection::StartRtcEventLog_w( |
| 2621 | std::unique_ptr<RtcEventLogOutput> output) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 2622 | if (!event_log_) { |
| 2623 | return false; |
| 2624 | } |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2625 | return event_log_->StartLogging(std::move(output)); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 2629 | if (event_log_) { |
| 2630 | event_log_->StopLogging(); |
| 2631 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2632 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 2633 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2634 | } // namespace webrtc |