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