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