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