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