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 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 33 | #include "talk/app/webrtc/audiotrack.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #include "talk/app/webrtc/dtmfsender.h" |
| 35 | #include "talk/app/webrtc/jsepicecandidate.h" |
| 36 | #include "talk/app/webrtc/jsepsessiondescription.h" |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 37 | #include "talk/app/webrtc/mediaconstraintsinterface.h" |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 38 | #include "talk/app/webrtc/mediastream.h" |
| 39 | #include "talk/app/webrtc/mediastreamproxy.h" |
| 40 | #include "talk/app/webrtc/mediastreamtrackproxy.h" |
| 41 | #include "talk/app/webrtc/remoteaudiosource.h" |
| 42 | #include "talk/app/webrtc/remotevideocapturer.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 43 | #include "talk/app/webrtc/rtpreceiver.h" |
| 44 | #include "talk/app/webrtc/rtpsender.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | #include "talk/app/webrtc/streamcollection.h" |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 46 | #include "talk/app/webrtc/videosource.h" |
| 47 | #include "talk/app/webrtc/videotrack.h" |
| 48 | #include "talk/media/sctp/sctpdataengine.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | #include "webrtc/p2p/client/basicportallocator.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | #include "talk/session/media/channelmanager.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 51 | #include "webrtc/base/logging.h" |
| 52 | #include "webrtc/base/stringencode.h" |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 53 | #include "webrtc/base/stringutils.h" |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 54 | #include "webrtc/system_wrappers/interface/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | |
| 56 | namespace { |
| 57 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 58 | using webrtc::DataChannel; |
| 59 | using webrtc::MediaConstraintsInterface; |
| 60 | using webrtc::MediaStreamInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | using webrtc::PeerConnectionInterface; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 62 | using webrtc::StreamCollection; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 63 | using webrtc::StunConfigurations; |
| 64 | using webrtc::TurnConfigurations; |
| 65 | typedef webrtc::PortAllocatorFactoryInterface::StunConfiguration |
| 66 | StunConfiguration; |
| 67 | typedef webrtc::PortAllocatorFactoryInterface::TurnConfiguration |
| 68 | TurnConfiguration; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 70 | static const char kDefaultStreamLabel[] = "default"; |
| 71 | static const char kDefaultAudioTrackLabel[] = "defaulta0"; |
| 72 | static const char kDefaultVideoTrackLabel[] = "defaultv0"; |
| 73 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | // The min number of tokens must present in Turn host uri. |
| 75 | // e.g. user@turn.example.org |
| 76 | static const size_t kTurnHostTokensNum = 2; |
| 77 | // Number of tokens must be preset when TURN uri has transport param. |
| 78 | static const size_t kTurnTransportTokensNum = 2; |
| 79 | // The default stun port. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 80 | static const int kDefaultStunPort = 3478; |
| 81 | static const int kDefaultStunTlsPort = 5349; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | static const char kTransport[] = "transport"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 83 | static const char kUdpTransportType[] = "udp"; |
| 84 | static const char kTcpTransportType[] = "tcp"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | |
| 86 | // NOTE: Must be in the same order as the ServiceType enum. |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 87 | static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 89 | // NOTE: A loop below assumes that the first value of this enum is 0 and all |
| 90 | // other values are incremental. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | enum ServiceType { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 92 | STUN = 0, // Indicates a STUN server. |
| 93 | STUNS, // Indicates a STUN server used with a TLS session. |
| 94 | TURN, // Indicates a TURN server |
| 95 | TURNS, // Indicates a TURN server used with a TLS session. |
| 96 | INVALID, // Unknown. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | }; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 98 | static_assert(INVALID == ARRAY_SIZE(kValidIceServiceTypes), |
| 99 | "kValidIceServiceTypes must have as many strings as ServiceType " |
| 100 | "has values."); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | |
| 102 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 103 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 105 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | MSG_GETSTATS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 109 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | explicit SetSessionDescriptionMsg( |
| 111 | webrtc::SetSessionDescriptionObserver* observer) |
| 112 | : observer(observer) { |
| 113 | } |
| 114 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 115 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | std::string error; |
| 117 | }; |
| 118 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 119 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 120 | explicit CreateSessionDescriptionMsg( |
| 121 | webrtc::CreateSessionDescriptionObserver* observer) |
| 122 | : observer(observer) {} |
| 123 | |
| 124 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 125 | std::string error; |
| 126 | }; |
| 127 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 128 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 129 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 130 | webrtc::MediaStreamTrackInterface* track) |
| 131 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 133 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 134 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 137 | // |in_str| should be of format |
| 138 | // stunURI = scheme ":" stun-host [ ":" stun-port ] |
| 139 | // scheme = "stun" / "stuns" |
| 140 | // stun-host = IP-literal / IPv4address / reg-name |
| 141 | // stun-port = *DIGIT |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 142 | // |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 143 | // draft-petithuguenin-behave-turn-uris-01 |
| 144 | // turnURI = scheme ":" turn-host [ ":" turn-port ] |
| 145 | // turn-host = username@IP-literal / IPv4address / reg-name |
| 146 | bool GetServiceTypeAndHostnameFromUri(const std::string& in_str, |
| 147 | ServiceType* service_type, |
| 148 | std::string* hostname) { |
Tommi | 77d444a | 2015-04-24 15:38:38 +0200 | [diff] [blame] | 149 | const std::string::size_type colonpos = in_str.find(':'); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 150 | if (colonpos == std::string::npos) { |
| 151 | LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 152 | return false; |
| 153 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 154 | if ((colonpos + 1) == in_str.length()) { |
| 155 | LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str; |
| 156 | return false; |
| 157 | } |
| 158 | *service_type = INVALID; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 159 | for (size_t i = 0; i < ARRAY_SIZE(kValidIceServiceTypes); ++i) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 160 | if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 161 | *service_type = static_cast<ServiceType>(i); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | if (*service_type == INVALID) { |
| 166 | return false; |
| 167 | } |
| 168 | *hostname = in_str.substr(colonpos + 1, std::string::npos); |
| 169 | return true; |
| 170 | } |
| 171 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 172 | bool ParsePort(const std::string& in_str, int* port) { |
| 173 | // Make sure port only contains digits. FromString doesn't check this. |
| 174 | for (const char& c : in_str) { |
| 175 | if (!std::isdigit(c)) { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | return rtc::FromString(in_str, port); |
| 180 | } |
| 181 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 182 | // This method parses IPv6 and IPv4 literal strings, along with hostnames in |
| 183 | // standard hostname:port format. |
| 184 | // Consider following formats as correct. |
| 185 | // |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port, |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 186 | // |hostname|, |[IPv6 address]|, |IPv4 address|. |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 187 | bool ParseHostnameAndPortFromString(const std::string& in_str, |
| 188 | std::string* host, |
| 189 | int* port) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 190 | RTC_DCHECK(host->empty()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 191 | if (in_str.at(0) == '[') { |
| 192 | std::string::size_type closebracket = in_str.rfind(']'); |
| 193 | if (closebracket != std::string::npos) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 194 | std::string::size_type colonpos = in_str.find(':', closebracket); |
| 195 | if (std::string::npos != colonpos) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 196 | if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos), |
| 197 | port)) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 201 | *host = in_str.substr(1, closebracket - 1); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 202 | } else { |
| 203 | return false; |
| 204 | } |
| 205 | } else { |
| 206 | std::string::size_type colonpos = in_str.find(':'); |
| 207 | if (std::string::npos != colonpos) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 208 | if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 209 | return false; |
| 210 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 211 | *host = in_str.substr(0, colonpos); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 212 | } else { |
| 213 | *host = in_str; |
| 214 | } |
| 215 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 216 | return !host->empty(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 217 | } |
| 218 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 219 | // Adds a StunConfiguration or TurnConfiguration to the appropriate list, |
| 220 | // by parsing |url| and using the username/password in |server|. |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 221 | bool ParseIceServerUrl(const PeerConnectionInterface::IceServer& server, |
| 222 | const std::string& url, |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 223 | StunConfigurations* stun_config, |
| 224 | TurnConfigurations* turn_config) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 225 | // draft-nandakumar-rtcweb-stun-uri-01 |
| 226 | // stunURI = scheme ":" stun-host [ ":" stun-port ] |
| 227 | // scheme = "stun" / "stuns" |
| 228 | // stun-host = IP-literal / IPv4address / reg-name |
| 229 | // stun-port = *DIGIT |
| 230 | |
| 231 | // draft-petithuguenin-behave-turn-uris-01 |
| 232 | // turnURI = scheme ":" turn-host [ ":" turn-port ] |
| 233 | // [ "?transport=" transport ] |
| 234 | // scheme = "turn" / "turns" |
| 235 | // transport = "udp" / "tcp" / transport-ext |
| 236 | // transport-ext = 1*unreserved |
| 237 | // turn-host = IP-literal / IPv4address / reg-name |
| 238 | // turn-port = *DIGIT |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 239 | RTC_DCHECK(stun_config != nullptr); |
| 240 | RTC_DCHECK(turn_config != nullptr); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 241 | std::vector<std::string> tokens; |
| 242 | std::string turn_transport_type = kUdpTransportType; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 243 | RTC_DCHECK(!url.empty()); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 244 | rtc::tokenize(url, '?', &tokens); |
| 245 | std::string uri_without_transport = tokens[0]; |
| 246 | // Let's look into transport= param, if it exists. |
| 247 | if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present. |
| 248 | std::string uri_transport_param = tokens[1]; |
| 249 | rtc::tokenize(uri_transport_param, '=', &tokens); |
| 250 | if (tokens[0] == kTransport) { |
| 251 | // As per above grammar transport param will be consist of lower case |
| 252 | // letters. |
| 253 | if (tokens[1] != kUdpTransportType && tokens[1] != kTcpTransportType) { |
| 254 | LOG(LS_WARNING) << "Transport param should always be udp or tcp."; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 255 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 256 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 257 | turn_transport_type = tokens[1]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 258 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 259 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 261 | std::string hoststring; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 262 | ServiceType service_type; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 263 | if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, |
| 264 | &service_type, |
| 265 | &hoststring)) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 266 | LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url; |
| 267 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 268 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 269 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 270 | // GetServiceTypeAndHostnameFromUri should never give an empty hoststring |
| 271 | RTC_DCHECK(!hoststring.empty()); |
Tommi | 77d444a | 2015-04-24 15:38:38 +0200 | [diff] [blame] | 272 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 273 | // Let's break hostname. |
| 274 | tokens.clear(); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 275 | rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens); |
| 276 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 277 | std::string username(server.username); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 278 | if (tokens.size() > kTurnHostTokensNum) { |
| 279 | LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; |
| 280 | return false; |
| 281 | } |
| 282 | if (tokens.size() == kTurnHostTokensNum) { |
| 283 | if (tokens[0].empty() || tokens[1].empty()) { |
| 284 | LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; |
| 285 | return false; |
| 286 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 287 | username.assign(rtc::s_url_decode(tokens[0])); |
| 288 | hoststring = tokens[1]; |
| 289 | } else { |
| 290 | hoststring = tokens[0]; |
| 291 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 292 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 293 | int port = kDefaultStunPort; |
| 294 | if (service_type == TURNS) { |
| 295 | port = kDefaultStunTlsPort; |
| 296 | turn_transport_type = kTcpTransportType; |
| 297 | } |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 298 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 299 | std::string address; |
| 300 | if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 301 | LOG(WARNING) << "Invalid hostname format: " << uri_without_transport; |
| 302 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 303 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 304 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 305 | if (port <= 0 || port > 0xffff) { |
| 306 | LOG(WARNING) << "Invalid port: " << port; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 307 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 308 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 309 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 310 | switch (service_type) { |
| 311 | case STUN: |
| 312 | case STUNS: |
| 313 | stun_config->push_back(StunConfiguration(address, port)); |
| 314 | break; |
| 315 | case TURN: |
| 316 | case TURNS: { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 317 | bool secure = (service_type == TURNS); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 318 | turn_config->push_back(TurnConfiguration(address, port, |
| 319 | username, |
| 320 | server.password, |
| 321 | turn_transport_type, |
| 322 | secure)); |
| 323 | break; |
| 324 | } |
| 325 | case INVALID: |
| 326 | default: |
| 327 | LOG(WARNING) << "Configuration not supported: " << url; |
| 328 | return false; |
| 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 333 | // Check if we can send |new_stream| on a PeerConnection. |
| 334 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 335 | webrtc::MediaStreamInterface* new_stream) { |
| 336 | if (!new_stream || !current_streams) { |
| 337 | return false; |
| 338 | } |
| 339 | if (current_streams->find(new_stream->label()) != nullptr) { |
| 340 | LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 341 | << " is already added."; |
| 342 | return false; |
| 343 | } |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | bool MediaContentDirectionHasSend(cricket::MediaContentDirection dir) { |
| 348 | return dir == cricket::MD_SENDONLY || dir == cricket::MD_SENDRECV; |
| 349 | } |
| 350 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 351 | // If the direction is "recvonly" or "inactive", treat the description |
| 352 | // as containing no streams. |
| 353 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 354 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 355 | const cricket::MediaContentDescription* desc) { |
| 356 | return MediaContentDirectionHasSend(desc->direction()) |
| 357 | ? desc->streams() |
| 358 | : std::vector<cricket::StreamParams>(); |
| 359 | } |
| 360 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 361 | bool IsValidOfferToReceiveMedia(int value) { |
| 362 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 363 | return (value >= Options::kUndefined) && |
| 364 | (value <= Options::kMaxOfferToReceiveMedia); |
| 365 | } |
| 366 | |
| 367 | // Add the stream and RTP data channel info to |session_options|. |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 368 | void SetStreams(cricket::MediaSessionOptions* session_options, |
| 369 | rtc::scoped_refptr<StreamCollection> streams, |
| 370 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 371 | rtp_data_channels) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 372 | session_options->streams.clear(); |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 373 | if (streams != nullptr) { |
| 374 | for (size_t i = 0; i < streams->count(); ++i) { |
| 375 | MediaStreamInterface* stream = streams->at(i); |
| 376 | // For each audio track in the stream, add it to the MediaSessionOptions. |
| 377 | for (const auto& track : stream->GetAudioTracks()) { |
| 378 | session_options->AddSendStream(cricket::MEDIA_TYPE_AUDIO, track->id(), |
| 379 | stream->label()); |
| 380 | } |
| 381 | // For each video track in the stream, add it to the MediaSessionOptions. |
| 382 | for (const auto& track : stream->GetVideoTracks()) { |
| 383 | session_options->AddSendStream(cricket::MEDIA_TYPE_VIDEO, track->id(), |
| 384 | stream->label()); |
| 385 | } |
| 386 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // Check for data channels. |
| 390 | for (const auto& kv : rtp_data_channels) { |
| 391 | const DataChannel* channel = kv.second; |
| 392 | if (channel->state() == DataChannel::kConnecting || |
| 393 | channel->state() == DataChannel::kOpen) { |
| 394 | // |streamid| and |sync_label| are both set to the DataChannel label |
| 395 | // here so they can be signaled the same way as MediaStreams and Tracks. |
| 396 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 397 | // track label is the same as |streamid|. |
| 398 | const std::string& streamid = channel->label(); |
| 399 | const std::string& sync_label = channel->label(); |
| 400 | session_options->AddSendStream(cricket::MEDIA_TYPE_DATA, streamid, |
| 401 | sync_label); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 406 | } // namespace |
| 407 | |
| 408 | namespace webrtc { |
| 409 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 410 | // Factory class for creating remote MediaStreams and MediaStreamTracks. |
| 411 | class RemoteMediaStreamFactory { |
| 412 | public: |
| 413 | explicit RemoteMediaStreamFactory(rtc::Thread* signaling_thread, |
| 414 | cricket::ChannelManager* channel_manager) |
| 415 | : signaling_thread_(signaling_thread), |
| 416 | channel_manager_(channel_manager) {} |
| 417 | |
| 418 | rtc::scoped_refptr<MediaStreamInterface> CreateMediaStream( |
| 419 | const std::string& stream_label) { |
| 420 | return MediaStreamProxy::Create(signaling_thread_, |
| 421 | MediaStream::Create(stream_label)); |
| 422 | } |
| 423 | |
| 424 | AudioTrackInterface* AddAudioTrack(webrtc::MediaStreamInterface* stream, |
| 425 | const std::string& track_id) { |
| 426 | return AddTrack<AudioTrackInterface, AudioTrack, AudioTrackProxy>( |
| 427 | stream, track_id, RemoteAudioSource::Create().get()); |
| 428 | } |
| 429 | |
| 430 | VideoTrackInterface* AddVideoTrack(webrtc::MediaStreamInterface* stream, |
| 431 | const std::string& track_id) { |
| 432 | return AddTrack<VideoTrackInterface, VideoTrack, VideoTrackProxy>( |
| 433 | stream, track_id, |
| 434 | VideoSource::Create(channel_manager_, new RemoteVideoCapturer(), |
| 435 | nullptr) |
| 436 | .get()); |
| 437 | } |
| 438 | |
| 439 | private: |
| 440 | template <typename TI, typename T, typename TP, typename S> |
| 441 | TI* AddTrack(MediaStreamInterface* stream, |
| 442 | const std::string& track_id, |
| 443 | S* source) { |
| 444 | rtc::scoped_refptr<TI> track( |
| 445 | TP::Create(signaling_thread_, T::Create(track_id, source))); |
| 446 | track->set_state(webrtc::MediaStreamTrackInterface::kLive); |
| 447 | if (stream->AddTrack(track)) { |
| 448 | return track; |
| 449 | } |
| 450 | return nullptr; |
| 451 | } |
| 452 | |
| 453 | rtc::Thread* signaling_thread_; |
| 454 | cricket::ChannelManager* channel_manager_; |
| 455 | }; |
| 456 | |
| 457 | bool ConvertRtcOptionsForOffer( |
| 458 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 459 | cricket::MediaSessionOptions* session_options) { |
| 460 | typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions; |
| 461 | if (!IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) || |
| 462 | !IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video)) { |
| 463 | return false; |
| 464 | } |
| 465 | |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 466 | if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 467 | session_options->recv_audio = (rtc_options.offer_to_receive_audio > 0); |
| 468 | } |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 469 | if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 470 | session_options->recv_video = (rtc_options.offer_to_receive_video > 0); |
| 471 | } |
| 472 | |
| 473 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 474 | session_options->transport_options.ice_restart = rtc_options.ice_restart; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 475 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 476 | |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints, |
| 481 | cricket::MediaSessionOptions* session_options) { |
| 482 | bool value = false; |
| 483 | size_t mandatory_constraints_satisfied = 0; |
| 484 | |
| 485 | // kOfferToReceiveAudio defaults to true according to spec. |
| 486 | if (!FindConstraint(constraints, |
| 487 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 488 | &mandatory_constraints_satisfied) || |
| 489 | value) { |
| 490 | session_options->recv_audio = true; |
| 491 | } |
| 492 | |
| 493 | // kOfferToReceiveVideo defaults to false according to spec. But |
| 494 | // if it is an answer and video is offered, we should still accept video |
| 495 | // per default. |
| 496 | value = false; |
| 497 | if (!FindConstraint(constraints, |
| 498 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 499 | &mandatory_constraints_satisfied) || |
| 500 | value) { |
| 501 | session_options->recv_video = true; |
| 502 | } |
| 503 | |
| 504 | if (FindConstraint(constraints, |
| 505 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 506 | &mandatory_constraints_satisfied)) { |
| 507 | session_options->vad_enabled = value; |
| 508 | } |
| 509 | |
| 510 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 511 | &mandatory_constraints_satisfied)) { |
| 512 | session_options->bundle_enabled = value; |
| 513 | } else { |
| 514 | // kUseRtpMux defaults to true according to spec. |
| 515 | session_options->bundle_enabled = true; |
| 516 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 517 | |
| 518 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 519 | &value, &mandatory_constraints_satisfied)) { |
| 520 | session_options->transport_options.ice_restart = value; |
| 521 | } else { |
| 522 | // kIceRestart defaults to false according to spec. |
| 523 | session_options->transport_options.ice_restart = false; |
| 524 | } |
| 525 | |
| 526 | if (!constraints) { |
| 527 | return true; |
| 528 | } |
| 529 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 530 | } |
| 531 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 532 | bool ParseIceServers(const PeerConnectionInterface::IceServers& servers, |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 533 | StunConfigurations* stun_config, |
| 534 | TurnConfigurations* turn_config) { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 535 | for (const webrtc::PeerConnectionInterface::IceServer& server : servers) { |
| 536 | if (!server.urls.empty()) { |
| 537 | for (const std::string& url : server.urls) { |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 538 | if (url.empty()) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 539 | LOG(LS_ERROR) << "Empty uri."; |
| 540 | return false; |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 541 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 542 | if (!ParseIceServerUrl(server, url, stun_config, turn_config)) { |
| 543 | return false; |
| 544 | } |
| 545 | } |
| 546 | } else if (!server.uri.empty()) { |
| 547 | // Fallback to old .uri if new .urls isn't present. |
| 548 | if (!ParseIceServerUrl(server, server.uri, stun_config, turn_config)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 549 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 550 | } |
| 551 | } else { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 552 | LOG(LS_ERROR) << "Empty uri."; |
| 553 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | return true; |
| 557 | } |
| 558 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 559 | PeerConnection::PeerConnection(PeerConnectionFactory* factory) |
| 560 | : factory_(factory), |
| 561 | observer_(NULL), |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 562 | uma_observer_(NULL), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 563 | signaling_state_(kStable), |
| 564 | ice_state_(kIceNew), |
| 565 | ice_connection_state_(kIceConnectionNew), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 566 | ice_gathering_state_(kIceGatheringNew), |
| 567 | local_streams_(StreamCollection::Create()), |
| 568 | remote_streams_(StreamCollection::Create()) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 569 | |
| 570 | PeerConnection::~PeerConnection() { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 571 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 572 | // Need to detach RTP senders/receivers from WebRtcSession, |
| 573 | // since it's about to be destroyed. |
| 574 | for (const auto& sender : senders_) { |
| 575 | sender->Stop(); |
| 576 | } |
| 577 | for (const auto& receiver : receivers_) { |
| 578 | receiver->Stop(); |
| 579 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 583 | const PeerConnectionInterface::RTCConfiguration& configuration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 584 | const MediaConstraintsInterface* constraints, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 585 | PortAllocatorFactoryInterface* allocator_factory, |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 586 | rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 587 | PeerConnectionObserver* observer) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 588 | RTC_DCHECK(observer != nullptr); |
| 589 | if (!observer) { |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 590 | return false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 591 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 592 | observer_ = observer; |
| 593 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 594 | std::vector<PortAllocatorFactoryInterface::StunConfiguration> stun_config; |
| 595 | std::vector<PortAllocatorFactoryInterface::TurnConfiguration> turn_config; |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 596 | if (!ParseIceServers(configuration.servers, &stun_config, &turn_config)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 597 | return false; |
| 598 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 599 | port_allocator_.reset( |
| 600 | allocator_factory->CreatePortAllocator(stun_config, turn_config)); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 601 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 602 | // To handle both internal and externally created port allocator, we will |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 603 | // enable BUNDLE here. |
braveyao@webrtc.org | 1732df6 | 2014-10-27 03:01:37 +0000 | [diff] [blame] | 604 | int portallocator_flags = port_allocator_->flags(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 605 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 606 | cricket::PORTALLOCATOR_ENABLE_IPV6; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 607 | bool value; |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 608 | // If IPv6 flag was specified, we'll not override it by experiment. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 609 | if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, |
| 610 | &value, nullptr)) { |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 611 | if (!value) { |
| 612 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 613 | } |
guoweis@webrtc.org | 2c1bcea | 2014-09-23 16:23:02 +0000 | [diff] [blame] | 614 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") == |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 615 | "Disabled") { |
| 616 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 619 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 620 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
| 621 | LOG(LS_INFO) << "TCP candidates are disabled."; |
| 622 | } |
| 623 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 624 | port_allocator_->set_flags(portallocator_flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 625 | // No step delay is used while allocating ports. |
| 626 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 627 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 628 | media_controller_.reset(factory_->CreateMediaController()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 629 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 630 | remote_stream_factory_.reset(new RemoteMediaStreamFactory( |
| 631 | factory_->signaling_thread(), media_controller_->channel_manager())); |
| 632 | |
| 633 | session_.reset( |
| 634 | new WebRtcSession(media_controller_.get(), factory_->signaling_thread(), |
| 635 | factory_->worker_thread(), port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 636 | stats_.reset(new StatsCollector(this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 637 | |
| 638 | // Initialize the WebRtcSession. It creates transport channels etc. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 639 | if (!session_->Initialize(factory_->options(), constraints, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 640 | dtls_identity_store.Pass(), configuration)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 641 | return false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 642 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 643 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 644 | // Register PeerConnection as receiver of local ice candidates. |
| 645 | // All the callbacks will be posted to the application from PeerConnection. |
| 646 | session_->RegisterIceObserver(this); |
| 647 | session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 648 | session_->SignalVoiceChannelDestroyed.connect( |
| 649 | this, &PeerConnection::OnVoiceChannelDestroyed); |
| 650 | session_->SignalVideoChannelDestroyed.connect( |
| 651 | this, &PeerConnection::OnVideoChannelDestroyed); |
| 652 | session_->SignalDataChannelCreated.connect( |
| 653 | this, &PeerConnection::OnDataChannelCreated); |
| 654 | session_->SignalDataChannelDestroyed.connect( |
| 655 | this, &PeerConnection::OnDataChannelDestroyed); |
| 656 | session_->SignalDataChannelOpenMessage.connect( |
| 657 | this, &PeerConnection::OnDataChannelOpenMessage); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 658 | return true; |
| 659 | } |
| 660 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 661 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 662 | PeerConnection::local_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 663 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 664 | } |
| 665 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 666 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 667 | PeerConnection::remote_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 668 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 669 | } |
| 670 | |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 671 | // TODO(deadbeef): Create RtpSenders immediately here, even if local |
| 672 | // description hasn't yet been set. |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 673 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 674 | if (IsClosed()) { |
| 675 | return false; |
| 676 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 677 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 680 | |
| 681 | local_streams_->AddStream(local_stream); |
| 682 | |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 683 | // Find tracks that have already been configured in SDP. This can occur if a |
| 684 | // local session description that contains the MSID of these tracks is set |
| 685 | // before AddLocalStream is called. It can also occur if the local session |
| 686 | // description is not changed and RemoveLocalStream is called and later |
| 687 | // AddLocalStream is called again with the same stream. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 688 | for (const auto& track : local_stream->GetAudioTracks()) { |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 689 | const TrackInfo* track_info = |
| 690 | FindTrackInfo(local_audio_tracks_, local_stream->label(), track->id()); |
| 691 | if (track_info) { |
| 692 | CreateAudioSender(local_stream, track.get(), track_info->ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | for (const auto& track : local_stream->GetVideoTracks()) { |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 696 | const TrackInfo* track_info = |
| 697 | FindTrackInfo(local_video_tracks_, local_stream->label(), track->id()); |
| 698 | if (track_info) { |
| 699 | CreateVideoSender(local_stream, track.get(), track_info->ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 703 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 704 | observer_->OnRenegotiationNeeded(); |
| 705 | return true; |
| 706 | } |
| 707 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 708 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 709 | // indefinitely. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 710 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 711 | for (const auto& track : local_stream->GetAudioTracks()) { |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 712 | const TrackInfo* track_info = |
| 713 | FindTrackInfo(local_audio_tracks_, local_stream->label(), track->id()); |
| 714 | if (track_info) { |
| 715 | DestroyAudioSender(local_stream, track.get(), track_info->ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | for (const auto& track : local_stream->GetVideoTracks()) { |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 719 | const TrackInfo* track_info = |
| 720 | FindTrackInfo(local_video_tracks_, local_stream->label(), track->id()); |
| 721 | if (track_info) { |
| 722 | DestroyVideoSender(local_stream, track.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | local_streams_->RemoveStream(local_stream); |
| 727 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 728 | if (IsClosed()) { |
| 729 | return; |
| 730 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 731 | observer_->OnRenegotiationNeeded(); |
| 732 | } |
| 733 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 734 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 735 | AudioTrackInterface* track) { |
| 736 | if (!track) { |
| 737 | LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
| 738 | return NULL; |
| 739 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 740 | if (!local_streams_->FindAudioTrack(track->id())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 741 | LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track."; |
| 742 | return NULL; |
| 743 | } |
| 744 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 745 | rtc::scoped_refptr<DtmfSenderInterface> sender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 746 | DtmfSender::Create(track, signaling_thread(), session_.get())); |
| 747 | if (!sender.get()) { |
| 748 | LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create."; |
| 749 | return NULL; |
| 750 | } |
| 751 | return DtmfSenderProxy::Create(signaling_thread(), sender.get()); |
| 752 | } |
| 753 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 754 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 755 | const { |
| 756 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders; |
| 757 | for (const auto& sender : senders_) { |
| 758 | senders.push_back(RtpSenderProxy::Create(signaling_thread(), sender.get())); |
| 759 | } |
| 760 | return senders; |
| 761 | } |
| 762 | |
| 763 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 764 | PeerConnection::GetReceivers() const { |
| 765 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers; |
| 766 | for (const auto& receiver : receivers_) { |
| 767 | receivers.push_back( |
| 768 | RtpReceiverProxy::Create(signaling_thread(), receiver.get())); |
| 769 | } |
| 770 | return receivers; |
| 771 | } |
| 772 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 773 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 774 | MediaStreamTrackInterface* track, |
| 775 | StatsOutputLevel level) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 776 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 777 | if (!VERIFY(observer != NULL)) { |
| 778 | LOG(LS_ERROR) << "GetStats - observer is NULL."; |
| 779 | return false; |
| 780 | } |
| 781 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 782 | stats_->UpdateStats(level); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 783 | signaling_thread()->Post(this, MSG_GETSTATS, |
| 784 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 785 | return true; |
| 786 | } |
| 787 | |
| 788 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 789 | return signaling_state_; |
| 790 | } |
| 791 | |
| 792 | PeerConnectionInterface::IceState PeerConnection::ice_state() { |
| 793 | return ice_state_; |
| 794 | } |
| 795 | |
| 796 | PeerConnectionInterface::IceConnectionState |
| 797 | PeerConnection::ice_connection_state() { |
| 798 | return ice_connection_state_; |
| 799 | } |
| 800 | |
| 801 | PeerConnectionInterface::IceGatheringState |
| 802 | PeerConnection::ice_gathering_state() { |
| 803 | return ice_gathering_state_; |
| 804 | } |
| 805 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 806 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 807 | PeerConnection::CreateDataChannel( |
| 808 | const std::string& label, |
| 809 | const DataChannelInit* config) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 810 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 811 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 812 | rtc::scoped_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 813 | if (config) { |
| 814 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 815 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 816 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 817 | InternalCreateDataChannel(label, internal_config.get())); |
| 818 | if (!channel.get()) { |
| 819 | return nullptr; |
| 820 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 822 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 823 | // the first SCTP DataChannel. |
| 824 | if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
| 825 | observer_->OnRenegotiationNeeded(); |
| 826 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 827 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 828 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 829 | } |
| 830 | |
| 831 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 832 | const MediaConstraintsInterface* constraints) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 833 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 834 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 835 | return; |
| 836 | } |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 837 | RTCOfferAnswerOptions options; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 838 | |
| 839 | bool value; |
| 840 | size_t mandatory_constraints = 0; |
| 841 | |
| 842 | if (FindConstraint(constraints, |
| 843 | MediaConstraintsInterface::kOfferToReceiveAudio, |
| 844 | &value, |
| 845 | &mandatory_constraints)) { |
| 846 | options.offer_to_receive_audio = |
| 847 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 848 | } |
| 849 | |
| 850 | if (FindConstraint(constraints, |
| 851 | MediaConstraintsInterface::kOfferToReceiveVideo, |
| 852 | &value, |
| 853 | &mandatory_constraints)) { |
| 854 | options.offer_to_receive_video = |
| 855 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 856 | } |
| 857 | |
| 858 | if (FindConstraint(constraints, |
| 859 | MediaConstraintsInterface::kVoiceActivityDetection, |
| 860 | &value, |
| 861 | &mandatory_constraints)) { |
| 862 | options.voice_activity_detection = value; |
| 863 | } |
| 864 | |
| 865 | if (FindConstraint(constraints, |
| 866 | MediaConstraintsInterface::kIceRestart, |
| 867 | &value, |
| 868 | &mandatory_constraints)) { |
| 869 | options.ice_restart = value; |
| 870 | } |
| 871 | |
| 872 | if (FindConstraint(constraints, |
| 873 | MediaConstraintsInterface::kUseRtpMux, |
| 874 | &value, |
| 875 | &mandatory_constraints)) { |
| 876 | options.use_rtp_mux = value; |
| 877 | } |
| 878 | |
| 879 | CreateOffer(observer, options); |
| 880 | } |
| 881 | |
| 882 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 883 | const RTCOfferAnswerOptions& options) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 884 | if (!VERIFY(observer != nullptr)) { |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 885 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 886 | return; |
| 887 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 888 | |
| 889 | cricket::MediaSessionOptions session_options; |
| 890 | if (!GetOptionsForOffer(options, &session_options)) { |
| 891 | std::string error = "CreateOffer called with invalid options."; |
| 892 | LOG(LS_ERROR) << error; |
| 893 | PostCreateSessionDescriptionFailure(observer, error); |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | session_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void PeerConnection::CreateAnswer( |
| 901 | CreateSessionDescriptionObserver* observer, |
| 902 | const MediaConstraintsInterface* constraints) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 903 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 904 | LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 905 | return; |
| 906 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 907 | |
| 908 | cricket::MediaSessionOptions session_options; |
| 909 | if (!GetOptionsForAnswer(constraints, &session_options)) { |
| 910 | std::string error = "CreateAnswer called with invalid constraints."; |
| 911 | LOG(LS_ERROR) << error; |
| 912 | PostCreateSessionDescriptionFailure(observer, error); |
| 913 | return; |
| 914 | } |
| 915 | |
| 916 | session_->CreateAnswer(observer, constraints, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void PeerConnection::SetLocalDescription( |
| 920 | SetSessionDescriptionObserver* observer, |
| 921 | SessionDescriptionInterface* desc) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 922 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 923 | LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
| 924 | return; |
| 925 | } |
| 926 | if (!desc) { |
| 927 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 928 | return; |
| 929 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 930 | // Update stats here so that we have the most recent stats for tracks and |
| 931 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 932 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 933 | std::string error; |
| 934 | if (!session_->SetLocalDescription(desc, &error)) { |
| 935 | PostSetSessionDescriptionFailure(observer, error); |
| 936 | return; |
| 937 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 938 | |
| 939 | // If setting the description decided our SSL role, allocate any necessary |
| 940 | // SCTP sids. |
| 941 | rtc::SSLRole role; |
| 942 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
| 943 | session_->GetSslRole(&role)) { |
| 944 | AllocateSctpSids(role); |
| 945 | } |
| 946 | |
| 947 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 948 | // local session description. |
| 949 | const cricket::ContentInfo* audio_content = |
| 950 | GetFirstAudioContent(desc->description()); |
| 951 | if (audio_content) { |
| 952 | const cricket::AudioContentDescription* audio_desc = |
| 953 | static_cast<const cricket::AudioContentDescription*>( |
| 954 | audio_content->description); |
| 955 | UpdateLocalTracks(audio_desc->streams(), audio_desc->type()); |
| 956 | } |
| 957 | |
| 958 | const cricket::ContentInfo* video_content = |
| 959 | GetFirstVideoContent(desc->description()); |
| 960 | if (video_content) { |
| 961 | const cricket::VideoContentDescription* video_desc = |
| 962 | static_cast<const cricket::VideoContentDescription*>( |
| 963 | video_content->description); |
| 964 | UpdateLocalTracks(video_desc->streams(), video_desc->type()); |
| 965 | } |
| 966 | |
| 967 | const cricket::ContentInfo* data_content = |
| 968 | GetFirstDataContent(desc->description()); |
| 969 | if (data_content) { |
| 970 | const cricket::DataContentDescription* data_desc = |
| 971 | static_cast<const cricket::DataContentDescription*>( |
| 972 | data_content->description); |
| 973 | if (rtc::starts_with(data_desc->protocol().data(), |
| 974 | cricket::kMediaProtocolRtpPrefix)) { |
| 975 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 980 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 981 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 982 | // MaybeStartGathering needs to be called after posting |
| 983 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 984 | // before signaling that SetLocalDescription completed. |
| 985 | session_->MaybeStartGathering(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | void PeerConnection::SetRemoteDescription( |
| 989 | SetSessionDescriptionObserver* observer, |
| 990 | SessionDescriptionInterface* desc) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 991 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 992 | LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
| 993 | return; |
| 994 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 995 | if (!desc) { |
| 996 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 997 | return; |
| 998 | } |
| 999 | // Update stats here so that we have the most recent stats for tracks and |
| 1000 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1001 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1002 | std::string error; |
| 1003 | if (!session_->SetRemoteDescription(desc, &error)) { |
| 1004 | PostSetSessionDescriptionFailure(observer, error); |
| 1005 | return; |
| 1006 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1007 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1008 | // If setting the description decided our SSL role, allocate any necessary |
| 1009 | // SCTP sids. |
| 1010 | rtc::SSLRole role; |
| 1011 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
| 1012 | session_->GetSslRole(&role)) { |
| 1013 | AllocateSctpSids(role); |
| 1014 | } |
| 1015 | |
| 1016 | const cricket::SessionDescription* remote_desc = desc->description(); |
| 1017 | |
| 1018 | // We wait to signal new streams until we finish processing the description, |
| 1019 | // since only at that point will new streams have all their tracks. |
| 1020 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 1021 | |
| 1022 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 1023 | // and MediaStreams. |
| 1024 | const cricket::ContentInfo* audio_content = GetFirstAudioContent(remote_desc); |
| 1025 | if (audio_content) { |
| 1026 | const cricket::AudioContentDescription* desc = |
| 1027 | static_cast<const cricket::AudioContentDescription*>( |
| 1028 | audio_content->description); |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 1029 | UpdateRemoteStreamsList(GetActiveStreams(desc), desc->type(), new_streams); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1030 | remote_info_.default_audio_track_needed = |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1031 | !remote_desc->msid_supported() && desc->streams().empty() && |
| 1032 | MediaContentDirectionHasSend(desc->direction()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 1036 | // and MediaStreams. |
| 1037 | const cricket::ContentInfo* video_content = GetFirstVideoContent(remote_desc); |
| 1038 | if (video_content) { |
| 1039 | const cricket::VideoContentDescription* desc = |
| 1040 | static_cast<const cricket::VideoContentDescription*>( |
| 1041 | video_content->description); |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 1042 | UpdateRemoteStreamsList(GetActiveStreams(desc), desc->type(), new_streams); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1043 | remote_info_.default_video_track_needed = |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1044 | !remote_desc->msid_supported() && desc->streams().empty() && |
| 1045 | MediaContentDirectionHasSend(desc->direction()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // Update the DataChannels with the information from the remote peer. |
| 1049 | const cricket::ContentInfo* data_content = GetFirstDataContent(remote_desc); |
| 1050 | if (data_content) { |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 1051 | const cricket::DataContentDescription* desc = |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1052 | static_cast<const cricket::DataContentDescription*>( |
| 1053 | data_content->description); |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 1054 | if (rtc::starts_with(desc->protocol().data(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1055 | cricket::kMediaProtocolRtpPrefix)) { |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 1056 | UpdateRemoteRtpDataChannels(GetActiveStreams(desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 1061 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 1062 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 1063 | stats_->AddStream(new_stream); |
| 1064 | observer_->OnAddStream(new_stream); |
| 1065 | } |
| 1066 | |
| 1067 | // Find removed MediaStreams. |
| 1068 | if (remote_info_.IsDefaultMediaStreamNeeded() && |
| 1069 | remote_streams_->find(kDefaultStreamLabel) != nullptr) { |
| 1070 | // The default media stream already exists. No need to do anything. |
| 1071 | } else { |
| 1072 | UpdateEndedRemoteMediaStreams(); |
| 1073 | remote_info_.msid_supported |= remote_streams_->count() > 0; |
| 1074 | } |
| 1075 | MaybeCreateDefaultStream(); |
| 1076 | |
| 1077 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 1078 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame] | 1081 | bool PeerConnection::SetConfiguration(const RTCConfiguration& config) { |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1082 | if (port_allocator_) { |
| 1083 | std::vector<PortAllocatorFactoryInterface::StunConfiguration> stuns; |
| 1084 | std::vector<PortAllocatorFactoryInterface::TurnConfiguration> turns; |
| 1085 | if (!ParseIceServers(config.servers, &stuns, &turns)) { |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1089 | std::vector<rtc::SocketAddress> stun_hosts; |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1090 | typedef std::vector<StunConfiguration>::const_iterator StunIt; |
| 1091 | for (StunIt stun_it = stuns.begin(); stun_it != stuns.end(); ++stun_it) { |
| 1092 | stun_hosts.push_back(stun_it->server); |
| 1093 | } |
| 1094 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1095 | rtc::SocketAddress stun_addr; |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1096 | if (!stun_hosts.empty()) { |
| 1097 | stun_addr = stun_hosts.front(); |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame] | 1098 | LOG(LS_INFO) << "SetConfiguration: StunServer Address: " |
| 1099 | << stun_addr.ToString(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | for (size_t i = 0; i < turns.size(); ++i) { |
| 1103 | cricket::RelayCredentials credentials(turns[i].username, |
| 1104 | turns[i].password); |
| 1105 | cricket::RelayServerConfig relay_server(cricket::RELAY_TURN); |
| 1106 | cricket::ProtocolType protocol; |
| 1107 | if (cricket::StringToProto(turns[i].transport_type.c_str(), &protocol)) { |
| 1108 | relay_server.ports.push_back(cricket::ProtocolAddress( |
| 1109 | turns[i].server, protocol, turns[i].secure)); |
| 1110 | relay_server.credentials = credentials; |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame] | 1111 | LOG(LS_INFO) << "SetConfiguration: TurnServer Address: " |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1112 | << turns[i].server.ToString(); |
| 1113 | } else { |
| 1114 | LOG(LS_WARNING) << "Ignoring TURN server " << turns[i].server << ". " |
| 1115 | << "Reason= Incorrect " << turns[i].transport_type |
| 1116 | << " transport parameter."; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 1120 | session_->SetIceConfig(session_->ParseIceConfig(config)); |
mallinath@webrtc.org | 3d81b1b | 2014-09-09 14:38:10 +0000 | [diff] [blame] | 1121 | return session_->SetIceTransports(config.type); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1124 | bool PeerConnection::AddIceCandidate( |
| 1125 | const IceCandidateInterface* ice_candidate) { |
| 1126 | return session_->ProcessIceMessage(ice_candidate); |
| 1127 | } |
| 1128 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1129 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
| 1130 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1131 | |
| 1132 | if (session_) { |
| 1133 | session_->set_metrics_observer(uma_observer_); |
| 1134 | } |
| 1135 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1136 | // Send information about IPv4/IPv6 status. |
| 1137 | if (uma_observer_ && port_allocator_) { |
| 1138 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1139 | uma_observer_->IncrementEnumCounter( |
| 1140 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 1141 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 1142 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1143 | uma_observer_->IncrementEnumCounter( |
| 1144 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 1145 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1150 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
| 1151 | return session_->local_description(); |
| 1152 | } |
| 1153 | |
| 1154 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
| 1155 | return session_->remote_description(); |
| 1156 | } |
| 1157 | |
| 1158 | void PeerConnection::Close() { |
| 1159 | // Update stats here so that we have the most recent stats for tracks and |
| 1160 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1161 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1162 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1163 | session_->Close(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1166 | void PeerConnection::OnSessionStateChange(WebRtcSession* /*session*/, |
| 1167 | WebRtcSession::State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1168 | switch (state) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1169 | case WebRtcSession::STATE_INIT: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1170 | ChangeSignalingState(PeerConnectionInterface::kStable); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1171 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1172 | case WebRtcSession::STATE_SENTOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1173 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer); |
| 1174 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1175 | case WebRtcSession::STATE_SENTPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1176 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer); |
| 1177 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1178 | case WebRtcSession::STATE_RECEIVEDOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1179 | ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer); |
| 1180 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1181 | case WebRtcSession::STATE_RECEIVEDPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1182 | ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer); |
| 1183 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1184 | case WebRtcSession::STATE_INPROGRESS: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1185 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 1186 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1187 | case WebRtcSession::STATE_CLOSED: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1188 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 1189 | break; |
| 1190 | default: |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1195 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1196 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1197 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 1198 | SetSessionDescriptionMsg* param = |
| 1199 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1200 | param->observer->OnSuccess(); |
| 1201 | delete param; |
| 1202 | break; |
| 1203 | } |
| 1204 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 1205 | SetSessionDescriptionMsg* param = |
| 1206 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1207 | param->observer->OnFailure(param->error); |
| 1208 | delete param; |
| 1209 | break; |
| 1210 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1211 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 1212 | CreateSessionDescriptionMsg* param = |
| 1213 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 1214 | param->observer->OnFailure(param->error); |
| 1215 | delete param; |
| 1216 | break; |
| 1217 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1218 | case MSG_GETSTATS: { |
| 1219 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1220 | StatsReports reports; |
| 1221 | stats_->GetStats(param->track, &reports); |
| 1222 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1223 | delete param; |
| 1224 | break; |
| 1225 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1226 | default: |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1227 | RTC_DCHECK(false && "Not implemented"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1228 | break; |
| 1229 | } |
| 1230 | } |
| 1231 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1232 | void PeerConnection::CreateAudioReceiver(MediaStreamInterface* stream, |
| 1233 | AudioTrackInterface* audio_track, |
| 1234 | uint32_t ssrc) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1235 | receivers_.push_back(new AudioRtpReceiver(audio_track, ssrc, session_.get())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1238 | void PeerConnection::CreateVideoReceiver(MediaStreamInterface* stream, |
| 1239 | VideoTrackInterface* video_track, |
| 1240 | uint32_t ssrc) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1241 | receivers_.push_back(new VideoRtpReceiver(video_track, ssrc, session_.get())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1244 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 1245 | // description. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1246 | void PeerConnection::DestroyAudioReceiver(MediaStreamInterface* stream, |
| 1247 | AudioTrackInterface* audio_track) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1248 | auto it = FindReceiverForTrack(audio_track); |
| 1249 | if (it == receivers_.end()) { |
| 1250 | LOG(LS_WARNING) << "RtpReceiver for track with id " << audio_track->id() |
| 1251 | << " doesn't exist."; |
| 1252 | } else { |
| 1253 | (*it)->Stop(); |
| 1254 | receivers_.erase(it); |
| 1255 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1258 | void PeerConnection::DestroyVideoReceiver(MediaStreamInterface* stream, |
| 1259 | VideoTrackInterface* video_track) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1260 | auto it = FindReceiverForTrack(video_track); |
| 1261 | if (it == receivers_.end()) { |
| 1262 | LOG(LS_WARNING) << "RtpReceiver for track with id " << video_track->id() |
| 1263 | << " doesn't exist."; |
| 1264 | } else { |
| 1265 | (*it)->Stop(); |
| 1266 | receivers_.erase(it); |
| 1267 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1268 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1269 | |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1270 | void PeerConnection::CreateAudioSender(MediaStreamInterface* stream, |
| 1271 | AudioTrackInterface* audio_track, |
| 1272 | uint32_t ssrc) { |
| 1273 | senders_.push_back(new AudioRtpSender(audio_track, ssrc, session_.get())); |
| 1274 | stats_->AddLocalAudioTrack(audio_track, ssrc); |
| 1275 | } |
| 1276 | |
| 1277 | void PeerConnection::CreateVideoSender(MediaStreamInterface* stream, |
| 1278 | VideoTrackInterface* video_track, |
| 1279 | uint32_t ssrc) { |
| 1280 | senders_.push_back(new VideoRtpSender(video_track, ssrc, session_.get())); |
| 1281 | } |
| 1282 | |
| 1283 | // TODO(deadbeef): Keep RtpSenders around even if track goes away in local |
| 1284 | // description. |
| 1285 | void PeerConnection::DestroyAudioSender(MediaStreamInterface* stream, |
| 1286 | AudioTrackInterface* audio_track, |
| 1287 | uint32_t ssrc) { |
| 1288 | auto it = FindSenderForTrack(audio_track); |
| 1289 | if (it == senders_.end()) { |
| 1290 | LOG(LS_WARNING) << "RtpSender for track with id " << audio_track->id() |
| 1291 | << " doesn't exist."; |
| 1292 | return; |
| 1293 | } else { |
| 1294 | (*it)->Stop(); |
| 1295 | senders_.erase(it); |
| 1296 | } |
| 1297 | stats_->RemoveLocalAudioTrack(audio_track, ssrc); |
| 1298 | } |
| 1299 | |
| 1300 | void PeerConnection::DestroyVideoSender(MediaStreamInterface* stream, |
| 1301 | VideoTrackInterface* video_track) { |
| 1302 | auto it = FindSenderForTrack(video_track); |
| 1303 | if (it == senders_.end()) { |
| 1304 | LOG(LS_WARNING) << "RtpSender for track with id " << video_track->id() |
| 1305 | << " doesn't exist."; |
| 1306 | return; |
| 1307 | } else { |
| 1308 | (*it)->Stop(); |
| 1309 | senders_.erase(it); |
| 1310 | } |
| 1311 | } |
| 1312 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1313 | void PeerConnection::OnIceConnectionChange( |
| 1314 | PeerConnectionInterface::IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1315 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1316 | // After transitioning to "closed", ignore any additional states from |
| 1317 | // WebRtcSession (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1318 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1319 | return; |
| 1320 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1321 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1322 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | void PeerConnection::OnIceGatheringChange( |
| 1326 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1327 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1328 | if (IsClosed()) { |
| 1329 | return; |
| 1330 | } |
| 1331 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1332 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1336 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1337 | observer_->OnIceCandidate(candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | void PeerConnection::OnIceComplete() { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1341 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1342 | observer_->OnIceComplete(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1345 | void PeerConnection::OnIceConnectionReceivingChange(bool receiving) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1346 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1347 | observer_->OnIceConnectionReceivingChange(receiving); |
| 1348 | } |
| 1349 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1350 | void PeerConnection::ChangeSignalingState( |
| 1351 | PeerConnectionInterface::SignalingState signaling_state) { |
| 1352 | signaling_state_ = signaling_state; |
| 1353 | if (signaling_state == kClosed) { |
| 1354 | ice_connection_state_ = kIceConnectionClosed; |
| 1355 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 1356 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 1357 | ice_gathering_state_ = kIceGatheringComplete; |
| 1358 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 1359 | } |
| 1360 | } |
| 1361 | observer_->OnSignalingChange(signaling_state_); |
| 1362 | observer_->OnStateChange(PeerConnectionObserver::kSignalingState); |
| 1363 | } |
| 1364 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1365 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 1366 | SetSessionDescriptionObserver* observer, |
| 1367 | const std::string& error) { |
| 1368 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 1369 | msg->error = error; |
| 1370 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
| 1371 | } |
| 1372 | |
| 1373 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 1374 | CreateSessionDescriptionObserver* observer, |
| 1375 | const std::string& error) { |
| 1376 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 1377 | msg->error = error; |
| 1378 | signaling_thread()->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
| 1379 | } |
| 1380 | |
| 1381 | bool PeerConnection::GetOptionsForOffer( |
| 1382 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 1383 | cricket::MediaSessionOptions* session_options) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1384 | if (!ConvertRtcOptionsForOffer(rtc_options, session_options)) { |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1388 | SetStreams(session_options, local_streams_, rtp_data_channels_); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1389 | // Offer to receive audio/video if the constraint is not set and there are |
| 1390 | // send streams, or we're currently receiving. |
| 1391 | if (rtc_options.offer_to_receive_audio == RTCOfferAnswerOptions::kUndefined) { |
| 1392 | session_options->recv_audio = |
| 1393 | session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO) || |
| 1394 | !remote_audio_tracks_.empty(); |
| 1395 | } |
| 1396 | if (rtc_options.offer_to_receive_video == RTCOfferAnswerOptions::kUndefined) { |
| 1397 | session_options->recv_video = |
| 1398 | session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO) || |
| 1399 | !remote_video_tracks_.empty(); |
| 1400 | } |
| 1401 | session_options->bundle_enabled = |
| 1402 | session_options->bundle_enabled && |
| 1403 | (session_options->has_audio() || session_options->has_video() || |
| 1404 | session_options->has_data()); |
| 1405 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1406 | if (session_->data_channel_type() == cricket::DCT_SCTP && HasDataChannels()) { |
| 1407 | session_options->data_channel_type = cricket::DCT_SCTP; |
| 1408 | } |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
| 1412 | bool PeerConnection::GetOptionsForAnswer( |
| 1413 | const MediaConstraintsInterface* constraints, |
| 1414 | cricket::MediaSessionOptions* session_options) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1415 | session_options->recv_audio = false; |
| 1416 | session_options->recv_video = false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1417 | if (!ParseConstraintsForAnswer(constraints, session_options)) { |
| 1418 | return false; |
| 1419 | } |
| 1420 | |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1421 | SetStreams(session_options, local_streams_, rtp_data_channels_); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1422 | session_options->bundle_enabled = |
| 1423 | session_options->bundle_enabled && |
| 1424 | (session_options->has_audio() || session_options->has_video() || |
| 1425 | session_options->has_data()); |
| 1426 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1427 | // RTP data channel is handled in MediaSessionOptions::AddStream. SCTP streams |
| 1428 | // are not signaled in the SDP so does not go through that path and must be |
| 1429 | // handled here. |
| 1430 | if (session_->data_channel_type() == cricket::DCT_SCTP) { |
| 1431 | session_options->data_channel_type = cricket::DCT_SCTP; |
| 1432 | } |
| 1433 | return true; |
| 1434 | } |
| 1435 | |
| 1436 | void PeerConnection::UpdateRemoteStreamsList( |
| 1437 | const cricket::StreamParamsVec& streams, |
| 1438 | cricket::MediaType media_type, |
| 1439 | StreamCollection* new_streams) { |
| 1440 | TrackInfos* current_tracks = GetRemoteTracks(media_type); |
| 1441 | |
| 1442 | // Find removed tracks. I.e., tracks where the track id or ssrc don't match |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1443 | // the |
| 1444 | // new StreamParam. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1445 | auto track_it = current_tracks->begin(); |
| 1446 | while (track_it != current_tracks->end()) { |
| 1447 | const TrackInfo& info = *track_it; |
| 1448 | const cricket::StreamParams* params = |
| 1449 | cricket::GetStreamBySsrc(streams, info.ssrc); |
| 1450 | if (!params || params->id != info.track_id) { |
| 1451 | OnRemoteTrackRemoved(info.stream_label, info.track_id, media_type); |
| 1452 | track_it = current_tracks->erase(track_it); |
| 1453 | } else { |
| 1454 | ++track_it; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | // Find new and active tracks. |
| 1459 | for (const cricket::StreamParams& params : streams) { |
| 1460 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 1461 | // track id. |
| 1462 | const std::string& stream_label = params.sync_label; |
| 1463 | const std::string& track_id = params.id; |
| 1464 | uint32_t ssrc = params.first_ssrc(); |
| 1465 | |
| 1466 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 1467 | remote_streams_->find(stream_label); |
| 1468 | if (!stream) { |
| 1469 | // This is a new MediaStream. Create a new remote MediaStream. |
| 1470 | stream = remote_stream_factory_->CreateMediaStream(stream_label); |
| 1471 | remote_streams_->AddStream(stream); |
| 1472 | new_streams->AddStream(stream); |
| 1473 | } |
| 1474 | |
| 1475 | const TrackInfo* track_info = |
| 1476 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 1477 | if (!track_info) { |
| 1478 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 1479 | OnRemoteTrackSeen(stream_label, track_id, ssrc, media_type); |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | void PeerConnection::OnRemoteTrackSeen(const std::string& stream_label, |
| 1485 | const std::string& track_id, |
| 1486 | uint32_t ssrc, |
| 1487 | cricket::MediaType media_type) { |
| 1488 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 1489 | |
| 1490 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1491 | AudioTrackInterface* audio_track = |
| 1492 | remote_stream_factory_->AddAudioTrack(stream, track_id); |
| 1493 | CreateAudioReceiver(stream, audio_track, ssrc); |
| 1494 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1495 | VideoTrackInterface* video_track = |
| 1496 | remote_stream_factory_->AddVideoTrack(stream, track_id); |
| 1497 | CreateVideoReceiver(stream, video_track, ssrc); |
| 1498 | } else { |
| 1499 | RTC_DCHECK(false && "Invalid media type"); |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | void PeerConnection::OnRemoteTrackRemoved(const std::string& stream_label, |
| 1504 | const std::string& track_id, |
| 1505 | cricket::MediaType media_type) { |
| 1506 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 1507 | |
| 1508 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1509 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 1510 | stream->FindAudioTrack(track_id); |
| 1511 | if (audio_track) { |
| 1512 | audio_track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1513 | stream->RemoveTrack(audio_track); |
| 1514 | DestroyAudioReceiver(stream, audio_track); |
| 1515 | } |
| 1516 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1517 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1518 | stream->FindVideoTrack(track_id); |
| 1519 | if (video_track) { |
| 1520 | video_track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1521 | stream->RemoveTrack(video_track); |
| 1522 | DestroyVideoReceiver(stream, video_track); |
| 1523 | } |
| 1524 | } else { |
| 1525 | ASSERT(false && "Invalid media type"); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 1530 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 1531 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 1532 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 1533 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 1534 | streams_to_remove.push_back(stream); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | for (const auto& stream : streams_to_remove) { |
| 1539 | remote_streams_->RemoveStream(stream); |
| 1540 | observer_->OnRemoveStream(stream); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | void PeerConnection::MaybeCreateDefaultStream() { |
| 1545 | if (!remote_info_.IsDefaultMediaStreamNeeded()) { |
| 1546 | return; |
| 1547 | } |
| 1548 | |
| 1549 | bool default_created = false; |
| 1550 | |
| 1551 | rtc::scoped_refptr<MediaStreamInterface> default_remote_stream = |
| 1552 | remote_streams_->find(kDefaultStreamLabel); |
| 1553 | if (default_remote_stream == nullptr) { |
| 1554 | default_created = true; |
| 1555 | default_remote_stream = |
| 1556 | remote_stream_factory_->CreateMediaStream(kDefaultStreamLabel); |
| 1557 | remote_streams_->AddStream(default_remote_stream); |
| 1558 | } |
| 1559 | if (remote_info_.default_audio_track_needed && |
| 1560 | default_remote_stream->GetAudioTracks().size() == 0) { |
| 1561 | remote_audio_tracks_.push_back( |
| 1562 | TrackInfo(kDefaultStreamLabel, kDefaultAudioTrackLabel, 0)); |
| 1563 | OnRemoteTrackSeen(kDefaultStreamLabel, kDefaultAudioTrackLabel, 0, |
| 1564 | cricket::MEDIA_TYPE_AUDIO); |
| 1565 | } |
| 1566 | if (remote_info_.default_video_track_needed && |
| 1567 | default_remote_stream->GetVideoTracks().size() == 0) { |
| 1568 | remote_video_tracks_.push_back( |
| 1569 | TrackInfo(kDefaultStreamLabel, kDefaultVideoTrackLabel, 0)); |
| 1570 | OnRemoteTrackSeen(kDefaultStreamLabel, kDefaultVideoTrackLabel, 0, |
| 1571 | cricket::MEDIA_TYPE_VIDEO); |
| 1572 | } |
| 1573 | if (default_created) { |
| 1574 | stats_->AddStream(default_remote_stream); |
| 1575 | observer_->OnAddStream(default_remote_stream); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | void PeerConnection::EndRemoteTracks(cricket::MediaType media_type) { |
| 1580 | TrackInfos* current_tracks = GetRemoteTracks(media_type); |
| 1581 | for (TrackInfos::iterator track_it = current_tracks->begin(); |
| 1582 | track_it != current_tracks->end(); ++track_it) { |
| 1583 | const TrackInfo& info = *track_it; |
| 1584 | MediaStreamInterface* stream = remote_streams_->find(info.stream_label); |
| 1585 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1586 | AudioTrackInterface* track = stream->FindAudioTrack(info.track_id); |
| 1587 | // There's no guarantee the track is still available, e.g. the track may |
| 1588 | // have been removed from the stream by javascript. |
| 1589 | if (track) { |
| 1590 | track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1591 | } |
| 1592 | } |
| 1593 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1594 | VideoTrackInterface* track = stream->FindVideoTrack(info.track_id); |
| 1595 | // There's no guarantee the track is still available, e.g. the track may |
| 1596 | // have been removed from the stream by javascript. |
| 1597 | if (track) { |
| 1598 | track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | void PeerConnection::UpdateLocalTracks( |
| 1605 | const std::vector<cricket::StreamParams>& streams, |
| 1606 | cricket::MediaType media_type) { |
| 1607 | TrackInfos* current_tracks = GetLocalTracks(media_type); |
| 1608 | |
| 1609 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 1610 | // don't match the new StreamParam. |
| 1611 | TrackInfos::iterator track_it = current_tracks->begin(); |
| 1612 | while (track_it != current_tracks->end()) { |
| 1613 | const TrackInfo& info = *track_it; |
| 1614 | const cricket::StreamParams* params = |
| 1615 | cricket::GetStreamBySsrc(streams, info.ssrc); |
| 1616 | if (!params || params->id != info.track_id || |
| 1617 | params->sync_label != info.stream_label) { |
| 1618 | OnLocalTrackRemoved(info.stream_label, info.track_id, info.ssrc, |
| 1619 | media_type); |
| 1620 | track_it = current_tracks->erase(track_it); |
| 1621 | } else { |
| 1622 | ++track_it; |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | // Find new and active tracks. |
| 1627 | for (const cricket::StreamParams& params : streams) { |
| 1628 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 1629 | // track id. |
| 1630 | const std::string& stream_label = params.sync_label; |
| 1631 | const std::string& track_id = params.id; |
| 1632 | uint32_t ssrc = params.first_ssrc(); |
| 1633 | const TrackInfo* track_info = |
| 1634 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 1635 | if (!track_info) { |
| 1636 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 1637 | OnLocalTrackSeen(stream_label, track_id, params.first_ssrc(), media_type); |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | void PeerConnection::OnLocalTrackSeen(const std::string& stream_label, |
| 1643 | const std::string& track_id, |
| 1644 | uint32_t ssrc, |
| 1645 | cricket::MediaType media_type) { |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1646 | MediaStreamInterface* stream = local_streams_->find(stream_label); |
| 1647 | if (!stream) { |
| 1648 | LOG(LS_WARNING) << "An unknown local MediaStream with label " |
| 1649 | << stream_label << " has been configured."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1650 | return; |
| 1651 | } |
| 1652 | |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1653 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1654 | AudioTrackInterface* audio_track = stream->FindAudioTrack(track_id); |
| 1655 | if (!audio_track) { |
| 1656 | LOG(LS_WARNING) << "An unknown local AudioTrack with id , " << track_id |
| 1657 | << " has been configured."; |
| 1658 | return; |
| 1659 | } |
| 1660 | CreateAudioSender(stream, audio_track, ssrc); |
| 1661 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1662 | VideoTrackInterface* video_track = stream->FindVideoTrack(track_id); |
| 1663 | if (!video_track) { |
| 1664 | LOG(LS_WARNING) << "An unknown local VideoTrack with id , " << track_id |
| 1665 | << " has been configured."; |
| 1666 | return; |
| 1667 | } |
| 1668 | CreateVideoSender(stream, video_track, ssrc); |
| 1669 | } else { |
| 1670 | RTC_DCHECK(false && "Invalid media type"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | void PeerConnection::OnLocalTrackRemoved(const std::string& stream_label, |
| 1675 | const std::string& track_id, |
| 1676 | uint32_t ssrc, |
| 1677 | cricket::MediaType media_type) { |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1678 | MediaStreamInterface* stream = local_streams_->find(stream_label); |
| 1679 | if (!stream) { |
| 1680 | // This is the normal case. I.e., RemoveLocalStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1681 | // SessionDescriptions has been renegotiated. |
| 1682 | return; |
| 1683 | } |
deadbeef | 8f46c63 | 2015-10-26 14:11:17 -0700 | [diff] [blame^] | 1684 | // A track has been removed from the SessionDescription but the MediaStream |
| 1685 | // is still associated with PeerConnection. This only occurs if the SDP |
| 1686 | // doesn't match with the calls to AddLocalStream and RemoveLocalStream. |
| 1687 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1688 | AudioTrackInterface* audio_track = stream->FindAudioTrack(track_id); |
| 1689 | if (!audio_track) { |
| 1690 | return; |
| 1691 | } |
| 1692 | DestroyAudioSender(stream, audio_track, ssrc); |
| 1693 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1694 | VideoTrackInterface* video_track = stream->FindVideoTrack(track_id); |
| 1695 | if (!video_track) { |
| 1696 | return; |
| 1697 | } |
| 1698 | DestroyVideoSender(stream, video_track); |
| 1699 | } else { |
| 1700 | RTC_DCHECK(false && "Invalid media type."); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 1705 | const cricket::StreamParamsVec& streams) { |
| 1706 | std::vector<std::string> existing_channels; |
| 1707 | |
| 1708 | // Find new and active data channels. |
| 1709 | for (const cricket::StreamParams& params : streams) { |
| 1710 | // |it->sync_label| is actually the data channel label. The reason is that |
| 1711 | // we use the same naming of data channels as we do for |
| 1712 | // MediaStreams and Tracks. |
| 1713 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 1714 | // track label is the same as |streamid|. |
| 1715 | const std::string& channel_label = params.sync_label; |
| 1716 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
| 1717 | if (!VERIFY(data_channel_it != rtp_data_channels_.end())) { |
| 1718 | continue; |
| 1719 | } |
| 1720 | // Set the SSRC the data channel should use for sending. |
| 1721 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 1722 | existing_channels.push_back(data_channel_it->first); |
| 1723 | } |
| 1724 | |
| 1725 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 1726 | } |
| 1727 | |
| 1728 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 1729 | const cricket::StreamParamsVec& streams) { |
| 1730 | std::vector<std::string> existing_channels; |
| 1731 | |
| 1732 | // Find new and active data channels. |
| 1733 | for (const cricket::StreamParams& params : streams) { |
| 1734 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 1735 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 1736 | std::string label = params.sync_label.empty() |
| 1737 | ? rtc::ToString(params.first_ssrc()) |
| 1738 | : params.sync_label; |
| 1739 | auto data_channel_it = rtp_data_channels_.find(label); |
| 1740 | if (data_channel_it == rtp_data_channels_.end()) { |
| 1741 | // This is a new data channel. |
| 1742 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 1743 | } else { |
| 1744 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 1745 | } |
| 1746 | existing_channels.push_back(label); |
| 1747 | } |
| 1748 | |
| 1749 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 1750 | } |
| 1751 | |
| 1752 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 1753 | const std::vector<std::string>& active_channels, |
| 1754 | bool is_local_update) { |
| 1755 | auto it = rtp_data_channels_.begin(); |
| 1756 | while (it != rtp_data_channels_.end()) { |
| 1757 | DataChannel* data_channel = it->second; |
| 1758 | if (std::find(active_channels.begin(), active_channels.end(), |
| 1759 | data_channel->label()) != active_channels.end()) { |
| 1760 | ++it; |
| 1761 | continue; |
| 1762 | } |
| 1763 | |
| 1764 | if (is_local_update) { |
| 1765 | data_channel->SetSendSsrc(0); |
| 1766 | } else { |
| 1767 | data_channel->RemotePeerRequestClose(); |
| 1768 | } |
| 1769 | |
| 1770 | if (data_channel->state() == DataChannel::kClosed) { |
| 1771 | rtp_data_channels_.erase(it); |
| 1772 | it = rtp_data_channels_.begin(); |
| 1773 | } else { |
| 1774 | ++it; |
| 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 1780 | uint32_t remote_ssrc) { |
| 1781 | rtc::scoped_refptr<DataChannel> channel( |
| 1782 | InternalCreateDataChannel(label, nullptr)); |
| 1783 | if (!channel.get()) { |
| 1784 | LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 1785 | << "CreateDataChannel failed."; |
| 1786 | return; |
| 1787 | } |
| 1788 | channel->SetReceiveSsrc(remote_ssrc); |
| 1789 | observer_->OnDataChannel( |
| 1790 | DataChannelProxy::Create(signaling_thread(), channel)); |
| 1791 | } |
| 1792 | |
| 1793 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 1794 | const std::string& label, |
| 1795 | const InternalDataChannelInit* config) { |
| 1796 | if (IsClosed()) { |
| 1797 | return nullptr; |
| 1798 | } |
| 1799 | if (session_->data_channel_type() == cricket::DCT_NONE) { |
| 1800 | LOG(LS_ERROR) |
| 1801 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 1802 | return nullptr; |
| 1803 | } |
| 1804 | InternalDataChannelInit new_config = |
| 1805 | config ? (*config) : InternalDataChannelInit(); |
| 1806 | if (session_->data_channel_type() == cricket::DCT_SCTP) { |
| 1807 | if (new_config.id < 0) { |
| 1808 | rtc::SSLRole role; |
| 1809 | if (session_->GetSslRole(&role) && |
| 1810 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
| 1811 | LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel."; |
| 1812 | return nullptr; |
| 1813 | } |
| 1814 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
| 1815 | LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 1816 | << "because the id is already in use or out of range."; |
| 1817 | return nullptr; |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | rtc::scoped_refptr<DataChannel> channel(DataChannel::Create( |
| 1822 | session_.get(), session_->data_channel_type(), label, new_config)); |
| 1823 | if (!channel) { |
| 1824 | sid_allocator_.ReleaseSid(new_config.id); |
| 1825 | return nullptr; |
| 1826 | } |
| 1827 | |
| 1828 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 1829 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
| 1830 | LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 1831 | << " already exists."; |
| 1832 | return nullptr; |
| 1833 | } |
| 1834 | rtp_data_channels_[channel->label()] = channel; |
| 1835 | } else { |
| 1836 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 1837 | sctp_data_channels_.push_back(channel); |
| 1838 | channel->SignalClosed.connect(this, |
| 1839 | &PeerConnection::OnSctpDataChannelClosed); |
| 1840 | } |
| 1841 | |
| 1842 | return channel; |
| 1843 | } |
| 1844 | |
| 1845 | bool PeerConnection::HasDataChannels() const { |
| 1846 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 1847 | } |
| 1848 | |
| 1849 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 1850 | for (const auto& channel : sctp_data_channels_) { |
| 1851 | if (channel->id() < 0) { |
| 1852 | int sid; |
| 1853 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
| 1854 | LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
| 1855 | continue; |
| 1856 | } |
| 1857 | channel->SetSctpSid(sid); |
| 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
| 1863 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 1864 | ++it) { |
| 1865 | if (it->get() == channel) { |
| 1866 | if (channel->id() >= 0) { |
| 1867 | sid_allocator_.ReleaseSid(channel->id()); |
| 1868 | } |
| 1869 | sctp_data_channels_.erase(it); |
| 1870 | return; |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | void PeerConnection::OnVoiceChannelDestroyed() { |
| 1876 | EndRemoteTracks(cricket::MEDIA_TYPE_AUDIO); |
| 1877 | } |
| 1878 | |
| 1879 | void PeerConnection::OnVideoChannelDestroyed() { |
| 1880 | EndRemoteTracks(cricket::MEDIA_TYPE_VIDEO); |
| 1881 | } |
| 1882 | |
| 1883 | void PeerConnection::OnDataChannelCreated() { |
| 1884 | for (const auto& channel : sctp_data_channels_) { |
| 1885 | channel->OnTransportChannelCreated(); |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | void PeerConnection::OnDataChannelDestroyed() { |
| 1890 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 1891 | // DataChannel may callback to us and try to modify the list. |
| 1892 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 1893 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 1894 | for (const auto& kv : temp_rtp_dcs) { |
| 1895 | kv.second->OnTransportChannelDestroyed(); |
| 1896 | } |
| 1897 | |
| 1898 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 1899 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 1900 | for (const auto& channel : temp_sctp_dcs) { |
| 1901 | channel->OnTransportChannelDestroyed(); |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | void PeerConnection::OnDataChannelOpenMessage( |
| 1906 | const std::string& label, |
| 1907 | const InternalDataChannelInit& config) { |
| 1908 | rtc::scoped_refptr<DataChannel> channel( |
| 1909 | InternalCreateDataChannel(label, &config)); |
| 1910 | if (!channel.get()) { |
| 1911 | LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
| 1912 | return; |
| 1913 | } |
| 1914 | |
| 1915 | observer_->OnDataChannel( |
| 1916 | DataChannelProxy::Create(signaling_thread(), channel)); |
| 1917 | } |
| 1918 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1919 | std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator |
| 1920 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) { |
| 1921 | return std::find_if( |
| 1922 | senders_.begin(), senders_.end(), |
| 1923 | [track](const rtc::scoped_refptr<RtpSenderInterface>& sender) { |
| 1924 | return sender->track() == track; |
| 1925 | }); |
| 1926 | } |
| 1927 | |
| 1928 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator |
| 1929 | PeerConnection::FindReceiverForTrack(MediaStreamTrackInterface* track) { |
| 1930 | return std::find_if( |
| 1931 | receivers_.begin(), receivers_.end(), |
| 1932 | [track](const rtc::scoped_refptr<RtpReceiverInterface>& receiver) { |
| 1933 | return receiver->track() == track; |
| 1934 | }); |
| 1935 | } |
| 1936 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1937 | PeerConnection::TrackInfos* PeerConnection::GetRemoteTracks( |
| 1938 | cricket::MediaType media_type) { |
| 1939 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1940 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 1941 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &remote_audio_tracks_ |
| 1942 | : &remote_video_tracks_; |
| 1943 | } |
| 1944 | |
| 1945 | PeerConnection::TrackInfos* PeerConnection::GetLocalTracks( |
| 1946 | cricket::MediaType media_type) { |
| 1947 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1948 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 1949 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_tracks_ |
| 1950 | : &local_video_tracks_; |
| 1951 | } |
| 1952 | |
| 1953 | const PeerConnection::TrackInfo* PeerConnection::FindTrackInfo( |
| 1954 | const PeerConnection::TrackInfos& infos, |
| 1955 | const std::string& stream_label, |
| 1956 | const std::string track_id) const { |
| 1957 | for (const TrackInfo& track_info : infos) { |
| 1958 | if (track_info.stream_label == stream_label && |
| 1959 | track_info.track_id == track_id) { |
| 1960 | return &track_info; |
| 1961 | } |
| 1962 | } |
| 1963 | return nullptr; |
| 1964 | } |
| 1965 | |
| 1966 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 1967 | for (const auto& channel : sctp_data_channels_) { |
| 1968 | if (channel->id() == sid) { |
| 1969 | return channel; |
| 1970 | } |
| 1971 | } |
| 1972 | return nullptr; |
| 1973 | } |
| 1974 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1975 | } // namespace webrtc |