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