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