henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/app/webrtc/peerconnection.h" |
| 29 | |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "talk/app/webrtc/dtmfsender.h" |
| 33 | #include "talk/app/webrtc/jsepicecandidate.h" |
| 34 | #include "talk/app/webrtc/jsepsessiondescription.h" |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 35 | #include "talk/app/webrtc/mediaconstraintsinterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | #include "talk/app/webrtc/mediastreamhandler.h" |
| 37 | #include "talk/app/webrtc/streamcollection.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 38 | #include "webrtc/p2p/client/basicportallocator.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | #include "talk/session/media/channelmanager.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 40 | #include "webrtc/base/logging.h" |
| 41 | #include "webrtc/base/stringencode.h" |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 42 | #include "webrtc/system_wrappers/interface/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | |
| 44 | namespace { |
| 45 | |
| 46 | using webrtc::PeerConnectionInterface; |
| 47 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | // The min number of tokens must present in Turn host uri. |
| 49 | // e.g. user@turn.example.org |
| 50 | static const size_t kTurnHostTokensNum = 2; |
| 51 | // Number of tokens must be preset when TURN uri has transport param. |
| 52 | static const size_t kTurnTransportTokensNum = 2; |
| 53 | // The default stun port. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 54 | static const int kDefaultStunPort = 3478; |
| 55 | static const int kDefaultStunTlsPort = 5349; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | static const char kTransport[] = "transport"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 57 | static const char kUdpTransportType[] = "udp"; |
| 58 | static const char kTcpTransportType[] = "tcp"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
| 60 | // NOTE: Must be in the same order as the ServiceType enum. |
| 61 | static const char* kValidIceServiceTypes[] = { |
| 62 | "stun", "stuns", "turn", "turns", "invalid" }; |
| 63 | |
| 64 | enum ServiceType { |
| 65 | STUN, // Indicates a STUN server. |
| 66 | STUNS, // Indicates a STUN server used with a TLS session. |
| 67 | TURN, // Indicates a TURN server |
| 68 | TURNS, // Indicates a TURN server used with a TLS session. |
| 69 | INVALID, // Unknown. |
| 70 | }; |
| 71 | |
| 72 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 73 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 75 | MSG_GETSTATS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 78 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | explicit SetSessionDescriptionMsg( |
| 80 | webrtc::SetSessionDescriptionObserver* observer) |
| 81 | : observer(observer) { |
| 82 | } |
| 83 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 84 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | std::string error; |
| 86 | }; |
| 87 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 88 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 89 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 90 | webrtc::MediaStreamTrackInterface* track) |
| 91 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 93 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 94 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 97 | // |in_str| should be of format |
| 98 | // stunURI = scheme ":" stun-host [ ":" stun-port ] |
| 99 | // scheme = "stun" / "stuns" |
| 100 | // stun-host = IP-literal / IPv4address / reg-name |
| 101 | // stun-port = *DIGIT |
| 102 | |
| 103 | // draft-petithuguenin-behave-turn-uris-01 |
| 104 | // turnURI = scheme ":" turn-host [ ":" turn-port ] |
| 105 | // turn-host = username@IP-literal / IPv4address / reg-name |
| 106 | bool GetServiceTypeAndHostnameFromUri(const std::string& in_str, |
| 107 | ServiceType* service_type, |
| 108 | std::string* hostname) { |
Tommi | 77d444a | 2015-04-24 15:38:38 +0200 | [diff] [blame] | 109 | const std::string::size_type colonpos = in_str.find(':'); |
| 110 | if (colonpos == std::string::npos || (colonpos + 1) == in_str.length()) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | std::string type = in_str.substr(0, colonpos); |
| 114 | for (size_t i = 0; i < ARRAY_SIZE(kValidIceServiceTypes); ++i) { |
| 115 | if (type.compare(kValidIceServiceTypes[i]) == 0) { |
| 116 | *service_type = static_cast<ServiceType>(i); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | if (*service_type == INVALID) { |
| 121 | return false; |
| 122 | } |
| 123 | *hostname = in_str.substr(colonpos + 1, std::string::npos); |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | // This method parses IPv6 and IPv4 literal strings, along with hostnames in |
| 128 | // standard hostname:port format. |
| 129 | // Consider following formats as correct. |
| 130 | // |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port, |
| 131 | // |hostname|, |[IPv6 address]|, |IPv4 address| |
| 132 | bool ParseHostnameAndPortFromString(const std::string& in_str, |
| 133 | std::string* host, |
| 134 | int* port) { |
| 135 | if (in_str.at(0) == '[') { |
| 136 | std::string::size_type closebracket = in_str.rfind(']'); |
| 137 | if (closebracket != std::string::npos) { |
| 138 | *host = in_str.substr(1, closebracket - 1); |
| 139 | std::string::size_type colonpos = in_str.find(':', closebracket); |
| 140 | if (std::string::npos != colonpos) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 141 | if (!rtc::FromString( |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 142 | in_str.substr(closebracket + 2, std::string::npos), port)) { |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | } else { |
| 147 | return false; |
| 148 | } |
| 149 | } else { |
| 150 | std::string::size_type colonpos = in_str.find(':'); |
| 151 | if (std::string::npos != colonpos) { |
| 152 | *host = in_str.substr(0, colonpos); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 153 | if (!rtc::FromString( |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 154 | in_str.substr(colonpos + 1, std::string::npos), port)) { |
| 155 | return false; |
| 156 | } |
| 157 | } else { |
| 158 | *host = in_str; |
| 159 | } |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | typedef webrtc::PortAllocatorFactoryInterface::StunConfiguration |
| 165 | StunConfiguration; |
| 166 | typedef webrtc::PortAllocatorFactoryInterface::TurnConfiguration |
| 167 | TurnConfiguration; |
| 168 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 169 | bool ParseIceServerUrl(const PeerConnectionInterface::IceServer& server, |
| 170 | const std::string& url, |
| 171 | std::vector<StunConfiguration>* stun_config, |
| 172 | std::vector<TurnConfiguration>* turn_config) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | // draft-nandakumar-rtcweb-stun-uri-01 |
| 174 | // stunURI = scheme ":" stun-host [ ":" stun-port ] |
| 175 | // scheme = "stun" / "stuns" |
| 176 | // stun-host = IP-literal / IPv4address / reg-name |
| 177 | // stun-port = *DIGIT |
| 178 | |
| 179 | // draft-petithuguenin-behave-turn-uris-01 |
| 180 | // turnURI = scheme ":" turn-host [ ":" turn-port ] |
| 181 | // [ "?transport=" transport ] |
| 182 | // scheme = "turn" / "turns" |
| 183 | // transport = "udp" / "tcp" / transport-ext |
| 184 | // transport-ext = 1*unreserved |
| 185 | // turn-host = IP-literal / IPv4address / reg-name |
| 186 | // turn-port = *DIGIT |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 187 | std::vector<std::string> tokens; |
| 188 | std::string turn_transport_type = kUdpTransportType; |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 189 | ASSERT(!url.empty()); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 190 | rtc::tokenize(url, '?', &tokens); |
| 191 | std::string uri_without_transport = tokens[0]; |
| 192 | // Let's look into transport= param, if it exists. |
| 193 | if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present. |
| 194 | std::string uri_transport_param = tokens[1]; |
| 195 | rtc::tokenize(uri_transport_param, '=', &tokens); |
| 196 | if (tokens[0] == kTransport) { |
| 197 | // As per above grammar transport param will be consist of lower case |
| 198 | // letters. |
| 199 | if (tokens[1] != kUdpTransportType && tokens[1] != kTcpTransportType) { |
| 200 | LOG(LS_WARNING) << "Transport param should always be udp or tcp."; |
| 201 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 202 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 203 | turn_transport_type = tokens[1]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 205 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 206 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 207 | std::string hoststring; |
| 208 | ServiceType service_type = INVALID; |
| 209 | if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, |
| 210 | &service_type, |
| 211 | &hoststring)) { |
| 212 | LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " |
| 213 | << uri_without_transport; |
| 214 | return true; |
| 215 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 216 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 217 | ASSERT(!hoststring.empty()); |
Tommi | 77d444a | 2015-04-24 15:38:38 +0200 | [diff] [blame] | 218 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 219 | // Let's break hostname. |
| 220 | tokens.clear(); |
| 221 | rtc::tokenize(hoststring, '@', &tokens); |
| 222 | ASSERT(!tokens.empty()); |
| 223 | std::string username(server.username); |
| 224 | // TODO(pthatcher): What's the right thing to do if tokens.size() is >2? |
| 225 | // E.g. a string like "foo@bar@bat". |
| 226 | if (tokens.size() >= kTurnHostTokensNum) { |
| 227 | username.assign(rtc::s_url_decode(tokens[0])); |
| 228 | hoststring = tokens[1]; |
| 229 | } else { |
| 230 | hoststring = tokens[0]; |
| 231 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 232 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 233 | int port = kDefaultStunPort; |
| 234 | if (service_type == TURNS) { |
| 235 | port = kDefaultStunTlsPort; |
| 236 | turn_transport_type = kTcpTransportType; |
| 237 | } |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 238 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 239 | std::string address; |
| 240 | if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) { |
| 241 | LOG(WARNING) << "Invalid Hostname format: " << uri_without_transport; |
| 242 | return true; |
| 243 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 244 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 245 | if (port <= 0 || port > 0xffff) { |
| 246 | LOG(WARNING) << "Invalid port: " << port; |
| 247 | return true; |
| 248 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 250 | switch (service_type) { |
| 251 | case STUN: |
| 252 | case STUNS: |
| 253 | stun_config->push_back(StunConfiguration(address, port)); |
| 254 | break; |
| 255 | case TURN: |
| 256 | case TURNS: { |
| 257 | if (username.empty()) { |
| 258 | // Turn url example from the spec |url:"turn:user@turn.example.org"|. |
| 259 | std::vector<std::string> turn_tokens; |
| 260 | rtc::tokenize(address, '@', &turn_tokens); |
| 261 | if (turn_tokens.size() == kTurnHostTokensNum) { |
| 262 | username.assign(rtc::s_url_decode(turn_tokens[0])); |
| 263 | address = turn_tokens[1]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 264 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 265 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 266 | |
| 267 | bool secure = (service_type == TURNS); |
| 268 | |
| 269 | turn_config->push_back(TurnConfiguration(address, port, |
| 270 | username, |
| 271 | server.password, |
| 272 | turn_transport_type, |
| 273 | secure)); |
| 274 | break; |
| 275 | } |
| 276 | case INVALID: |
| 277 | default: |
| 278 | LOG(WARNING) << "Configuration not supported: " << url; |
| 279 | return false; |
| 280 | } |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | bool ParseIceServers(const PeerConnectionInterface::IceServers& servers, |
| 285 | std::vector<StunConfiguration>* stun_config, |
| 286 | std::vector<TurnConfiguration>* turn_config) { |
| 287 | for (const webrtc::PeerConnectionInterface::IceServer& server : servers) { |
| 288 | if (!server.urls.empty()) { |
| 289 | for (const std::string& url : server.urls) { |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 290 | if (url.empty()) { |
| 291 | LOG(WARNING) << "Empty uri."; |
| 292 | continue; |
| 293 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 294 | if (!ParseIceServerUrl(server, url, stun_config, turn_config)) { |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | } else if (!server.uri.empty()) { |
| 299 | // Fallback to old .uri if new .urls isn't present. |
| 300 | if (!ParseIceServerUrl(server, server.uri, stun_config, turn_config)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 301 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 302 | } |
| 303 | } else { |
| 304 | LOG(WARNING) << "Empty uri."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | // Check if we can send |new_stream| on a PeerConnection. |
| 311 | // Currently only one audio but multiple video track is supported per |
| 312 | // PeerConnection. |
| 313 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 314 | webrtc::MediaStreamInterface* new_stream) { |
| 315 | if (!new_stream || !current_streams) |
| 316 | return false; |
| 317 | if (current_streams->find(new_stream->label()) != NULL) { |
| 318 | LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 319 | << " is already added."; |
| 320 | return false; |
| 321 | } |
| 322 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 323 | return true; |
| 324 | } |
| 325 | |
| 326 | } // namespace |
| 327 | |
| 328 | namespace webrtc { |
| 329 | |
| 330 | PeerConnection::PeerConnection(PeerConnectionFactory* factory) |
| 331 | : factory_(factory), |
| 332 | observer_(NULL), |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 333 | uma_observer_(NULL), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 334 | signaling_state_(kStable), |
| 335 | ice_state_(kIceNew), |
| 336 | ice_connection_state_(kIceConnectionNew), |
| 337 | ice_gathering_state_(kIceGatheringNew) { |
| 338 | } |
| 339 | |
| 340 | PeerConnection::~PeerConnection() { |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame^] | 341 | ASSERT(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 342 | if (mediastream_signaling_) |
| 343 | mediastream_signaling_->TearDown(); |
| 344 | if (stream_handler_container_) |
| 345 | stream_handler_container_->TearDown(); |
| 346 | } |
| 347 | |
| 348 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 349 | const PeerConnectionInterface::RTCConfiguration& configuration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 350 | const MediaConstraintsInterface* constraints, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 351 | PortAllocatorFactoryInterface* allocator_factory, |
| 352 | DTLSIdentityServiceInterface* dtls_identity_service, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 353 | PeerConnectionObserver* observer) { |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 354 | ASSERT(observer != NULL); |
| 355 | if (!observer) |
| 356 | return false; |
| 357 | observer_ = observer; |
| 358 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 359 | std::vector<PortAllocatorFactoryInterface::StunConfiguration> stun_config; |
| 360 | std::vector<PortAllocatorFactoryInterface::TurnConfiguration> turn_config; |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 361 | if (!ParseIceServers(configuration.servers, &stun_config, &turn_config)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 362 | return false; |
| 363 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 364 | port_allocator_.reset( |
| 365 | allocator_factory->CreatePortAllocator(stun_config, turn_config)); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 366 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | // To handle both internal and externally created port allocator, we will |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 368 | // enable BUNDLE here. |
braveyao@webrtc.org | 1732df6 | 2014-10-27 03:01:37 +0000 | [diff] [blame] | 369 | int portallocator_flags = port_allocator_->flags(); |
Donald Curtis | 0e209b0 | 2015-03-24 09:29:54 -0700 | [diff] [blame] | 370 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 371 | cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 372 | cricket::PORTALLOCATOR_ENABLE_IPV6; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 373 | bool value; |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 374 | // If IPv6 flag was specified, we'll not override it by experiment. |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 375 | if (FindConstraint( |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 376 | constraints, MediaConstraintsInterface::kEnableIPv6, &value, NULL)) { |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 377 | if (!value) { |
| 378 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 379 | } |
guoweis@webrtc.org | 2c1bcea | 2014-09-23 16:23:02 +0000 | [diff] [blame] | 380 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") == |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 381 | "Disabled") { |
| 382 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 385 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 386 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
| 387 | LOG(LS_INFO) << "TCP candidates are disabled."; |
| 388 | } |
| 389 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 390 | port_allocator_->set_flags(portallocator_flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 391 | // No step delay is used while allocating ports. |
| 392 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 393 | |
| 394 | mediastream_signaling_.reset(new MediaStreamSignaling( |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 395 | factory_->signaling_thread(), this, factory_->channel_manager())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 396 | |
| 397 | session_.reset(new WebRtcSession(factory_->channel_manager(), |
| 398 | factory_->signaling_thread(), |
| 399 | factory_->worker_thread(), |
| 400 | port_allocator_.get(), |
| 401 | mediastream_signaling_.get())); |
| 402 | stream_handler_container_.reset(new MediaStreamHandlerContainer( |
| 403 | session_.get(), session_.get())); |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 404 | stats_.reset(new StatsCollector(session_.get())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 405 | |
| 406 | // Initialize the WebRtcSession. It creates transport channels etc. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 407 | if (!session_->Initialize(factory_->options(), constraints, |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 408 | dtls_identity_service, configuration)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 409 | return false; |
| 410 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 411 | // Register PeerConnection as receiver of local ice candidates. |
| 412 | // All the callbacks will be posted to the application from PeerConnection. |
| 413 | session_->RegisterIceObserver(this); |
| 414 | session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); |
| 415 | return true; |
| 416 | } |
| 417 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 418 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 419 | PeerConnection::local_streams() { |
| 420 | return mediastream_signaling_->local_streams(); |
| 421 | } |
| 422 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 423 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 424 | PeerConnection::remote_streams() { |
| 425 | return mediastream_signaling_->remote_streams(); |
| 426 | } |
| 427 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 428 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | if (IsClosed()) { |
| 430 | return false; |
| 431 | } |
| 432 | if (!CanAddLocalMediaStream(mediastream_signaling_->local_streams(), |
| 433 | local_stream)) |
| 434 | return false; |
| 435 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | if (!mediastream_signaling_->AddLocalStream(local_stream)) { |
| 437 | return false; |
| 438 | } |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 439 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 440 | observer_->OnRenegotiationNeeded(); |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
fischman@webrtc.org | 32001ef | 2013-08-12 23:26:21 +0000 | [diff] [blame] | 445 | mediastream_signaling_->RemoveLocalStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 446 | if (IsClosed()) { |
| 447 | return; |
| 448 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 449 | observer_->OnRenegotiationNeeded(); |
| 450 | } |
| 451 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 452 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 453 | AudioTrackInterface* track) { |
| 454 | if (!track) { |
| 455 | LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
| 456 | return NULL; |
| 457 | } |
| 458 | if (!mediastream_signaling_->local_streams()->FindAudioTrack(track->id())) { |
| 459 | LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track."; |
| 460 | return NULL; |
| 461 | } |
| 462 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 463 | rtc::scoped_refptr<DtmfSenderInterface> sender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 464 | DtmfSender::Create(track, signaling_thread(), session_.get())); |
| 465 | if (!sender.get()) { |
| 466 | LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create."; |
| 467 | return NULL; |
| 468 | } |
| 469 | return DtmfSenderProxy::Create(signaling_thread(), sender.get()); |
| 470 | } |
| 471 | |
| 472 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 473 | MediaStreamTrackInterface* track, |
| 474 | StatsOutputLevel level) { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 475 | ASSERT(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 476 | if (!VERIFY(observer != NULL)) { |
| 477 | LOG(LS_ERROR) << "GetStats - observer is NULL."; |
| 478 | return false; |
| 479 | } |
| 480 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 481 | stats_->UpdateStats(level); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 482 | signaling_thread()->Post(this, MSG_GETSTATS, |
| 483 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
| 487 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 488 | return signaling_state_; |
| 489 | } |
| 490 | |
| 491 | PeerConnectionInterface::IceState PeerConnection::ice_state() { |
| 492 | return ice_state_; |
| 493 | } |
| 494 | |
| 495 | PeerConnectionInterface::IceConnectionState |
| 496 | PeerConnection::ice_connection_state() { |
| 497 | return ice_connection_state_; |
| 498 | } |
| 499 | |
| 500 | PeerConnectionInterface::IceGatheringState |
| 501 | PeerConnection::ice_gathering_state() { |
| 502 | return ice_gathering_state_; |
| 503 | } |
| 504 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 505 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 506 | PeerConnection::CreateDataChannel( |
| 507 | const std::string& label, |
| 508 | const DataChannelInit* config) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 509 | bool first_datachannel = !mediastream_signaling_->HasDataChannels(); |
| 510 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 511 | rtc::scoped_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 512 | if (config) { |
| 513 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 514 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 515 | rtc::scoped_refptr<DataChannelInterface> channel( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 516 | session_->CreateDataChannel(label, internal_config.get())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 517 | if (!channel.get()) |
| 518 | return NULL; |
| 519 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 520 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 521 | // the first SCTP DataChannel. |
| 522 | if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
| 523 | observer_->OnRenegotiationNeeded(); |
| 524 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 525 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 526 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 527 | } |
| 528 | |
| 529 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 530 | const MediaConstraintsInterface* constraints) { |
| 531 | if (!VERIFY(observer != NULL)) { |
| 532 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 533 | return; |
| 534 | } |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 535 | RTCOfferAnswerOptions options; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 536 | |
| 537 | bool value; |
| 538 | size_t mandatory_constraints = 0; |
| 539 | |
| 540 | if (FindConstraint(constraints, |
| 541 | MediaConstraintsInterface::kOfferToReceiveAudio, |
| 542 | &value, |
| 543 | &mandatory_constraints)) { |
| 544 | options.offer_to_receive_audio = |
| 545 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 546 | } |
| 547 | |
| 548 | if (FindConstraint(constraints, |
| 549 | MediaConstraintsInterface::kOfferToReceiveVideo, |
| 550 | &value, |
| 551 | &mandatory_constraints)) { |
| 552 | options.offer_to_receive_video = |
| 553 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 554 | } |
| 555 | |
| 556 | if (FindConstraint(constraints, |
| 557 | MediaConstraintsInterface::kVoiceActivityDetection, |
| 558 | &value, |
| 559 | &mandatory_constraints)) { |
| 560 | options.voice_activity_detection = value; |
| 561 | } |
| 562 | |
| 563 | if (FindConstraint(constraints, |
| 564 | MediaConstraintsInterface::kIceRestart, |
| 565 | &value, |
| 566 | &mandatory_constraints)) { |
| 567 | options.ice_restart = value; |
| 568 | } |
| 569 | |
| 570 | if (FindConstraint(constraints, |
| 571 | MediaConstraintsInterface::kUseRtpMux, |
| 572 | &value, |
| 573 | &mandatory_constraints)) { |
| 574 | options.use_rtp_mux = value; |
| 575 | } |
| 576 | |
| 577 | CreateOffer(observer, options); |
| 578 | } |
| 579 | |
| 580 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 581 | const RTCOfferAnswerOptions& options) { |
| 582 | if (!VERIFY(observer != NULL)) { |
| 583 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 584 | return; |
| 585 | } |
| 586 | session_->CreateOffer(observer, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void PeerConnection::CreateAnswer( |
| 590 | CreateSessionDescriptionObserver* observer, |
| 591 | const MediaConstraintsInterface* constraints) { |
| 592 | if (!VERIFY(observer != NULL)) { |
| 593 | LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 594 | return; |
| 595 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 596 | session_->CreateAnswer(observer, constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void PeerConnection::SetLocalDescription( |
| 600 | SetSessionDescriptionObserver* observer, |
| 601 | SessionDescriptionInterface* desc) { |
| 602 | if (!VERIFY(observer != NULL)) { |
| 603 | LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
| 604 | return; |
| 605 | } |
| 606 | if (!desc) { |
| 607 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 608 | return; |
| 609 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 610 | // Update stats here so that we have the most recent stats for tracks and |
| 611 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 612 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 613 | std::string error; |
| 614 | if (!session_->SetLocalDescription(desc, &error)) { |
| 615 | PostSetSessionDescriptionFailure(observer, error); |
| 616 | return; |
| 617 | } |
| 618 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 619 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 620 | } |
| 621 | |
| 622 | void PeerConnection::SetRemoteDescription( |
| 623 | SetSessionDescriptionObserver* observer, |
| 624 | SessionDescriptionInterface* desc) { |
| 625 | if (!VERIFY(observer != NULL)) { |
| 626 | LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
| 627 | return; |
| 628 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 629 | if (!desc) { |
| 630 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 631 | return; |
| 632 | } |
| 633 | // Update stats here so that we have the most recent stats for tracks and |
| 634 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 635 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 636 | std::string error; |
| 637 | if (!session_->SetRemoteDescription(desc, &error)) { |
| 638 | PostSetSessionDescriptionFailure(observer, error); |
| 639 | return; |
| 640 | } |
| 641 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 642 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 643 | } |
| 644 | |
| 645 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 646 | SetSessionDescriptionObserver* observer, |
| 647 | const std::string& error) { |
| 648 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 649 | msg->error = error; |
| 650 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
| 651 | } |
| 652 | |
| 653 | bool PeerConnection::UpdateIce(const IceServers& configuration, |
| 654 | const MediaConstraintsInterface* constraints) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 658 | bool PeerConnection::UpdateIce(const RTCConfiguration& config) { |
| 659 | if (port_allocator_) { |
| 660 | std::vector<PortAllocatorFactoryInterface::StunConfiguration> stuns; |
| 661 | std::vector<PortAllocatorFactoryInterface::TurnConfiguration> turns; |
| 662 | if (!ParseIceServers(config.servers, &stuns, &turns)) { |
| 663 | return false; |
| 664 | } |
| 665 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 666 | std::vector<rtc::SocketAddress> stun_hosts; |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 667 | typedef std::vector<StunConfiguration>::const_iterator StunIt; |
| 668 | for (StunIt stun_it = stuns.begin(); stun_it != stuns.end(); ++stun_it) { |
| 669 | stun_hosts.push_back(stun_it->server); |
| 670 | } |
| 671 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 672 | rtc::SocketAddress stun_addr; |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 673 | if (!stun_hosts.empty()) { |
| 674 | stun_addr = stun_hosts.front(); |
| 675 | LOG(LS_INFO) << "UpdateIce: StunServer Address: " << stun_addr.ToString(); |
| 676 | } |
| 677 | |
| 678 | for (size_t i = 0; i < turns.size(); ++i) { |
| 679 | cricket::RelayCredentials credentials(turns[i].username, |
| 680 | turns[i].password); |
| 681 | cricket::RelayServerConfig relay_server(cricket::RELAY_TURN); |
| 682 | cricket::ProtocolType protocol; |
| 683 | if (cricket::StringToProto(turns[i].transport_type.c_str(), &protocol)) { |
| 684 | relay_server.ports.push_back(cricket::ProtocolAddress( |
| 685 | turns[i].server, protocol, turns[i].secure)); |
| 686 | relay_server.credentials = credentials; |
| 687 | LOG(LS_INFO) << "UpdateIce: TurnServer Address: " |
| 688 | << turns[i].server.ToString(); |
| 689 | } else { |
| 690 | LOG(LS_WARNING) << "Ignoring TURN server " << turns[i].server << ". " |
| 691 | << "Reason= Incorrect " << turns[i].transport_type |
| 692 | << " transport parameter."; |
| 693 | } |
| 694 | } |
| 695 | } |
mallinath@webrtc.org | 3d81b1b | 2014-09-09 14:38:10 +0000 | [diff] [blame] | 696 | return session_->SetIceTransports(config.type); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 697 | } |
| 698 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 699 | bool PeerConnection::AddIceCandidate( |
| 700 | const IceCandidateInterface* ice_candidate) { |
| 701 | return session_->ProcessIceMessage(ice_candidate); |
| 702 | } |
| 703 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 704 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
| 705 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 706 | |
| 707 | if (session_) { |
| 708 | session_->set_metrics_observer(uma_observer_); |
| 709 | } |
| 710 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 711 | // Send information about IPv4/IPv6 status. |
| 712 | if (uma_observer_ && port_allocator_) { |
| 713 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 714 | uma_observer_->IncrementCounter(kPeerConnection_IPv6); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 715 | } else { |
| 716 | uma_observer_->IncrementCounter(kPeerConnection_IPv4); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 719 | } |
| 720 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 721 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
| 722 | return session_->local_description(); |
| 723 | } |
| 724 | |
| 725 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
| 726 | return session_->remote_description(); |
| 727 | } |
| 728 | |
| 729 | void PeerConnection::Close() { |
| 730 | // Update stats here so that we have the most recent stats for tracks and |
| 731 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 732 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 733 | |
| 734 | session_->Terminate(); |
| 735 | } |
| 736 | |
| 737 | void PeerConnection::OnSessionStateChange(cricket::BaseSession* /*session*/, |
| 738 | cricket::BaseSession::State state) { |
| 739 | switch (state) { |
| 740 | case cricket::BaseSession::STATE_INIT: |
| 741 | ChangeSignalingState(PeerConnectionInterface::kStable); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 742 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 743 | case cricket::BaseSession::STATE_SENTINITIATE: |
| 744 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer); |
| 745 | break; |
| 746 | case cricket::BaseSession::STATE_SENTPRACCEPT: |
| 747 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer); |
| 748 | break; |
| 749 | case cricket::BaseSession::STATE_RECEIVEDINITIATE: |
| 750 | ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer); |
| 751 | break; |
| 752 | case cricket::BaseSession::STATE_RECEIVEDPRACCEPT: |
| 753 | ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer); |
| 754 | break; |
| 755 | case cricket::BaseSession::STATE_SENTACCEPT: |
| 756 | case cricket::BaseSession::STATE_RECEIVEDACCEPT: |
| 757 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 758 | break; |
| 759 | case cricket::BaseSession::STATE_RECEIVEDTERMINATE: |
| 760 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 761 | break; |
| 762 | default: |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 767 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 768 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 769 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 770 | SetSessionDescriptionMsg* param = |
| 771 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 772 | param->observer->OnSuccess(); |
| 773 | delete param; |
| 774 | break; |
| 775 | } |
| 776 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 777 | SetSessionDescriptionMsg* param = |
| 778 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 779 | param->observer->OnFailure(param->error); |
| 780 | delete param; |
| 781 | break; |
| 782 | } |
| 783 | case MSG_GETSTATS: { |
| 784 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 785 | StatsReports reports; |
| 786 | stats_->GetStats(param->track, &reports); |
| 787 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 788 | delete param; |
| 789 | break; |
| 790 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 791 | default: |
| 792 | ASSERT(false && "Not implemented"); |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | void PeerConnection::OnAddRemoteStream(MediaStreamInterface* stream) { |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 798 | stats_->AddStream(stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 799 | observer_->OnAddStream(stream); |
| 800 | } |
| 801 | |
| 802 | void PeerConnection::OnRemoveRemoteStream(MediaStreamInterface* stream) { |
| 803 | stream_handler_container_->RemoveRemoteStream(stream); |
| 804 | observer_->OnRemoveStream(stream); |
| 805 | } |
| 806 | |
| 807 | void PeerConnection::OnAddDataChannel(DataChannelInterface* data_channel) { |
| 808 | observer_->OnDataChannel(DataChannelProxy::Create(signaling_thread(), |
| 809 | data_channel)); |
| 810 | } |
| 811 | |
| 812 | void PeerConnection::OnAddRemoteAudioTrack(MediaStreamInterface* stream, |
| 813 | AudioTrackInterface* audio_track, |
| 814 | uint32 ssrc) { |
| 815 | stream_handler_container_->AddRemoteAudioTrack(stream, audio_track, ssrc); |
| 816 | } |
| 817 | |
| 818 | void PeerConnection::OnAddRemoteVideoTrack(MediaStreamInterface* stream, |
| 819 | VideoTrackInterface* video_track, |
| 820 | uint32 ssrc) { |
| 821 | stream_handler_container_->AddRemoteVideoTrack(stream, video_track, ssrc); |
| 822 | } |
| 823 | |
| 824 | void PeerConnection::OnRemoveRemoteAudioTrack( |
| 825 | MediaStreamInterface* stream, |
| 826 | AudioTrackInterface* audio_track) { |
| 827 | stream_handler_container_->RemoveRemoteTrack(stream, audio_track); |
| 828 | } |
| 829 | |
| 830 | void PeerConnection::OnRemoveRemoteVideoTrack( |
| 831 | MediaStreamInterface* stream, |
| 832 | VideoTrackInterface* video_track) { |
| 833 | stream_handler_container_->RemoveRemoteTrack(stream, video_track); |
| 834 | } |
| 835 | void PeerConnection::OnAddLocalAudioTrack(MediaStreamInterface* stream, |
| 836 | AudioTrackInterface* audio_track, |
| 837 | uint32 ssrc) { |
| 838 | stream_handler_container_->AddLocalAudioTrack(stream, audio_track, ssrc); |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 839 | stats_->AddLocalAudioTrack(audio_track, ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 840 | } |
| 841 | void PeerConnection::OnAddLocalVideoTrack(MediaStreamInterface* stream, |
| 842 | VideoTrackInterface* video_track, |
| 843 | uint32 ssrc) { |
| 844 | stream_handler_container_->AddLocalVideoTrack(stream, video_track, ssrc); |
| 845 | } |
| 846 | |
| 847 | void PeerConnection::OnRemoveLocalAudioTrack(MediaStreamInterface* stream, |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 848 | AudioTrackInterface* audio_track, |
| 849 | uint32 ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 850 | stream_handler_container_->RemoveLocalTrack(stream, audio_track); |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 851 | stats_->RemoveLocalAudioTrack(audio_track, ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void PeerConnection::OnRemoveLocalVideoTrack(MediaStreamInterface* stream, |
| 855 | VideoTrackInterface* video_track) { |
| 856 | stream_handler_container_->RemoveLocalTrack(stream, video_track); |
| 857 | } |
| 858 | |
| 859 | void PeerConnection::OnRemoveLocalStream(MediaStreamInterface* stream) { |
| 860 | stream_handler_container_->RemoveLocalStream(stream); |
| 861 | } |
| 862 | |
| 863 | void PeerConnection::OnIceConnectionChange( |
| 864 | PeerConnectionInterface::IceConnectionState new_state) { |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 865 | ASSERT(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 866 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 867 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | void PeerConnection::OnIceGatheringChange( |
| 871 | PeerConnectionInterface::IceGatheringState new_state) { |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 872 | ASSERT(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 873 | if (IsClosed()) { |
| 874 | return; |
| 875 | } |
| 876 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 877 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) { |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 881 | ASSERT(signaling_thread()->IsCurrent()); |
| 882 | observer_->OnIceCandidate(candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | void PeerConnection::OnIceComplete() { |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 886 | ASSERT(signaling_thread()->IsCurrent()); |
| 887 | observer_->OnIceComplete(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 890 | void PeerConnection::OnIceConnectionReceivingChange(bool receiving) { |
| 891 | ASSERT(signaling_thread()->IsCurrent()); |
| 892 | observer_->OnIceConnectionReceivingChange(receiving); |
| 893 | } |
| 894 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 895 | void PeerConnection::ChangeSignalingState( |
| 896 | PeerConnectionInterface::SignalingState signaling_state) { |
| 897 | signaling_state_ = signaling_state; |
| 898 | if (signaling_state == kClosed) { |
| 899 | ice_connection_state_ = kIceConnectionClosed; |
| 900 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 901 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 902 | ice_gathering_state_ = kIceGatheringComplete; |
| 903 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 904 | } |
| 905 | } |
| 906 | observer_->OnSignalingChange(signaling_state_); |
| 907 | observer_->OnStateChange(PeerConnectionObserver::kSignalingState); |
| 908 | } |
| 909 | |
| 910 | } // namespace webrtc |