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