blob: f0efe908ea02d8242a02c2386da021358f2f4908 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/peerconnection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
deadbeefeb459812015-12-15 19:24:43 -080013#include <algorithm>
deadbeef0a6c4ca2015-10-06 11:38:28 -070014#include <cctype> // for isdigit
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
16#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#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 Kjellander15583c12016-02-10 10:53:12 +010028#include "webrtc/api/rtpreceiver.h"
29#include "webrtc/api/rtpsender.h"
30#include "webrtc/api/streamcollection.h"
perkja3ede6c2016-03-08 01:27:48 +010031#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010032#include "webrtc/api/videotrack.h"
tfarina5237aaf2015-11-10 23:44:30 -080033#include "webrtc/base/arraysize.h"
ivoc14d5dbe2016-07-04 07:06:55 -070034#include "webrtc/base/bind.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000035#include "webrtc/base/logging.h"
36#include "webrtc/base/stringencode.h"
deadbeefab9b2d12015-10-14 11:33:11 -070037#include "webrtc/base/stringutils.h"
Peter Boström1a9d6152015-12-08 22:15:17 +010038#include "webrtc/base/trace_event.h"
ossuf515ab82016-12-07 04:52:58 -080039#include "webrtc/call/call.h"
skvlad11a9cbf2016-10-07 11:53:05 -070040#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
deadbeefc0dad892017-01-04 20:28:21 -080041#include "webrtc/media/sctp/sctpdataengine.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010042#include "webrtc/pc/channelmanager.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010043#include "webrtc/system_wrappers/include/field_trial.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45namespace {
46
deadbeefab9b2d12015-10-14 11:33:11 -070047using webrtc::DataChannel;
48using webrtc::MediaConstraintsInterface;
49using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050using webrtc::PeerConnectionInterface;
deadbeefa601f5c2016-06-06 14:27:39 -070051using webrtc::RtpSenderInternal;
deadbeeffac06552015-11-25 11:26:01 -080052using webrtc::RtpSenderInterface;
deadbeefa601f5c2016-06-06 14:27:39 -070053using webrtc::RtpSenderProxy;
54using webrtc::RtpSenderProxyWithInternal;
deadbeefab9b2d12015-10-14 11:33:11 -070055using webrtc::StreamCollection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
deadbeefab9b2d12015-10-14 11:33:11 -070057static const char kDefaultStreamLabel[] = "default";
58static const char kDefaultAudioTrackLabel[] = "defaulta0";
59static const char kDefaultVideoTrackLabel[] = "defaultv0";
60
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061// The min number of tokens must present in Turn host uri.
62// e.g. user@turn.example.org
63static const size_t kTurnHostTokensNum = 2;
64// Number of tokens must be preset when TURN uri has transport param.
65static const size_t kTurnTransportTokensNum = 2;
66// The default stun port.
wu@webrtc.org91053e72013-08-10 07:18:04 +000067static const int kDefaultStunPort = 3478;
68static const int kDefaultStunTlsPort = 5349;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069static const char kTransport[] = "transport";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
71// NOTE: Must be in the same order as the ServiceType enum.
deadbeef0a6c4ca2015-10-06 11:38:28 -070072static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
zhihuang8f65cdf2016-05-06 18:40:30 -070074// The length of RTCP CNAMEs.
75static const int kRtcpCnameLength = 16;
76
deadbeef0a6c4ca2015-10-06 11:38:28 -070077// NOTE: A loop below assumes that the first value of this enum is 0 and all
78// other values are incremental.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079enum ServiceType {
deadbeef0a6c4ca2015-10-06 11:38:28 -070080 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.org28e20752013-07-10 00:45:36 +000085};
tfarina5237aaf2015-11-10 23:44:30 -080086static_assert(INVALID == arraysize(kValidIceServiceTypes),
deadbeef0a6c4ca2015-10-06 11:38:28 -070087 "kValidIceServiceTypes must have as many strings as ServiceType "
88 "has values.");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90enum {
wu@webrtc.org91053e72013-08-10 07:18:04 +000091 MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 MSG_SET_SESSIONDESCRIPTION_FAILED,
deadbeefab9b2d12015-10-14 11:33:11 -070093 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 MSG_GETSTATS,
deadbeefbd292462015-12-14 18:15:29 -080095 MSG_FREE_DATACHANNELS,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096};
97
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098struct SetSessionDescriptionMsg : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 explicit SetSessionDescriptionMsg(
100 webrtc::SetSessionDescriptionObserver* observer)
101 : observer(observer) {
102 }
103
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 std::string error;
106};
107
deadbeefab9b2d12015-10-14 11:33:11 -0700108struct 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.orgd4e598d2014-07-29 17:36:52 +0000117struct GetStatsMsg : public rtc::MessageData {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000118 GetStatsMsg(webrtc::StatsObserver* observer,
119 webrtc::MediaStreamTrackInterface* track)
120 : observer(observer), track(track) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000122 rtc::scoped_refptr<webrtc::StatsObserver> observer;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000123 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124};
125
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000126// |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
deadbeef0a6c4ca2015-10-06 11:38:28 -0700131//
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000132// draft-petithuguenin-behave-turn-uris-01
133// turnURI = scheme ":" turn-host [ ":" turn-port ]
134// turn-host = username@IP-literal / IPv4address / reg-name
135bool GetServiceTypeAndHostnameFromUri(const std::string& in_str,
136 ServiceType* service_type,
137 std::string* hostname) {
Tommi77d444a2015-04-24 15:38:38 +0200138 const std::string::size_type colonpos = in_str.find(':');
deadbeef0a6c4ca2015-10-06 11:38:28 -0700139 if (colonpos == std::string::npos) {
140 LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000141 return false;
142 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700143 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;
tfarina5237aaf2015-11-10 23:44:30 -0800148 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700149 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000150 *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
deadbeef0a6c4ca2015-10-06 11:38:28 -0700161bool 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.orga23f0ca2013-11-13 22:48:52 +0000171// 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,
deadbeef0a6c4ca2015-10-06 11:38:28 -0700175// |hostname|, |[IPv6 address]|, |IPv4 address|.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000176bool ParseHostnameAndPortFromString(const std::string& in_str,
177 std::string* host,
178 int* port) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700179 RTC_DCHECK(host->empty());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000180 if (in_str.at(0) == '[') {
181 std::string::size_type closebracket = in_str.rfind(']');
182 if (closebracket != std::string::npos) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000183 std::string::size_type colonpos = in_str.find(':', closebracket);
184 if (std::string::npos != colonpos) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700185 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos),
186 port)) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000187 return false;
188 }
189 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700190 *host = in_str.substr(1, closebracket - 1);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000191 } else {
192 return false;
193 }
194 } else {
195 std::string::size_type colonpos = in_str.find(':');
196 if (std::string::npos != colonpos) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700197 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000198 return false;
199 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700200 *host = in_str.substr(0, colonpos);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000201 } else {
202 *host = in_str;
203 }
204 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700205 return !host->empty();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000206}
207
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800208// Adds a STUN or TURN server to the appropriate list,
deadbeef0a6c4ca2015-10-06 11:38:28 -0700209// by parsing |url| and using the username/password in |server|.
deadbeef1e234612016-12-24 01:43:32 -0800210bool ParseIceServerUrl(const PeerConnectionInterface::IceServer& server,
211 const std::string& url,
212 cricket::ServerAddresses* stun_servers,
213 std::vector<cricket::RelayServerConfig>* turn_servers) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 // 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 Brandstetter0c7e9f52015-12-29 14:14:52 -0800228 RTC_DCHECK(stun_servers != nullptr);
229 RTC_DCHECK(turn_servers != nullptr);
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200230 std::vector<std::string> tokens;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800231 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700232 RTC_DCHECK(!url.empty());
hnslbd44bb02016-12-12 03:14:30 -0800233 rtc::tokenize_with_empty_tokens(url, '?', &tokens);
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200234 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];
hnslbd44bb02016-12-12 03:14:30 -0800238 rtc::tokenize_with_empty_tokens(uri_transport_param, '=', &tokens);
239 if (tokens[0] != kTransport) {
240 LOG(LS_WARNING) << "Invalid transport parameter key.";
deadbeef1e234612016-12-24 01:43:32 -0800241 return false;
hnslbd44bb02016-12-12 03:14:30 -0800242 }
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.";
deadbeef1e234612016-12-24 01:43:32 -0800248 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 }
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200252 std::string hoststring;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700253 ServiceType service_type;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200254 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport,
255 &service_type,
256 &hoststring)) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700257 LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
deadbeef1e234612016-12-24 01:43:32 -0800258 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200259 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000260
deadbeef0a6c4ca2015-10-06 11:38:28 -0700261 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
262 RTC_DCHECK(!hoststring.empty());
Tommi77d444a2015-04-24 15:38:38 +0200263
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200264 // Let's break hostname.
265 tokens.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -0700266 rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens);
267
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200268 std::string username(server.username);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700269 if (tokens.size() > kTurnHostTokensNum) {
270 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
deadbeef1e234612016-12-24 01:43:32 -0800271 return false;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700272 }
273 if (tokens.size() == kTurnHostTokensNum) {
274 if (tokens[0].empty() || tokens[1].empty()) {
275 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
deadbeef1e234612016-12-24 01:43:32 -0800276 return false;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700277 }
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200278 username.assign(rtc::s_url_decode(tokens[0]));
279 hoststring = tokens[1];
280 } else {
281 hoststring = tokens[0];
282 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000283
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200284 int port = kDefaultStunPort;
285 if (service_type == TURNS) {
286 port = kDefaultStunTlsPort;
hnsl277b2502016-12-13 05:17:23 -0800287 turn_transport_type = cricket::PROTO_TLS;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200288 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000289
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200290 std::string address;
291 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700292 LOG(WARNING) << "Invalid hostname format: " << uri_without_transport;
deadbeef1e234612016-12-24 01:43:32 -0800293 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200294 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000295
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200296 if (port <= 0 || port > 0xffff) {
297 LOG(WARNING) << "Invalid port: " << port;
deadbeef1e234612016-12-24 01:43:32 -0800298 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200301 switch (service_type) {
302 case STUN:
303 case STUNS:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800304 stun_servers->insert(rtc::SocketAddress(address, port));
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200305 break;
306 case TURN:
307 case TURNS: {
hnsl04833622017-01-09 08:35:45 -0800308 cricket::RelayServerConfig config = cricket::RelayServerConfig(
309 address, port, username, server.password, turn_transport_type);
310 if (server.tls_cert_policy ==
311 PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) {
312 config.tls_cert_policy =
313 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
314 }
315 turn_servers->push_back(config);
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200316 break;
317 }
deadbeef1e234612016-12-24 01:43:32 -0800318 case INVALID:
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200319 default:
deadbeef1e234612016-12-24 01:43:32 -0800320 LOG(WARNING) << "Configuration not supported: " << url;
321 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200322 }
deadbeef1e234612016-12-24 01:43:32 -0800323 return true;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200324}
325
deadbeefab9b2d12015-10-14 11:33:11 -0700326// Check if we can send |new_stream| on a PeerConnection.
327bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
328 webrtc::MediaStreamInterface* new_stream) {
329 if (!new_stream || !current_streams) {
330 return false;
331 }
332 if (current_streams->find(new_stream->label()) != nullptr) {
333 LOG(LS_ERROR) << "MediaStream with label " << new_stream->label()
334 << " is already added.";
335 return false;
336 }
337 return true;
338}
339
340bool MediaContentDirectionHasSend(cricket::MediaContentDirection dir) {
341 return dir == cricket::MD_SENDONLY || dir == cricket::MD_SENDRECV;
342}
343
deadbeef5e97fb52015-10-15 12:49:08 -0700344// If the direction is "recvonly" or "inactive", treat the description
345// as containing no streams.
346// See: https://code.google.com/p/webrtc/issues/detail?id=5054
347std::vector<cricket::StreamParams> GetActiveStreams(
348 const cricket::MediaContentDescription* desc) {
349 return MediaContentDirectionHasSend(desc->direction())
350 ? desc->streams()
351 : std::vector<cricket::StreamParams>();
352}
353
deadbeefab9b2d12015-10-14 11:33:11 -0700354bool IsValidOfferToReceiveMedia(int value) {
355 typedef PeerConnectionInterface::RTCOfferAnswerOptions Options;
356 return (value >= Options::kUndefined) &&
357 (value <= Options::kMaxOfferToReceiveMedia);
358}
359
360// Add the stream and RTP data channel info to |session_options|.
deadbeeffac06552015-11-25 11:26:01 -0800361void AddSendStreams(
362 cricket::MediaSessionOptions* session_options,
deadbeefa601f5c2016-06-06 14:27:39 -0700363 const std::vector<rtc::scoped_refptr<
364 RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders,
deadbeeffac06552015-11-25 11:26:01 -0800365 const std::map<std::string, rtc::scoped_refptr<DataChannel>>&
366 rtp_data_channels) {
deadbeefab9b2d12015-10-14 11:33:11 -0700367 session_options->streams.clear();
deadbeeffac06552015-11-25 11:26:01 -0800368 for (const auto& sender : senders) {
369 session_options->AddSendStream(sender->media_type(), sender->id(),
deadbeefa601f5c2016-06-06 14:27:39 -0700370 sender->internal()->stream_id());
deadbeefab9b2d12015-10-14 11:33:11 -0700371 }
372
373 // Check for data channels.
374 for (const auto& kv : rtp_data_channels) {
375 const DataChannel* channel = kv.second;
376 if (channel->state() == DataChannel::kConnecting ||
377 channel->state() == DataChannel::kOpen) {
378 // |streamid| and |sync_label| are both set to the DataChannel label
379 // here so they can be signaled the same way as MediaStreams and Tracks.
380 // For MediaStreams, the sync_label is the MediaStream label and the
381 // track label is the same as |streamid|.
382 const std::string& streamid = channel->label();
383 const std::string& sync_label = channel->label();
384 session_options->AddSendStream(cricket::MEDIA_TYPE_DATA, streamid,
385 sync_label);
386 }
387 }
388}
389
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390uint32_t ConvertIceTransportTypeToCandidateFilter(
391 PeerConnectionInterface::IceTransportsType type) {
392 switch (type) {
393 case PeerConnectionInterface::kNone:
394 return cricket::CF_NONE;
395 case PeerConnectionInterface::kRelay:
396 return cricket::CF_RELAY;
397 case PeerConnectionInterface::kNoHost:
398 return (cricket::CF_ALL & ~cricket::CF_HOST);
399 case PeerConnectionInterface::kAll:
400 return cricket::CF_ALL;
401 default:
402 ASSERT(false);
403 }
404 return cricket::CF_NONE;
405}
406
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700407// Helper method to set a voice/video channel on all applicable senders
408// and receivers when one is created/destroyed by WebRtcSession.
409//
410// Used by On(Voice|Video)Channel(Created|Destroyed)
411template <class SENDER,
412 class RECEIVER,
413 class CHANNEL,
414 class SENDERS,
415 class RECEIVERS>
416void SetChannelOnSendersAndReceivers(CHANNEL* channel,
417 SENDERS& senders,
418 RECEIVERS& receivers,
419 cricket::MediaType media_type) {
420 for (auto& sender : senders) {
421 if (sender->media_type() == media_type) {
422 static_cast<SENDER*>(sender->internal())->SetChannel(channel);
423 }
424 }
425 for (auto& receiver : receivers) {
426 if (receiver->media_type() == media_type) {
427 if (!channel) {
428 receiver->internal()->Stop();
429 }
430 static_cast<RECEIVER*>(receiver->internal())->SetChannel(channel);
431 }
432 }
433}
434
deadbeef0a6c4ca2015-10-06 11:38:28 -0700435} // namespace
436
437namespace webrtc {
438
deadbeef1e234612016-12-24 01:43:32 -0800439static const char* const kRtcErrorNames[] = {
deadbeef3edec7c2016-12-10 11:44:26 -0800440 "NONE",
441 "UNSUPPORTED_PARAMETER",
442 "INVALID_PARAMETER",
443 "INVALID_RANGE",
444 "SYNTAX_ERROR",
445 "INVALID_STATE",
446 "INVALID_MODIFICATION",
447 "NETWORK_ERROR",
448 "INTERNAL_ERROR",
449};
450
deadbeef1e234612016-12-24 01:43:32 -0800451std::ostream& operator<<(std::ostream& stream, RtcError error) {
deadbeef3edec7c2016-12-10 11:44:26 -0800452 int index = static_cast<int>(error);
deadbeef1e234612016-12-24 01:43:32 -0800453 RTC_CHECK(index < static_cast<int>(sizeof(kRtcErrorNames) /
454 sizeof(kRtcErrorNames[0])));
455 return stream << kRtcErrorNames[index];
deadbeef3edec7c2016-12-10 11:44:26 -0800456}
457
zhihuang8f65cdf2016-05-06 18:40:30 -0700458// Generate a RTCP CNAME when a PeerConnection is created.
459std::string GenerateRtcpCname() {
460 std::string cname;
461 if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
462 LOG(LS_ERROR) << "Failed to generate CNAME.";
463 RTC_DCHECK(false);
464 }
465 return cname;
466}
467
htaa2a49d92016-03-04 02:51:39 -0800468bool ExtractMediaSessionOptions(
deadbeefab9b2d12015-10-14 11:33:11 -0700469 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
htaaac2dea2016-03-10 13:35:55 -0800470 bool is_offer,
deadbeefab9b2d12015-10-14 11:33:11 -0700471 cricket::MediaSessionOptions* session_options) {
472 typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
473 if (!IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) ||
474 !IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video)) {
475 return false;
476 }
477
htaaac2dea2016-03-10 13:35:55 -0800478 // If constraints don't prevent us, we always accept video.
deadbeefc80741f2015-10-22 13:14:45 -0700479 if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) {
deadbeefab9b2d12015-10-14 11:33:11 -0700480 session_options->recv_audio = (rtc_options.offer_to_receive_audio > 0);
htaaac2dea2016-03-10 13:35:55 -0800481 } else {
482 session_options->recv_audio = true;
deadbeefab9b2d12015-10-14 11:33:11 -0700483 }
htaaac2dea2016-03-10 13:35:55 -0800484 // For offers, we only offer video if we have it or it's forced by options.
485 // For answers, we will always accept video (if offered).
deadbeefc80741f2015-10-22 13:14:45 -0700486 if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) {
deadbeefab9b2d12015-10-14 11:33:11 -0700487 session_options->recv_video = (rtc_options.offer_to_receive_video > 0);
htaaac2dea2016-03-10 13:35:55 -0800488 } else if (is_offer) {
489 session_options->recv_video = false;
490 } else {
491 session_options->recv_video = true;
deadbeefab9b2d12015-10-14 11:33:11 -0700492 }
493
494 session_options->vad_enabled = rtc_options.voice_activity_detection;
deadbeefc80741f2015-10-22 13:14:45 -0700495 session_options->bundle_enabled = rtc_options.use_rtp_mux;
deadbeef0ed85b22016-02-23 17:24:52 -0800496 for (auto& kv : session_options->transport_options) {
497 kv.second.ice_restart = rtc_options.ice_restart;
498 }
deadbeefab9b2d12015-10-14 11:33:11 -0700499
500 return true;
501}
502
503bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints,
504 cricket::MediaSessionOptions* session_options) {
505 bool value = false;
506 size_t mandatory_constraints_satisfied = 0;
507
508 // kOfferToReceiveAudio defaults to true according to spec.
509 if (!FindConstraint(constraints,
510 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
511 &mandatory_constraints_satisfied) ||
512 value) {
513 session_options->recv_audio = true;
514 }
515
516 // kOfferToReceiveVideo defaults to false according to spec. But
517 // if it is an answer and video is offered, we should still accept video
518 // per default.
519 value = false;
520 if (!FindConstraint(constraints,
521 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
522 &mandatory_constraints_satisfied) ||
523 value) {
524 session_options->recv_video = true;
525 }
526
527 if (FindConstraint(constraints,
528 MediaConstraintsInterface::kVoiceActivityDetection, &value,
529 &mandatory_constraints_satisfied)) {
530 session_options->vad_enabled = value;
531 }
532
533 if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value,
534 &mandatory_constraints_satisfied)) {
535 session_options->bundle_enabled = value;
536 } else {
537 // kUseRtpMux defaults to true according to spec.
538 session_options->bundle_enabled = true;
539 }
deadbeefab9b2d12015-10-14 11:33:11 -0700540
deadbeef0ed85b22016-02-23 17:24:52 -0800541 bool ice_restart = false;
deadbeefab9b2d12015-10-14 11:33:11 -0700542 if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart,
543 &value, &mandatory_constraints_satisfied)) {
deadbeefab9b2d12015-10-14 11:33:11 -0700544 // kIceRestart defaults to false according to spec.
deadbeef0ed85b22016-02-23 17:24:52 -0800545 ice_restart = true;
546 }
547 for (auto& kv : session_options->transport_options) {
548 kv.second.ice_restart = ice_restart;
deadbeefab9b2d12015-10-14 11:33:11 -0700549 }
550
551 if (!constraints) {
552 return true;
553 }
554 return mandatory_constraints_satisfied == constraints->GetMandatory().size();
555}
556
deadbeef1e234612016-12-24 01:43:32 -0800557bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
558 cricket::ServerAddresses* stun_servers,
559 std::vector<cricket::RelayServerConfig>* turn_servers) {
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200560 for (const webrtc::PeerConnectionInterface::IceServer& server : servers) {
561 if (!server.urls.empty()) {
562 for (const std::string& url : server.urls) {
Joachim Bauchd935f912015-05-29 22:14:21 +0200563 if (url.empty()) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700564 LOG(LS_ERROR) << "Empty uri.";
deadbeef1e234612016-12-24 01:43:32 -0800565 return false;
Joachim Bauchd935f912015-05-29 22:14:21 +0200566 }
deadbeef1e234612016-12-24 01:43:32 -0800567 if (!ParseIceServerUrl(server, url, stun_servers, turn_servers)) {
568 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200569 }
570 }
571 } else if (!server.uri.empty()) {
572 // Fallback to old .uri if new .urls isn't present.
deadbeef1e234612016-12-24 01:43:32 -0800573 if (!ParseIceServerUrl(server, server.uri, stun_servers, turn_servers)) {
574 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200575 }
576 } else {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700577 LOG(LS_ERROR) << "Empty uri.";
deadbeef1e234612016-12-24 01:43:32 -0800578 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 }
580 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800581 // Candidates must have unique priorities, so that connectivity checks
582 // are performed in a well-defined order.
583 int priority = static_cast<int>(turn_servers->size() - 1);
584 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
585 // First in the list gets highest priority.
586 turn_server.priority = priority--;
587 }
deadbeef1e234612016-12-24 01:43:32 -0800588 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589}
590
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591PeerConnection::PeerConnection(PeerConnectionFactory* factory)
592 : factory_(factory),
593 observer_(NULL),
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000594 uma_observer_(NULL),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 signaling_state_(kStable),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 ice_connection_state_(kIceConnectionNew),
deadbeefab9b2d12015-10-14 11:33:11 -0700597 ice_gathering_state_(kIceGatheringNew),
nisse30612762016-12-20 05:03:58 -0800598 event_log_(RtcEventLog::Create()),
zhihuang8f65cdf2016-05-06 18:40:30 -0700599 rtcp_cname_(GenerateRtcpCname()),
deadbeefab9b2d12015-10-14 11:33:11 -0700600 local_streams_(StreamCollection::Create()),
601 remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602
603PeerConnection::~PeerConnection() {
Peter Boström1a9d6152015-12-08 22:15:17 +0100604 TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection");
deadbeef0a6c4ca2015-10-06 11:38:28 -0700605 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef70ab1a12015-09-28 16:53:55 -0700606 // Need to detach RTP senders/receivers from WebRtcSession,
607 // since it's about to be destroyed.
608 for (const auto& sender : senders_) {
deadbeefa601f5c2016-06-06 14:27:39 -0700609 sender->internal()->Stop();
deadbeef70ab1a12015-09-28 16:53:55 -0700610 }
611 for (const auto& receiver : receivers_) {
deadbeefa601f5c2016-06-06 14:27:39 -0700612 receiver->internal()->Stop();
deadbeef70ab1a12015-09-28 16:53:55 -0700613 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700614 // Destroy stats_ because it depends on session_.
615 stats_.reset(nullptr);
hbosb78306a2016-12-19 05:06:57 -0800616 if (stats_collector_) {
617 stats_collector_->WaitForPendingRequest();
618 stats_collector_ = nullptr;
619 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700620 // Now destroy session_ before destroying other members,
621 // because its destruction fires signals (such as VoiceChannelDestroyed)
622 // which will trigger some final actions in PeerConnection...
623 session_.reset(nullptr);
deadbeef91dd5672016-05-18 16:55:30 -0700624 // port_allocator_ lives on the network thread and should be destroyed there.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700625 network_thread()->Invoke<void>(RTC_FROM_HERE,
626 [this] { port_allocator_.reset(nullptr); });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627}
628
629bool PeerConnection::Initialize(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000630 const PeerConnectionInterface::RTCConfiguration& configuration,
kwibergd1fe2812016-04-27 06:47:29 -0700631 std::unique_ptr<cricket::PortAllocator> allocator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200632 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
deadbeef653b8e02015-11-11 12:55:10 -0800633 PeerConnectionObserver* observer) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100634 TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
deadbeef1e234612016-12-24 01:43:32 -0800635 RTC_DCHECK(observer != nullptr);
deadbeef653b8e02015-11-11 12:55:10 -0800636 if (!observer) {
637 return false;
638 }
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000639 observer_ = observer;
deadbeef1e234612016-12-24 01:43:32 -0800640
kwiberg0eb15ed2015-12-17 03:04:15 -0800641 port_allocator_ = std::move(allocator);
deadbeef653b8e02015-11-11 12:55:10 -0800642
deadbeef91dd5672016-05-18 16:55:30 -0700643 // The port allocator lives on the network thread and should be initialized
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700644 // there.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700645 if (!network_thread()->Invoke<bool>(
646 RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n,
647 this, configuration))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 return false;
649 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650
skvlad11a9cbf2016-10-07 11:53:05 -0700651 media_controller_.reset(factory_->CreateMediaController(
652 configuration.media_config, event_log_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653
zhihuang29ff8442016-07-27 11:07:25 -0700654 session_.reset(new WebRtcSession(
655 media_controller_.get(), factory_->network_thread(),
656 factory_->worker_thread(), factory_->signaling_thread(),
657 port_allocator_.get(),
658 std::unique_ptr<cricket::TransportController>(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700659 factory_->CreateTransportController(
660 port_allocator_.get(),
deadbeefc0dad892017-01-04 20:28:21 -0800661 configuration.redetermine_role_on_ice_restart))));
zhihuang29ff8442016-07-27 11:07:25 -0700662
deadbeefab9b2d12015-10-14 11:33:11 -0700663 stats_.reset(new StatsCollector(this));
hbos74e1a4f2016-09-15 23:33:01 -0700664 stats_collector_ = RTCStatsCollector::Create(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665
666 // Initialize the WebRtcSession. It creates transport channels etc.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200667 if (!session_->Initialize(factory_->options(), std::move(cert_generator),
htaa2a49d92016-03-04 02:51:39 -0800668 configuration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 return false;
deadbeefab9b2d12015-10-14 11:33:11 -0700670 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 // Register PeerConnection as receiver of local ice candidates.
673 // All the callbacks will be posted to the application from PeerConnection.
674 session_->RegisterIceObserver(this);
675 session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700676 session_->SignalVoiceChannelCreated.connect(
677 this, &PeerConnection::OnVoiceChannelCreated);
deadbeefab9b2d12015-10-14 11:33:11 -0700678 session_->SignalVoiceChannelDestroyed.connect(
679 this, &PeerConnection::OnVoiceChannelDestroyed);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700680 session_->SignalVideoChannelCreated.connect(
681 this, &PeerConnection::OnVideoChannelCreated);
deadbeefab9b2d12015-10-14 11:33:11 -0700682 session_->SignalVideoChannelDestroyed.connect(
683 this, &PeerConnection::OnVideoChannelDestroyed);
684 session_->SignalDataChannelCreated.connect(
685 this, &PeerConnection::OnDataChannelCreated);
686 session_->SignalDataChannelDestroyed.connect(
687 this, &PeerConnection::OnDataChannelDestroyed);
688 session_->SignalDataChannelOpenMessage.connect(
689 this, &PeerConnection::OnDataChannelOpenMessage);
deadbeef46c73892016-11-16 19:42:04 -0800690
691 configuration_ = configuration;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 return true;
693}
694
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000695rtc::scoped_refptr<StreamCollectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696PeerConnection::local_streams() {
deadbeefab9b2d12015-10-14 11:33:11 -0700697 return local_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698}
699
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000700rtc::scoped_refptr<StreamCollectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701PeerConnection::remote_streams() {
deadbeefab9b2d12015-10-14 11:33:11 -0700702 return remote_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703}
704
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000705bool PeerConnection::AddStream(MediaStreamInterface* local_stream) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100706 TRACE_EVENT0("webrtc", "PeerConnection::AddStream");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 if (IsClosed()) {
708 return false;
709 }
deadbeefab9b2d12015-10-14 11:33:11 -0700710 if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 return false;
712 }
deadbeefab9b2d12015-10-14 11:33:11 -0700713
714 local_streams_->AddStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -0800715 MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
716 observer->SignalAudioTrackAdded.connect(this,
717 &PeerConnection::OnAudioTrackAdded);
718 observer->SignalAudioTrackRemoved.connect(
719 this, &PeerConnection::OnAudioTrackRemoved);
720 observer->SignalVideoTrackAdded.connect(this,
721 &PeerConnection::OnVideoTrackAdded);
722 observer->SignalVideoTrackRemoved.connect(
723 this, &PeerConnection::OnVideoTrackRemoved);
kwibergd1fe2812016-04-27 06:47:29 -0700724 stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer));
deadbeefab9b2d12015-10-14 11:33:11 -0700725
deadbeefab9b2d12015-10-14 11:33:11 -0700726 for (const auto& track : local_stream->GetAudioTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800727 OnAudioTrackAdded(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700728 }
729 for (const auto& track : local_stream->GetVideoTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800730 OnVideoTrackAdded(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700731 }
732
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000733 stats_->AddStream(local_stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 observer_->OnRenegotiationNeeded();
735 return true;
736}
737
738void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100739 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
deadbeefab9b2d12015-10-14 11:33:11 -0700740 for (const auto& track : local_stream->GetAudioTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800741 OnAudioTrackRemoved(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700742 }
743 for (const auto& track : local_stream->GetVideoTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800744 OnVideoTrackRemoved(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700745 }
746
747 local_streams_->RemoveStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -0800748 stream_observers_.erase(
749 std::remove_if(
750 stream_observers_.begin(), stream_observers_.end(),
kwibergd1fe2812016-04-27 06:47:29 -0700751 [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) {
deadbeefeb459812015-12-15 19:24:43 -0800752 return observer->stream()->label().compare(local_stream->label()) ==
753 0;
754 }),
755 stream_observers_.end());
deadbeefab9b2d12015-10-14 11:33:11 -0700756
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 if (IsClosed()) {
758 return;
759 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 observer_->OnRenegotiationNeeded();
761}
762
deadbeefe1f9d832016-01-14 15:35:42 -0800763rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack(
764 MediaStreamTrackInterface* track,
765 std::vector<MediaStreamInterface*> streams) {
766 TRACE_EVENT0("webrtc", "PeerConnection::AddTrack");
767 if (IsClosed()) {
768 return nullptr;
769 }
770 if (streams.size() >= 2) {
771 LOG(LS_ERROR)
772 << "Adding a track with two streams is not currently supported.";
773 return nullptr;
774 }
775 // TODO(deadbeef): Support adding a track to two different senders.
776 if (FindSenderForTrack(track) != senders_.end()) {
777 LOG(LS_ERROR) << "Sender for track " << track->id() << " already exists.";
778 return nullptr;
779 }
780
781 // TODO(deadbeef): Support adding a track to multiple streams.
deadbeefa601f5c2016-06-06 14:27:39 -0700782 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender;
deadbeefe1f9d832016-01-14 15:35:42 -0800783 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
deadbeefa601f5c2016-06-06 14:27:39 -0700784 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
deadbeefe1f9d832016-01-14 15:35:42 -0800785 signaling_thread(),
786 new AudioRtpSender(static_cast<AudioTrackInterface*>(track),
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700787 session_->voice_channel(), stats_.get()));
deadbeefe1f9d832016-01-14 15:35:42 -0800788 if (!streams.empty()) {
deadbeefa601f5c2016-06-06 14:27:39 -0700789 new_sender->internal()->set_stream_id(streams[0]->label());
deadbeefe1f9d832016-01-14 15:35:42 -0800790 }
791 const TrackInfo* track_info = FindTrackInfo(
deadbeefa601f5c2016-06-06 14:27:39 -0700792 local_audio_tracks_, new_sender->internal()->stream_id(), track->id());
deadbeefe1f9d832016-01-14 15:35:42 -0800793 if (track_info) {
deadbeefa601f5c2016-06-06 14:27:39 -0700794 new_sender->internal()->SetSsrc(track_info->ssrc);
deadbeefe1f9d832016-01-14 15:35:42 -0800795 }
796 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
deadbeefa601f5c2016-06-06 14:27:39 -0700797 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
deadbeefe1f9d832016-01-14 15:35:42 -0800798 signaling_thread(),
799 new VideoRtpSender(static_cast<VideoTrackInterface*>(track),
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700800 session_->video_channel()));
deadbeefe1f9d832016-01-14 15:35:42 -0800801 if (!streams.empty()) {
deadbeefa601f5c2016-06-06 14:27:39 -0700802 new_sender->internal()->set_stream_id(streams[0]->label());
deadbeefe1f9d832016-01-14 15:35:42 -0800803 }
804 const TrackInfo* track_info = FindTrackInfo(
deadbeefa601f5c2016-06-06 14:27:39 -0700805 local_video_tracks_, new_sender->internal()->stream_id(), track->id());
deadbeefe1f9d832016-01-14 15:35:42 -0800806 if (track_info) {
deadbeefa601f5c2016-06-06 14:27:39 -0700807 new_sender->internal()->SetSsrc(track_info->ssrc);
deadbeefe1f9d832016-01-14 15:35:42 -0800808 }
809 } else {
810 LOG(LS_ERROR) << "CreateSender called with invalid kind: " << track->kind();
811 return rtc::scoped_refptr<RtpSenderInterface>();
812 }
813
814 senders_.push_back(new_sender);
815 observer_->OnRenegotiationNeeded();
816 return new_sender;
817}
818
819bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) {
820 TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack");
821 if (IsClosed()) {
822 return false;
823 }
824
825 auto it = std::find(senders_.begin(), senders_.end(), sender);
826 if (it == senders_.end()) {
827 LOG(LS_ERROR) << "Couldn't find sender " << sender->id() << " to remove.";
828 return false;
829 }
deadbeefa601f5c2016-06-06 14:27:39 -0700830 (*it)->internal()->Stop();
deadbeefe1f9d832016-01-14 15:35:42 -0800831 senders_.erase(it);
832
833 observer_->OnRenegotiationNeeded();
834 return true;
835}
836
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000837rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 AudioTrackInterface* track) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100839 TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender");
zhihuang29ff8442016-07-27 11:07:25 -0700840 if (IsClosed()) {
841 return nullptr;
842 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 if (!track) {
844 LOG(LS_ERROR) << "CreateDtmfSender - track is NULL.";
845 return NULL;
846 }
deadbeefab9b2d12015-10-14 11:33:11 -0700847 if (!local_streams_->FindAudioTrack(track->id())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track.";
849 return NULL;
850 }
851
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000852 rtc::scoped_refptr<DtmfSenderInterface> sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 DtmfSender::Create(track, signaling_thread(), session_.get()));
854 if (!sender.get()) {
855 LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create.";
856 return NULL;
857 }
858 return DtmfSenderProxy::Create(signaling_thread(), sender.get());
859}
860
deadbeeffac06552015-11-25 11:26:01 -0800861rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800862 const std::string& kind,
863 const std::string& stream_id) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100864 TRACE_EVENT0("webrtc", "PeerConnection::CreateSender");
zhihuang29ff8442016-07-27 11:07:25 -0700865 if (IsClosed()) {
866 return nullptr;
867 }
deadbeefa601f5c2016-06-06 14:27:39 -0700868 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800869 if (kind == MediaStreamTrackInterface::kAudioKind) {
deadbeefa601f5c2016-06-06 14:27:39 -0700870 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700871 signaling_thread(),
872 new AudioRtpSender(session_->voice_channel(), stats_.get()));
deadbeeffac06552015-11-25 11:26:01 -0800873 } else if (kind == MediaStreamTrackInterface::kVideoKind) {
deadbeefa601f5c2016-06-06 14:27:39 -0700874 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700875 signaling_thread(), new VideoRtpSender(session_->video_channel()));
deadbeeffac06552015-11-25 11:26:01 -0800876 } else {
877 LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind;
deadbeefe1f9d832016-01-14 15:35:42 -0800878 return new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800879 }
deadbeefbd7d8f72015-12-18 16:58:44 -0800880 if (!stream_id.empty()) {
deadbeefa601f5c2016-06-06 14:27:39 -0700881 new_sender->internal()->set_stream_id(stream_id);
deadbeefbd7d8f72015-12-18 16:58:44 -0800882 }
deadbeeffac06552015-11-25 11:26:01 -0800883 senders_.push_back(new_sender);
deadbeefe1f9d832016-01-14 15:35:42 -0800884 return new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800885}
886
deadbeef70ab1a12015-09-28 16:53:55 -0700887std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders()
888 const {
deadbeefa601f5c2016-06-06 14:27:39 -0700889 std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret;
890 for (const auto& sender : senders_) {
891 ret.push_back(sender.get());
892 }
893 return ret;
deadbeef70ab1a12015-09-28 16:53:55 -0700894}
895
896std::vector<rtc::scoped_refptr<RtpReceiverInterface>>
897PeerConnection::GetReceivers() const {
deadbeefa601f5c2016-06-06 14:27:39 -0700898 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret;
899 for (const auto& receiver : receivers_) {
900 ret.push_back(receiver.get());
901 }
902 return ret;
deadbeef70ab1a12015-09-28 16:53:55 -0700903}
904
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905bool PeerConnection::GetStats(StatsObserver* observer,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000906 MediaStreamTrackInterface* track,
907 StatsOutputLevel level) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100908 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
deadbeef0a6c4ca2015-10-06 11:38:28 -0700909 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 if (!VERIFY(observer != NULL)) {
911 LOG(LS_ERROR) << "GetStats - observer is NULL.";
912 return false;
913 }
914
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000915 stats_->UpdateStats(level);
zhihuange9e94c32016-11-04 11:38:15 -0700916 // The StatsCollector is used to tell if a track is valid because it may
917 // remember tracks that the PeerConnection previously removed.
918 if (track && !stats_->IsValidTrack(track->id())) {
919 LOG(LS_WARNING) << "GetStats is called with an invalid track: "
920 << track->id();
921 return false;
922 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700923 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS,
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000924 new GetStatsMsg(observer, track));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 return true;
926}
927
hbos74e1a4f2016-09-15 23:33:01 -0700928void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) {
929 RTC_DCHECK(stats_collector_);
930 stats_collector_->GetStatsReport(callback);
931}
932
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933PeerConnectionInterface::SignalingState PeerConnection::signaling_state() {
934 return signaling_state_;
935}
936
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937PeerConnectionInterface::IceConnectionState
938PeerConnection::ice_connection_state() {
939 return ice_connection_state_;
940}
941
942PeerConnectionInterface::IceGatheringState
943PeerConnection::ice_gathering_state() {
944 return ice_gathering_state_;
945}
946
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000947rtc::scoped_refptr<DataChannelInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948PeerConnection::CreateDataChannel(
949 const std::string& label,
950 const DataChannelInit* config) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100951 TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel");
zhihuang9763d562016-08-05 11:14:50 -0700952#ifdef HAVE_QUIC
953 if (session_->data_channel_type() == cricket::DCT_QUIC) {
954 // TODO(zhihuang): Handle case when config is NULL.
955 if (!config) {
956 LOG(LS_ERROR) << "Missing config for QUIC data channel.";
957 return nullptr;
958 }
959 // TODO(zhihuang): Allow unreliable or ordered QUIC data channels.
960 if (!config->reliable || config->ordered) {
961 LOG(LS_ERROR) << "QUIC data channel does not implement unreliable or "
962 "ordered delivery.";
963 return nullptr;
964 }
965 return session_->quic_data_transport()->CreateDataChannel(label, config);
966 }
967#endif // HAVE_QUIC
968
deadbeefab9b2d12015-10-14 11:33:11 -0700969 bool first_datachannel = !HasDataChannels();
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000970
kwibergd1fe2812016-04-27 06:47:29 -0700971 std::unique_ptr<InternalDataChannelInit> internal_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000972 if (config) {
973 internal_config.reset(new InternalDataChannelInit(*config));
974 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000975 rtc::scoped_refptr<DataChannelInterface> channel(
deadbeefab9b2d12015-10-14 11:33:11 -0700976 InternalCreateDataChannel(label, internal_config.get()));
977 if (!channel.get()) {
978 return nullptr;
979 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000981 // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or
982 // the first SCTP DataChannel.
983 if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) {
984 observer_->OnRenegotiationNeeded();
985 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000986
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 return DataChannelProxy::Create(signaling_thread(), channel.get());
988}
989
990void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
991 const MediaConstraintsInterface* constraints) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100992 TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer");
deadbeefab9b2d12015-10-14 11:33:11 -0700993 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
995 return;
996 }
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000997 RTCOfferAnswerOptions options;
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000998
999 bool value;
1000 size_t mandatory_constraints = 0;
1001
1002 if (FindConstraint(constraints,
1003 MediaConstraintsInterface::kOfferToReceiveAudio,
1004 &value,
1005 &mandatory_constraints)) {
1006 options.offer_to_receive_audio =
1007 value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0;
1008 }
1009
1010 if (FindConstraint(constraints,
1011 MediaConstraintsInterface::kOfferToReceiveVideo,
1012 &value,
1013 &mandatory_constraints)) {
1014 options.offer_to_receive_video =
1015 value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0;
1016 }
1017
1018 if (FindConstraint(constraints,
1019 MediaConstraintsInterface::kVoiceActivityDetection,
1020 &value,
1021 &mandatory_constraints)) {
1022 options.voice_activity_detection = value;
1023 }
1024
1025 if (FindConstraint(constraints,
1026 MediaConstraintsInterface::kIceRestart,
1027 &value,
1028 &mandatory_constraints)) {
1029 options.ice_restart = value;
1030 }
1031
1032 if (FindConstraint(constraints,
1033 MediaConstraintsInterface::kUseRtpMux,
1034 &value,
1035 &mandatory_constraints)) {
1036 options.use_rtp_mux = value;
1037 }
1038
1039 CreateOffer(observer, options);
1040}
1041
1042void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
1043 const RTCOfferAnswerOptions& options) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001044 TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer");
deadbeefab9b2d12015-10-14 11:33:11 -07001045 if (!VERIFY(observer != nullptr)) {
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00001046 LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
1047 return;
1048 }
deadbeefab9b2d12015-10-14 11:33:11 -07001049
1050 cricket::MediaSessionOptions session_options;
1051 if (!GetOptionsForOffer(options, &session_options)) {
1052 std::string error = "CreateOffer called with invalid options.";
1053 LOG(LS_ERROR) << error;
1054 PostCreateSessionDescriptionFailure(observer, error);
1055 return;
1056 }
1057
1058 session_->CreateOffer(observer, options, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059}
1060
1061void PeerConnection::CreateAnswer(
1062 CreateSessionDescriptionObserver* observer,
1063 const MediaConstraintsInterface* constraints) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001064 TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer");
deadbeefab9b2d12015-10-14 11:33:11 -07001065 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
1067 return;
1068 }
deadbeefab9b2d12015-10-14 11:33:11 -07001069
1070 cricket::MediaSessionOptions session_options;
1071 if (!GetOptionsForAnswer(constraints, &session_options)) {
1072 std::string error = "CreateAnswer called with invalid constraints.";
1073 LOG(LS_ERROR) << error;
1074 PostCreateSessionDescriptionFailure(observer, error);
1075 return;
1076 }
1077
htaa2a49d92016-03-04 02:51:39 -08001078 session_->CreateAnswer(observer, session_options);
1079}
1080
1081void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer,
1082 const RTCOfferAnswerOptions& options) {
1083 TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer");
1084 if (!VERIFY(observer != nullptr)) {
1085 LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
1086 return;
1087 }
1088
1089 cricket::MediaSessionOptions session_options;
1090 if (!GetOptionsForAnswer(options, &session_options)) {
1091 std::string error = "CreateAnswer called with invalid options.";
1092 LOG(LS_ERROR) << error;
1093 PostCreateSessionDescriptionFailure(observer, error);
1094 return;
1095 }
1096
1097 session_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098}
1099
1100void PeerConnection::SetLocalDescription(
1101 SetSessionDescriptionObserver* observer,
1102 SessionDescriptionInterface* desc) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001103 TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription");
zhihuang29ff8442016-07-27 11:07:25 -07001104 if (IsClosed()) {
1105 return;
1106 }
deadbeefab9b2d12015-10-14 11:33:11 -07001107 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
1109 return;
1110 }
1111 if (!desc) {
1112 PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL.");
1113 return;
1114 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 // Update stats here so that we have the most recent stats for tracks and
1116 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001117 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 std::string error;
1119 if (!session_->SetLocalDescription(desc, &error)) {
1120 PostSetSessionDescriptionFailure(observer, error);
1121 return;
1122 }
deadbeefab9b2d12015-10-14 11:33:11 -07001123
1124 // If setting the description decided our SSL role, allocate any necessary
1125 // SCTP sids.
1126 rtc::SSLRole role;
1127 if (session_->data_channel_type() == cricket::DCT_SCTP &&
deadbeefc0dad892017-01-04 20:28:21 -08001128 session_->GetSslRole(session_->data_channel(), &role)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001129 AllocateSctpSids(role);
1130 }
1131
1132 // Update state and SSRC of local MediaStreams and DataChannels based on the
1133 // local session description.
1134 const cricket::ContentInfo* audio_content =
1135 GetFirstAudioContent(desc->description());
1136 if (audio_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001137 if (audio_content->rejected) {
1138 RemoveTracks(cricket::MEDIA_TYPE_AUDIO);
1139 } else {
1140 const cricket::AudioContentDescription* audio_desc =
1141 static_cast<const cricket::AudioContentDescription*>(
1142 audio_content->description);
1143 UpdateLocalTracks(audio_desc->streams(), audio_desc->type());
1144 }
deadbeefab9b2d12015-10-14 11:33:11 -07001145 }
1146
1147 const cricket::ContentInfo* video_content =
1148 GetFirstVideoContent(desc->description());
1149 if (video_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001150 if (video_content->rejected) {
1151 RemoveTracks(cricket::MEDIA_TYPE_VIDEO);
1152 } else {
1153 const cricket::VideoContentDescription* video_desc =
1154 static_cast<const cricket::VideoContentDescription*>(
1155 video_content->description);
1156 UpdateLocalTracks(video_desc->streams(), video_desc->type());
1157 }
deadbeefab9b2d12015-10-14 11:33:11 -07001158 }
1159
1160 const cricket::ContentInfo* data_content =
1161 GetFirstDataContent(desc->description());
1162 if (data_content) {
1163 const cricket::DataContentDescription* data_desc =
1164 static_cast<const cricket::DataContentDescription*>(
1165 data_content->description);
1166 if (rtc::starts_with(data_desc->protocol().data(),
1167 cricket::kMediaProtocolRtpPrefix)) {
1168 UpdateLocalRtpDataChannels(data_desc->streams());
1169 }
1170 }
1171
1172 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001173 signaling_thread()->Post(RTC_FROM_HERE, this,
1174 MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
deadbeefab9b2d12015-10-14 11:33:11 -07001175
deadbeefcbecd352015-09-23 11:50:27 -07001176 // MaybeStartGathering needs to be called after posting
1177 // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates
1178 // before signaling that SetLocalDescription completed.
1179 session_->MaybeStartGathering();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180}
1181
1182void PeerConnection::SetRemoteDescription(
1183 SetSessionDescriptionObserver* observer,
1184 SessionDescriptionInterface* desc) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001185 TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription");
zhihuang29ff8442016-07-27 11:07:25 -07001186 if (IsClosed()) {
1187 return;
1188 }
deadbeefab9b2d12015-10-14 11:33:11 -07001189 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
1191 return;
1192 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 if (!desc) {
1194 PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL.");
1195 return;
1196 }
1197 // Update stats here so that we have the most recent stats for tracks and
1198 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001199 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 std::string error;
1201 if (!session_->SetRemoteDescription(desc, &error)) {
1202 PostSetSessionDescriptionFailure(observer, error);
1203 return;
1204 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205
deadbeefab9b2d12015-10-14 11:33:11 -07001206 // If setting the description decided our SSL role, allocate any necessary
1207 // SCTP sids.
1208 rtc::SSLRole role;
1209 if (session_->data_channel_type() == cricket::DCT_SCTP &&
deadbeefc0dad892017-01-04 20:28:21 -08001210 session_->GetSslRole(session_->data_channel(), &role)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001211 AllocateSctpSids(role);
1212 }
1213
1214 const cricket::SessionDescription* remote_desc = desc->description();
deadbeefbda7e0b2015-12-08 17:13:40 -08001215 const cricket::ContentInfo* audio_content = GetFirstAudioContent(remote_desc);
1216 const cricket::ContentInfo* video_content = GetFirstVideoContent(remote_desc);
1217 const cricket::AudioContentDescription* audio_desc =
1218 GetFirstAudioContentDescription(remote_desc);
1219 const cricket::VideoContentDescription* video_desc =
1220 GetFirstVideoContentDescription(remote_desc);
1221 const cricket::DataContentDescription* data_desc =
1222 GetFirstDataContentDescription(remote_desc);
1223
1224 // Check if the descriptions include streams, just in case the peer supports
1225 // MSID, but doesn't indicate so with "a=msid-semantic".
1226 if (remote_desc->msid_supported() ||
1227 (audio_desc && !audio_desc->streams().empty()) ||
1228 (video_desc && !video_desc->streams().empty())) {
1229 remote_peer_supports_msid_ = true;
1230 }
deadbeefab9b2d12015-10-14 11:33:11 -07001231
1232 // We wait to signal new streams until we finish processing the description,
1233 // since only at that point will new streams have all their tracks.
1234 rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
1235
1236 // Find all audio rtp streams and create corresponding remote AudioTracks
1237 // and MediaStreams.
deadbeefab9b2d12015-10-14 11:33:11 -07001238 if (audio_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001239 if (audio_content->rejected) {
1240 RemoveTracks(cricket::MEDIA_TYPE_AUDIO);
1241 } else {
deadbeefbda7e0b2015-12-08 17:13:40 -08001242 bool default_audio_track_needed =
1243 !remote_peer_supports_msid_ &&
1244 MediaContentDirectionHasSend(audio_desc->direction());
1245 UpdateRemoteStreamsList(GetActiveStreams(audio_desc),
1246 default_audio_track_needed, audio_desc->type(),
deadbeeffaac4972015-11-12 15:33:07 -08001247 new_streams);
deadbeeffaac4972015-11-12 15:33:07 -08001248 }
deadbeefab9b2d12015-10-14 11:33:11 -07001249 }
1250
1251 // Find all video rtp streams and create corresponding remote VideoTracks
1252 // and MediaStreams.
deadbeefab9b2d12015-10-14 11:33:11 -07001253 if (video_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001254 if (video_content->rejected) {
1255 RemoveTracks(cricket::MEDIA_TYPE_VIDEO);
1256 } else {
deadbeefbda7e0b2015-12-08 17:13:40 -08001257 bool default_video_track_needed =
1258 !remote_peer_supports_msid_ &&
1259 MediaContentDirectionHasSend(video_desc->direction());
1260 UpdateRemoteStreamsList(GetActiveStreams(video_desc),
1261 default_video_track_needed, video_desc->type(),
deadbeeffaac4972015-11-12 15:33:07 -08001262 new_streams);
deadbeeffaac4972015-11-12 15:33:07 -08001263 }
deadbeefab9b2d12015-10-14 11:33:11 -07001264 }
1265
1266 // Update the DataChannels with the information from the remote peer.
deadbeefbda7e0b2015-12-08 17:13:40 -08001267 if (data_desc) {
1268 if (rtc::starts_with(data_desc->protocol().data(),
deadbeefab9b2d12015-10-14 11:33:11 -07001269 cricket::kMediaProtocolRtpPrefix)) {
deadbeefbda7e0b2015-12-08 17:13:40 -08001270 UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc));
deadbeefab9b2d12015-10-14 11:33:11 -07001271 }
1272 }
1273
1274 // Iterate new_streams and notify the observer about new MediaStreams.
1275 for (size_t i = 0; i < new_streams->count(); ++i) {
1276 MediaStreamInterface* new_stream = new_streams->at(i);
1277 stats_->AddStream(new_stream);
Taylor Brandstetter98cde262016-05-31 13:02:21 -07001278 // Call both the raw pointer and scoped_refptr versions of the method
1279 // for compatibility.
deadbeefab9b2d12015-10-14 11:33:11 -07001280 observer_->OnAddStream(new_stream);
Taylor Brandstetter98cde262016-05-31 13:02:21 -07001281 observer_->OnAddStream(
1282 rtc::scoped_refptr<MediaStreamInterface>(new_stream));
deadbeefab9b2d12015-10-14 11:33:11 -07001283 }
1284
deadbeefbda7e0b2015-12-08 17:13:40 -08001285 UpdateEndedRemoteMediaStreams();
deadbeefab9b2d12015-10-14 11:33:11 -07001286
1287 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001288 signaling_thread()->Post(RTC_FROM_HERE, this,
1289 MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
deadbeeffc648b62015-10-13 16:42:33 -07001290}
1291
deadbeef46c73892016-11-16 19:42:04 -08001292PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() {
1293 return configuration_;
1294}
1295
deadbeef1e234612016-12-24 01:43:32 -08001296bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001297 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
deadbeef6de92f92016-12-12 18:49:32 -08001298
1299 if (session_->local_description() &&
1300 configuration.ice_candidate_pool_size !=
1301 configuration_.ice_candidate_pool_size) {
1302 LOG(LS_ERROR) << "Can't change candidate pool size after calling "
1303 "SetLocalDescription.";
deadbeef1e234612016-12-24 01:43:32 -08001304 return false;
1305 }
1306 // TODO(deadbeef): Return false and log an error if there are any unsupported
1307 // modifications.
1308 if (port_allocator_) {
1309 if (!network_thread()->Invoke<bool>(
1310 RTC_FROM_HERE,
1311 rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this,
1312 configuration))) {
1313 LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator.";
1314 return false;
1315 }
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001316 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001317
deadbeef1e234612016-12-24 01:43:32 -08001318 // TODO(deadbeef): Shouldn't have to hop to the network thread twice...
1319 session_->SetIceConfig(session_->ParseIceConfig(configuration));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001320
deadbeefd1a38b52016-12-10 13:15:33 -08001321 // As described in JSEP, calling setConfiguration with new ICE servers or
1322 // candidate policy must set a "needs-ice-restart" bit so that the next offer
1323 // triggers an ICE restart which will pick up the changes.
deadbeef1e234612016-12-24 01:43:32 -08001324 if (configuration.servers != configuration_.servers ||
1325 configuration.type != configuration_.type) {
deadbeefd1a38b52016-12-10 13:15:33 -08001326 session_->SetNeedsIceRestartFlag();
1327 }
deadbeef1e234612016-12-24 01:43:32 -08001328 configuration_ = configuration;
1329 return true;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001330}
1331
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332bool PeerConnection::AddIceCandidate(
1333 const IceCandidateInterface* ice_candidate) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001334 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
zhihuang29ff8442016-07-27 11:07:25 -07001335 if (IsClosed()) {
1336 return false;
1337 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 return session_->ProcessIceMessage(ice_candidate);
1339}
1340
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001341bool PeerConnection::RemoveIceCandidates(
1342 const std::vector<cricket::Candidate>& candidates) {
1343 TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates");
1344 return session_->RemoveRemoteIceCandidates(candidates);
1345}
1346
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001347void PeerConnection::RegisterUMAObserver(UMAObserver* observer) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001348 TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver");
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001349 uma_observer_ = observer;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001350
1351 if (session_) {
1352 session_->set_metrics_observer(uma_observer_);
1353 }
1354
mallinath@webrtc.orgd37bcfa2014-05-12 23:10:18 +00001355 // Send information about IPv4/IPv6 status.
deadbeef1e234612016-12-24 01:43:32 -08001356 if (uma_observer_ && port_allocator_) {
Honghai Zhangd93f50c2016-10-05 11:47:22 -07001357 port_allocator_->SetMetricsObserver(uma_observer_);
mallinath@webrtc.orgd37bcfa2014-05-12 23:10:18 +00001358 if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) {
Guo-wei Shiehdfbe6792015-09-03 17:12:07 -07001359 uma_observer_->IncrementEnumCounter(
1360 kEnumCounterAddressFamily, kPeerConnection_IPv6,
1361 kPeerConnectionAddressFamilyCounter_Max);
mallinath@webrtc.orgb445f262014-05-23 22:19:37 +00001362 } else {
Guo-wei Shiehdfbe6792015-09-03 17:12:07 -07001363 uma_observer_->IncrementEnumCounter(
1364 kEnumCounterAddressFamily, kPeerConnection_IPv4,
1365 kPeerConnectionAddressFamilyCounter_Max);
mallinath@webrtc.orgd37bcfa2014-05-12 23:10:18 +00001366 }
1367 }
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001368}
1369
ivoc14d5dbe2016-07-04 07:06:55 -07001370bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file,
1371 int64_t max_size_bytes) {
1372 return factory_->worker_thread()->Invoke<bool>(
1373 RTC_FROM_HERE, rtc::Bind(&PeerConnection::StartRtcEventLog_w, this, file,
1374 max_size_bytes));
1375}
1376
1377void PeerConnection::StopRtcEventLog() {
1378 factory_->worker_thread()->Invoke<void>(
1379 RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this));
1380}
1381
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382const SessionDescriptionInterface* PeerConnection::local_description() const {
1383 return session_->local_description();
1384}
1385
1386const SessionDescriptionInterface* PeerConnection::remote_description() const {
1387 return session_->remote_description();
1388}
1389
deadbeeffe4a8a42016-12-20 17:56:17 -08001390const SessionDescriptionInterface* PeerConnection::current_local_description()
1391 const {
1392 return session_->current_local_description();
1393}
1394
1395const SessionDescriptionInterface* PeerConnection::current_remote_description()
1396 const {
1397 return session_->current_remote_description();
1398}
1399
1400const SessionDescriptionInterface* PeerConnection::pending_local_description()
1401 const {
1402 return session_->pending_local_description();
1403}
1404
1405const SessionDescriptionInterface* PeerConnection::pending_remote_description()
1406 const {
1407 return session_->pending_remote_description();
1408}
1409
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410void PeerConnection::Close() {
Peter Boström1a9d6152015-12-08 22:15:17 +01001411 TRACE_EVENT0("webrtc", "PeerConnection::Close");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 // Update stats here so that we have the most recent stats for tracks and
1413 // streams before the channels are closed.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001414 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415
deadbeefd59daf82015-10-14 15:02:44 -07001416 session_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417}
1418
deadbeefd59daf82015-10-14 15:02:44 -07001419void PeerConnection::OnSessionStateChange(WebRtcSession* /*session*/,
1420 WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 switch (state) {
deadbeefd59daf82015-10-14 15:02:44 -07001422 case WebRtcSession::STATE_INIT:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 ChangeSignalingState(PeerConnectionInterface::kStable);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001424 break;
deadbeefd59daf82015-10-14 15:02:44 -07001425 case WebRtcSession::STATE_SENTOFFER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer);
1427 break;
deadbeefd59daf82015-10-14 15:02:44 -07001428 case WebRtcSession::STATE_SENTPRANSWER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429 ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer);
1430 break;
deadbeefd59daf82015-10-14 15:02:44 -07001431 case WebRtcSession::STATE_RECEIVEDOFFER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer);
1433 break;
deadbeefd59daf82015-10-14 15:02:44 -07001434 case WebRtcSession::STATE_RECEIVEDPRANSWER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer);
1436 break;
deadbeefd59daf82015-10-14 15:02:44 -07001437 case WebRtcSession::STATE_INPROGRESS:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 ChangeSignalingState(PeerConnectionInterface::kStable);
1439 break;
deadbeefd59daf82015-10-14 15:02:44 -07001440 case WebRtcSession::STATE_CLOSED:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 ChangeSignalingState(PeerConnectionInterface::kClosed);
1442 break;
1443 default:
1444 break;
1445 }
1446}
1447
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001448void PeerConnection::OnMessage(rtc::Message* msg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 switch (msg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 case MSG_SET_SESSIONDESCRIPTION_SUCCESS: {
1451 SetSessionDescriptionMsg* param =
1452 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
1453 param->observer->OnSuccess();
1454 delete param;
1455 break;
1456 }
1457 case MSG_SET_SESSIONDESCRIPTION_FAILED: {
1458 SetSessionDescriptionMsg* param =
1459 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
1460 param->observer->OnFailure(param->error);
1461 delete param;
1462 break;
1463 }
deadbeefab9b2d12015-10-14 11:33:11 -07001464 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
1465 CreateSessionDescriptionMsg* param =
1466 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
1467 param->observer->OnFailure(param->error);
1468 delete param;
1469 break;
1470 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 case MSG_GETSTATS: {
1472 GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata);
nisseb36ee8d2016-12-20 03:30:00 -08001473 std::unique_ptr<StatsReports> reports(new StatsReports);
1474 stats_->GetStats(param->track, reports.get());
1475 param->observer->OnCompleteReports(std::move(reports));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 delete param;
1477 break;
1478 }
deadbeefbd292462015-12-14 18:15:29 -08001479 case MSG_FREE_DATACHANNELS: {
1480 sctp_data_channels_to_free_.clear();
1481 break;
1482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 default:
deadbeef0a6c4ca2015-10-06 11:38:28 -07001484 RTC_DCHECK(false && "Not implemented");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 break;
1486 }
1487}
1488
deadbeefab9b2d12015-10-14 11:33:11 -07001489void PeerConnection::CreateAudioReceiver(MediaStreamInterface* stream,
perkjd61bf802016-03-24 03:16:19 -07001490 const std::string& track_id,
deadbeefab9b2d12015-10-14 11:33:11 -07001491 uint32_t ssrc) {
zhihuang81c3a032016-11-17 12:06:24 -08001492 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1493 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -07001494 signaling_thread(), new AudioRtpReceiver(stream, track_id, ssrc,
zhihuang81c3a032016-11-17 12:06:24 -08001495 session_->voice_channel()));
1496
1497 receivers_.push_back(receiver);
1498 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
1499 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
1500 observer_->OnAddTrack(receiver, streams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501}
1502
deadbeefab9b2d12015-10-14 11:33:11 -07001503void PeerConnection::CreateVideoReceiver(MediaStreamInterface* stream,
perkjf0dcfe22016-03-10 18:32:00 +01001504 const std::string& track_id,
deadbeefab9b2d12015-10-14 11:33:11 -07001505 uint32_t ssrc) {
zhihuang81c3a032016-11-17 12:06:24 -08001506 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1507 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
deadbeefa601f5c2016-06-06 14:27:39 -07001508 signaling_thread(),
1509 new VideoRtpReceiver(stream, track_id, factory_->worker_thread(),
zhihuang81c3a032016-11-17 12:06:24 -08001510 ssrc, session_->video_channel()));
1511 receivers_.push_back(receiver);
1512 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
1513 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
1514 observer_->OnAddTrack(receiver, streams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515}
1516
deadbeef70ab1a12015-09-28 16:53:55 -07001517// TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote
1518// description.
perkjd61bf802016-03-24 03:16:19 -07001519void PeerConnection::DestroyReceiver(const std::string& track_id) {
1520 auto it = FindReceiverForTrack(track_id);
deadbeef70ab1a12015-09-28 16:53:55 -07001521 if (it == receivers_.end()) {
perkjd61bf802016-03-24 03:16:19 -07001522 LOG(LS_WARNING) << "RtpReceiver for track with id " << track_id
deadbeef70ab1a12015-09-28 16:53:55 -07001523 << " doesn't exist.";
1524 } else {
deadbeefa601f5c2016-06-06 14:27:39 -07001525 (*it)->internal()->Stop();
deadbeef70ab1a12015-09-28 16:53:55 -07001526 receivers_.erase(it);
1527 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528}
1529
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530void PeerConnection::OnIceConnectionChange(
1531 PeerConnectionInterface::IceConnectionState new_state) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001532 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001533 // After transitioning to "closed", ignore any additional states from
1534 // WebRtcSession (such as "disconnected").
deadbeefab9b2d12015-10-14 11:33:11 -07001535 if (IsClosed()) {
deadbeefcbecd352015-09-23 11:50:27 -07001536 return;
1537 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 ice_connection_state_ = new_state;
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001539 observer_->OnIceConnectionChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540}
1541
1542void PeerConnection::OnIceGatheringChange(
1543 PeerConnectionInterface::IceGatheringState new_state) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001544 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 if (IsClosed()) {
1546 return;
1547 }
1548 ice_gathering_state_ = new_state;
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001549 observer_->OnIceGatheringChange(ice_gathering_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550}
1551
1552void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001553 RTC_DCHECK(signaling_thread()->IsCurrent());
zhihuang29ff8442016-07-27 11:07:25 -07001554 if (IsClosed()) {
1555 return;
1556 }
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001557 observer_->OnIceCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558}
1559
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001560void PeerConnection::OnIceCandidatesRemoved(
1561 const std::vector<cricket::Candidate>& candidates) {
1562 RTC_DCHECK(signaling_thread()->IsCurrent());
zhihuang29ff8442016-07-27 11:07:25 -07001563 if (IsClosed()) {
1564 return;
1565 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001566 observer_->OnIceCandidatesRemoved(candidates);
1567}
1568
Peter Thatcher54360512015-07-08 11:08:35 -07001569void PeerConnection::OnIceConnectionReceivingChange(bool receiving) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001570 RTC_DCHECK(signaling_thread()->IsCurrent());
zhihuang29ff8442016-07-27 11:07:25 -07001571 if (IsClosed()) {
1572 return;
1573 }
Peter Thatcher54360512015-07-08 11:08:35 -07001574 observer_->OnIceConnectionReceivingChange(receiving);
1575}
1576
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577void PeerConnection::ChangeSignalingState(
1578 PeerConnectionInterface::SignalingState signaling_state) {
1579 signaling_state_ = signaling_state;
1580 if (signaling_state == kClosed) {
1581 ice_connection_state_ = kIceConnectionClosed;
1582 observer_->OnIceConnectionChange(ice_connection_state_);
1583 if (ice_gathering_state_ != kIceGatheringComplete) {
1584 ice_gathering_state_ = kIceGatheringComplete;
1585 observer_->OnIceGatheringChange(ice_gathering_state_);
1586 }
1587 }
1588 observer_->OnSignalingChange(signaling_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589}
1590
deadbeefeb459812015-12-15 19:24:43 -08001591void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track,
1592 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07001593 if (IsClosed()) {
1594 return;
1595 }
deadbeefeb459812015-12-15 19:24:43 -08001596 auto sender = FindSenderForTrack(track);
1597 if (sender != senders_.end()) {
1598 // We already have a sender for this track, so just change the stream_id
1599 // so that it's correct in the next call to CreateOffer.
deadbeefa601f5c2016-06-06 14:27:39 -07001600 (*sender)->internal()->set_stream_id(stream->label());
deadbeefeb459812015-12-15 19:24:43 -08001601 return;
1602 }
1603
1604 // Normal case; we've never seen this track before.
deadbeefa601f5c2016-06-06 14:27:39 -07001605 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender =
1606 RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -07001607 signaling_thread(),
1608 new AudioRtpSender(track, stream->label(), session_->voice_channel(),
1609 stats_.get()));
deadbeefeb459812015-12-15 19:24:43 -08001610 senders_.push_back(new_sender);
1611 // If the sender has already been configured in SDP, we call SetSsrc,
1612 // which will connect the sender to the underlying transport. This can
1613 // occur if a local session description that contains the ID of the sender
1614 // is set before AddStream is called. It can also occur if the local
1615 // session description is not changed and RemoveStream is called, and
1616 // later AddStream is called again with the same stream.
1617 const TrackInfo* track_info =
1618 FindTrackInfo(local_audio_tracks_, stream->label(), track->id());
1619 if (track_info) {
deadbeefa601f5c2016-06-06 14:27:39 -07001620 new_sender->internal()->SetSsrc(track_info->ssrc);
deadbeefeb459812015-12-15 19:24:43 -08001621 }
1622}
1623
1624// TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
1625// indefinitely, when we have unified plan SDP.
1626void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track,
1627 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07001628 if (IsClosed()) {
1629 return;
1630 }
deadbeefeb459812015-12-15 19:24:43 -08001631 auto sender = FindSenderForTrack(track);
1632 if (sender == senders_.end()) {
1633 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1634 << " doesn't exist.";
1635 return;
1636 }
deadbeefa601f5c2016-06-06 14:27:39 -07001637 (*sender)->internal()->Stop();
deadbeefeb459812015-12-15 19:24:43 -08001638 senders_.erase(sender);
1639}
1640
1641void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track,
1642 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07001643 if (IsClosed()) {
1644 return;
1645 }
deadbeefeb459812015-12-15 19:24:43 -08001646 auto sender = FindSenderForTrack(track);
1647 if (sender != senders_.end()) {
1648 // We already have a sender for this track, so just change the stream_id
1649 // so that it's correct in the next call to CreateOffer.
deadbeefa601f5c2016-06-06 14:27:39 -07001650 (*sender)->internal()->set_stream_id(stream->label());
deadbeefeb459812015-12-15 19:24:43 -08001651 return;
1652 }
1653
1654 // Normal case; we've never seen this track before.
deadbeefa601f5c2016-06-06 14:27:39 -07001655 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender =
1656 RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -07001657 signaling_thread(), new VideoRtpSender(track, stream->label(),
1658 session_->video_channel()));
deadbeefeb459812015-12-15 19:24:43 -08001659 senders_.push_back(new_sender);
1660 const TrackInfo* track_info =
1661 FindTrackInfo(local_video_tracks_, stream->label(), track->id());
1662 if (track_info) {
deadbeefa601f5c2016-06-06 14:27:39 -07001663 new_sender->internal()->SetSsrc(track_info->ssrc);
deadbeefeb459812015-12-15 19:24:43 -08001664 }
1665}
1666
1667void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track,
1668 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07001669 if (IsClosed()) {
1670 return;
1671 }
deadbeefeb459812015-12-15 19:24:43 -08001672 auto sender = FindSenderForTrack(track);
1673 if (sender == senders_.end()) {
1674 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1675 << " doesn't exist.";
1676 return;
1677 }
deadbeefa601f5c2016-06-06 14:27:39 -07001678 (*sender)->internal()->Stop();
deadbeefeb459812015-12-15 19:24:43 -08001679 senders_.erase(sender);
1680}
1681
deadbeefab9b2d12015-10-14 11:33:11 -07001682void PeerConnection::PostSetSessionDescriptionFailure(
1683 SetSessionDescriptionObserver* observer,
1684 const std::string& error) {
1685 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
1686 msg->error = error;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001687 signaling_thread()->Post(RTC_FROM_HERE, this,
1688 MSG_SET_SESSIONDESCRIPTION_FAILED, msg);
deadbeefab9b2d12015-10-14 11:33:11 -07001689}
1690
1691void PeerConnection::PostCreateSessionDescriptionFailure(
1692 CreateSessionDescriptionObserver* observer,
1693 const std::string& error) {
1694 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
1695 msg->error = error;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001696 signaling_thread()->Post(RTC_FROM_HERE, this,
1697 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
deadbeefab9b2d12015-10-14 11:33:11 -07001698}
1699
1700bool PeerConnection::GetOptionsForOffer(
1701 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
1702 cricket::MediaSessionOptions* session_options) {
deadbeef0ed85b22016-02-23 17:24:52 -08001703 // TODO(deadbeef): Once we have transceivers, enumerate them here instead of
1704 // ContentInfos.
1705 if (session_->local_description()) {
1706 for (const cricket::ContentInfo& content :
1707 session_->local_description()->description()->contents()) {
1708 session_options->transport_options[content.name] =
1709 cricket::TransportOptions();
1710 }
1711 }
deadbeef46c73892016-11-16 19:42:04 -08001712 session_options->enable_ice_renomination =
1713 configuration_.enable_ice_renomination;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001714
htaaac2dea2016-03-10 13:35:55 -08001715 if (!ExtractMediaSessionOptions(rtc_options, true, session_options)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001716 return false;
1717 }
1718
deadbeeffac06552015-11-25 11:26:01 -08001719 AddSendStreams(session_options, senders_, rtp_data_channels_);
deadbeefc80741f2015-10-22 13:14:45 -07001720 // Offer to receive audio/video if the constraint is not set and there are
1721 // send streams, or we're currently receiving.
1722 if (rtc_options.offer_to_receive_audio == RTCOfferAnswerOptions::kUndefined) {
1723 session_options->recv_audio =
1724 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO) ||
1725 !remote_audio_tracks_.empty();
1726 }
1727 if (rtc_options.offer_to_receive_video == RTCOfferAnswerOptions::kUndefined) {
1728 session_options->recv_video =
1729 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO) ||
1730 !remote_video_tracks_.empty();
1731 }
deadbeefc80741f2015-10-22 13:14:45 -07001732
zhihuang9763d562016-08-05 11:14:50 -07001733 // Intentionally unset the data channel type for RTP data channel with the
1734 // second condition. Otherwise the RTP data channels would be successfully
1735 // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail
1736 // when building with chromium. We want to leave RTP data channels broken, so
1737 // people won't try to use them.
1738 if (HasDataChannels() && session_->data_channel_type() != cricket::DCT_RTP) {
1739 session_options->data_channel_type = session_->data_channel_type();
deadbeefab9b2d12015-10-14 11:33:11 -07001740 }
zhihuang8f65cdf2016-05-06 18:40:30 -07001741
zhihuangaf388472016-11-02 16:49:48 -07001742 session_options->bundle_enabled =
1743 session_options->bundle_enabled &&
1744 (session_options->has_audio() || session_options->has_video() ||
1745 session_options->has_data());
1746
zhihuang8f65cdf2016-05-06 18:40:30 -07001747 session_options->rtcp_cname = rtcp_cname_;
jbauchcb560652016-08-04 05:20:32 -07001748 session_options->crypto_options = factory_->options().crypto_options;
deadbeefab9b2d12015-10-14 11:33:11 -07001749 return true;
1750}
1751
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001752void PeerConnection::InitializeOptionsForAnswer(
1753 cricket::MediaSessionOptions* session_options) {
1754 session_options->recv_audio = false;
1755 session_options->recv_video = false;
deadbeef46c73892016-11-16 19:42:04 -08001756 session_options->enable_ice_renomination =
1757 configuration_.enable_ice_renomination;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001758}
1759
htaa2a49d92016-03-04 02:51:39 -08001760void PeerConnection::FinishOptionsForAnswer(
deadbeefab9b2d12015-10-14 11:33:11 -07001761 cricket::MediaSessionOptions* session_options) {
deadbeef0ed85b22016-02-23 17:24:52 -08001762 // TODO(deadbeef): Once we have transceivers, enumerate them here instead of
1763 // ContentInfos.
1764 if (session_->remote_description()) {
1765 // Initialize the transport_options map.
1766 for (const cricket::ContentInfo& content :
1767 session_->remote_description()->description()->contents()) {
1768 session_options->transport_options[content.name] =
1769 cricket::TransportOptions();
1770 }
1771 }
deadbeeffac06552015-11-25 11:26:01 -08001772 AddSendStreams(session_options, senders_, rtp_data_channels_);
deadbeefab9b2d12015-10-14 11:33:11 -07001773 // RTP data channel is handled in MediaSessionOptions::AddStream. SCTP streams
1774 // are not signaled in the SDP so does not go through that path and must be
1775 // handled here.
zhihuang9763d562016-08-05 11:14:50 -07001776 // Intentionally unset the data channel type for RTP data channel. Otherwise
1777 // the RTP data channels would be successfully negotiated by default and the
1778 // unit tests in WebRtcDataBrowserTest will fail when building with chromium.
1779 // We want to leave RTP data channels broken, so people won't try to use them.
1780 if (session_->data_channel_type() != cricket::DCT_RTP) {
1781 session_options->data_channel_type = session_->data_channel_type();
deadbeef907abe42016-08-04 12:22:18 -07001782 }
zhihuangaf388472016-11-02 16:49:48 -07001783 session_options->bundle_enabled =
1784 session_options->bundle_enabled &&
1785 (session_options->has_audio() || session_options->has_video() ||
1786 session_options->has_data());
1787
jbauchcb560652016-08-04 05:20:32 -07001788 session_options->crypto_options = factory_->options().crypto_options;
htaa2a49d92016-03-04 02:51:39 -08001789}
1790
1791bool PeerConnection::GetOptionsForAnswer(
1792 const MediaConstraintsInterface* constraints,
1793 cricket::MediaSessionOptions* session_options) {
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001794 InitializeOptionsForAnswer(session_options);
htaa2a49d92016-03-04 02:51:39 -08001795 if (!ParseConstraintsForAnswer(constraints, session_options)) {
1796 return false;
1797 }
zhihuang8f65cdf2016-05-06 18:40:30 -07001798 session_options->rtcp_cname = rtcp_cname_;
1799
htaa2a49d92016-03-04 02:51:39 -08001800 FinishOptionsForAnswer(session_options);
1801 return true;
1802}
1803
1804bool PeerConnection::GetOptionsForAnswer(
1805 const RTCOfferAnswerOptions& options,
1806 cricket::MediaSessionOptions* session_options) {
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001807 InitializeOptionsForAnswer(session_options);
htaaac2dea2016-03-10 13:35:55 -08001808 if (!ExtractMediaSessionOptions(options, false, session_options)) {
htaa2a49d92016-03-04 02:51:39 -08001809 return false;
1810 }
zhihuang8f65cdf2016-05-06 18:40:30 -07001811 session_options->rtcp_cname = rtcp_cname_;
1812
htaa2a49d92016-03-04 02:51:39 -08001813 FinishOptionsForAnswer(session_options);
deadbeefab9b2d12015-10-14 11:33:11 -07001814 return true;
1815}
1816
deadbeeffaac4972015-11-12 15:33:07 -08001817void PeerConnection::RemoveTracks(cricket::MediaType media_type) {
1818 UpdateLocalTracks(std::vector<cricket::StreamParams>(), media_type);
deadbeefbda7e0b2015-12-08 17:13:40 -08001819 UpdateRemoteStreamsList(std::vector<cricket::StreamParams>(), false,
1820 media_type, nullptr);
deadbeeffaac4972015-11-12 15:33:07 -08001821}
1822
deadbeefab9b2d12015-10-14 11:33:11 -07001823void PeerConnection::UpdateRemoteStreamsList(
1824 const cricket::StreamParamsVec& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -08001825 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -07001826 cricket::MediaType media_type,
1827 StreamCollection* new_streams) {
1828 TrackInfos* current_tracks = GetRemoteTracks(media_type);
1829
1830 // Find removed tracks. I.e., tracks where the track id or ssrc don't match
deadbeeffac06552015-11-25 11:26:01 -08001831 // the new StreamParam.
deadbeefab9b2d12015-10-14 11:33:11 -07001832 auto track_it = current_tracks->begin();
1833 while (track_it != current_tracks->end()) {
1834 const TrackInfo& info = *track_it;
1835 const cricket::StreamParams* params =
1836 cricket::GetStreamBySsrc(streams, info.ssrc);
deadbeefbda7e0b2015-12-08 17:13:40 -08001837 bool track_exists = params && params->id == info.track_id;
1838 // If this is a default track, and we still need it, don't remove it.
1839 if ((info.stream_label == kDefaultStreamLabel && default_track_needed) ||
1840 track_exists) {
1841 ++track_it;
1842 } else {
deadbeefab9b2d12015-10-14 11:33:11 -07001843 OnRemoteTrackRemoved(info.stream_label, info.track_id, media_type);
1844 track_it = current_tracks->erase(track_it);
deadbeefab9b2d12015-10-14 11:33:11 -07001845 }
1846 }
1847
1848 // Find new and active tracks.
1849 for (const cricket::StreamParams& params : streams) {
1850 // The sync_label is the MediaStream label and the |stream.id| is the
1851 // track id.
1852 const std::string& stream_label = params.sync_label;
1853 const std::string& track_id = params.id;
1854 uint32_t ssrc = params.first_ssrc();
1855
1856 rtc::scoped_refptr<MediaStreamInterface> stream =
1857 remote_streams_->find(stream_label);
1858 if (!stream) {
1859 // This is a new MediaStream. Create a new remote MediaStream.
perkjd61bf802016-03-24 03:16:19 -07001860 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
1861 MediaStream::Create(stream_label));
deadbeefab9b2d12015-10-14 11:33:11 -07001862 remote_streams_->AddStream(stream);
1863 new_streams->AddStream(stream);
1864 }
1865
1866 const TrackInfo* track_info =
1867 FindTrackInfo(*current_tracks, stream_label, track_id);
1868 if (!track_info) {
1869 current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc));
1870 OnRemoteTrackSeen(stream_label, track_id, ssrc, media_type);
1871 }
1872 }
deadbeefbda7e0b2015-12-08 17:13:40 -08001873
1874 // Add default track if necessary.
1875 if (default_track_needed) {
1876 rtc::scoped_refptr<MediaStreamInterface> default_stream =
1877 remote_streams_->find(kDefaultStreamLabel);
1878 if (!default_stream) {
1879 // Create the new default MediaStream.
perkjd61bf802016-03-24 03:16:19 -07001880 default_stream = MediaStreamProxy::Create(
1881 rtc::Thread::Current(), MediaStream::Create(kDefaultStreamLabel));
deadbeefbda7e0b2015-12-08 17:13:40 -08001882 remote_streams_->AddStream(default_stream);
1883 new_streams->AddStream(default_stream);
1884 }
1885 std::string default_track_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
1886 ? kDefaultAudioTrackLabel
1887 : kDefaultVideoTrackLabel;
1888 const TrackInfo* default_track_info =
1889 FindTrackInfo(*current_tracks, kDefaultStreamLabel, default_track_id);
1890 if (!default_track_info) {
1891 current_tracks->push_back(
1892 TrackInfo(kDefaultStreamLabel, default_track_id, 0));
1893 OnRemoteTrackSeen(kDefaultStreamLabel, default_track_id, 0, media_type);
1894 }
1895 }
deadbeefab9b2d12015-10-14 11:33:11 -07001896}
1897
1898void PeerConnection::OnRemoteTrackSeen(const std::string& stream_label,
1899 const std::string& track_id,
1900 uint32_t ssrc,
1901 cricket::MediaType media_type) {
1902 MediaStreamInterface* stream = remote_streams_->find(stream_label);
1903
1904 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
perkjd61bf802016-03-24 03:16:19 -07001905 CreateAudioReceiver(stream, track_id, ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -07001906 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
perkjf0dcfe22016-03-10 18:32:00 +01001907 CreateVideoReceiver(stream, track_id, ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -07001908 } else {
1909 RTC_DCHECK(false && "Invalid media type");
1910 }
1911}
1912
1913void PeerConnection::OnRemoteTrackRemoved(const std::string& stream_label,
1914 const std::string& track_id,
1915 cricket::MediaType media_type) {
1916 MediaStreamInterface* stream = remote_streams_->find(stream_label);
1917
1918 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
perkjd61bf802016-03-24 03:16:19 -07001919 // When the MediaEngine audio channel is destroyed, the RemoteAudioSource
1920 // will be notified which will end the AudioRtpReceiver::track().
1921 DestroyReceiver(track_id);
deadbeefab9b2d12015-10-14 11:33:11 -07001922 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1923 stream->FindAudioTrack(track_id);
1924 if (audio_track) {
deadbeefab9b2d12015-10-14 11:33:11 -07001925 stream->RemoveTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -07001926 }
1927 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
perkjd61bf802016-03-24 03:16:19 -07001928 // Stopping or destroying a VideoRtpReceiver will end the
1929 // VideoRtpReceiver::track().
1930 DestroyReceiver(track_id);
deadbeefab9b2d12015-10-14 11:33:11 -07001931 rtc::scoped_refptr<VideoTrackInterface> video_track =
1932 stream->FindVideoTrack(track_id);
1933 if (video_track) {
perkjd61bf802016-03-24 03:16:19 -07001934 // There's no guarantee the track is still available, e.g. the track may
1935 // have been removed from the stream by an application.
deadbeefab9b2d12015-10-14 11:33:11 -07001936 stream->RemoveTrack(video_track);
deadbeefab9b2d12015-10-14 11:33:11 -07001937 }
1938 } else {
1939 ASSERT(false && "Invalid media type");
1940 }
1941}
1942
1943void PeerConnection::UpdateEndedRemoteMediaStreams() {
1944 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
1945 for (size_t i = 0; i < remote_streams_->count(); ++i) {
1946 MediaStreamInterface* stream = remote_streams_->at(i);
1947 if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
1948 streams_to_remove.push_back(stream);
1949 }
1950 }
1951
Taylor Brandstetter98cde262016-05-31 13:02:21 -07001952 for (auto& stream : streams_to_remove) {
deadbeefab9b2d12015-10-14 11:33:11 -07001953 remote_streams_->RemoveStream(stream);
Taylor Brandstetter98cde262016-05-31 13:02:21 -07001954 // Call both the raw pointer and scoped_refptr versions of the method
1955 // for compatibility.
1956 observer_->OnRemoveStream(stream.get());
1957 observer_->OnRemoveStream(std::move(stream));
deadbeefab9b2d12015-10-14 11:33:11 -07001958 }
1959}
1960
deadbeefab9b2d12015-10-14 11:33:11 -07001961void PeerConnection::UpdateLocalTracks(
1962 const std::vector<cricket::StreamParams>& streams,
1963 cricket::MediaType media_type) {
1964 TrackInfos* current_tracks = GetLocalTracks(media_type);
1965
1966 // Find removed tracks. I.e., tracks where the track id, stream label or ssrc
1967 // don't match the new StreamParam.
1968 TrackInfos::iterator track_it = current_tracks->begin();
1969 while (track_it != current_tracks->end()) {
1970 const TrackInfo& info = *track_it;
1971 const cricket::StreamParams* params =
1972 cricket::GetStreamBySsrc(streams, info.ssrc);
1973 if (!params || params->id != info.track_id ||
1974 params->sync_label != info.stream_label) {
1975 OnLocalTrackRemoved(info.stream_label, info.track_id, info.ssrc,
1976 media_type);
1977 track_it = current_tracks->erase(track_it);
1978 } else {
1979 ++track_it;
1980 }
1981 }
1982
1983 // Find new and active tracks.
1984 for (const cricket::StreamParams& params : streams) {
1985 // The sync_label is the MediaStream label and the |stream.id| is the
1986 // track id.
1987 const std::string& stream_label = params.sync_label;
1988 const std::string& track_id = params.id;
1989 uint32_t ssrc = params.first_ssrc();
1990 const TrackInfo* track_info =
1991 FindTrackInfo(*current_tracks, stream_label, track_id);
1992 if (!track_info) {
1993 current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc));
1994 OnLocalTrackSeen(stream_label, track_id, params.first_ssrc(), media_type);
1995 }
1996 }
1997}
1998
1999void PeerConnection::OnLocalTrackSeen(const std::string& stream_label,
2000 const std::string& track_id,
2001 uint32_t ssrc,
2002 cricket::MediaType media_type) {
deadbeefa601f5c2016-06-06 14:27:39 -07002003 RtpSenderInternal* sender = FindSenderById(track_id);
deadbeeffac06552015-11-25 11:26:01 -08002004 if (!sender) {
2005 LOG(LS_WARNING) << "An unknown RtpSender with id " << track_id
2006 << " has been configured in the local description.";
deadbeefab9b2d12015-10-14 11:33:11 -07002007 return;
2008 }
2009
deadbeeffac06552015-11-25 11:26:01 -08002010 if (sender->media_type() != media_type) {
2011 LOG(LS_WARNING) << "An RtpSender has been configured in the local"
2012 << " description with an unexpected media type.";
2013 return;
deadbeefab9b2d12015-10-14 11:33:11 -07002014 }
deadbeeffac06552015-11-25 11:26:01 -08002015
2016 sender->set_stream_id(stream_label);
2017 sender->SetSsrc(ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -07002018}
2019
2020void PeerConnection::OnLocalTrackRemoved(const std::string& stream_label,
2021 const std::string& track_id,
2022 uint32_t ssrc,
2023 cricket::MediaType media_type) {
deadbeefa601f5c2016-06-06 14:27:39 -07002024 RtpSenderInternal* sender = FindSenderById(track_id);
deadbeeffac06552015-11-25 11:26:01 -08002025 if (!sender) {
2026 // This is the normal case. I.e., RemoveStream has been called and the
deadbeefab9b2d12015-10-14 11:33:11 -07002027 // SessionDescriptions has been renegotiated.
2028 return;
2029 }
deadbeeffac06552015-11-25 11:26:01 -08002030
2031 // A sender has been removed from the SessionDescription but it's still
2032 // associated with the PeerConnection. This only occurs if the SDP doesn't
2033 // match with the calls to CreateSender, AddStream and RemoveStream.
2034 if (sender->media_type() != media_type) {
2035 LOG(LS_WARNING) << "An RtpSender has been configured in the local"
2036 << " description with an unexpected media type.";
2037 return;
deadbeefab9b2d12015-10-14 11:33:11 -07002038 }
deadbeeffac06552015-11-25 11:26:01 -08002039
2040 sender->SetSsrc(0);
deadbeefab9b2d12015-10-14 11:33:11 -07002041}
2042
2043void PeerConnection::UpdateLocalRtpDataChannels(
2044 const cricket::StreamParamsVec& streams) {
2045 std::vector<std::string> existing_channels;
2046
2047 // Find new and active data channels.
2048 for (const cricket::StreamParams& params : streams) {
2049 // |it->sync_label| is actually the data channel label. The reason is that
2050 // we use the same naming of data channels as we do for
2051 // MediaStreams and Tracks.
2052 // For MediaStreams, the sync_label is the MediaStream label and the
2053 // track label is the same as |streamid|.
2054 const std::string& channel_label = params.sync_label;
2055 auto data_channel_it = rtp_data_channels_.find(channel_label);
2056 if (!VERIFY(data_channel_it != rtp_data_channels_.end())) {
2057 continue;
2058 }
2059 // Set the SSRC the data channel should use for sending.
2060 data_channel_it->second->SetSendSsrc(params.first_ssrc());
2061 existing_channels.push_back(data_channel_it->first);
2062 }
2063
2064 UpdateClosingRtpDataChannels(existing_channels, true);
2065}
2066
2067void PeerConnection::UpdateRemoteRtpDataChannels(
2068 const cricket::StreamParamsVec& streams) {
2069 std::vector<std::string> existing_channels;
2070
2071 // Find new and active data channels.
2072 for (const cricket::StreamParams& params : streams) {
2073 // The data channel label is either the mslabel or the SSRC if the mslabel
2074 // does not exist. Ex a=ssrc:444330170 mslabel:test1.
2075 std::string label = params.sync_label.empty()
2076 ? rtc::ToString(params.first_ssrc())
2077 : params.sync_label;
2078 auto data_channel_it = rtp_data_channels_.find(label);
2079 if (data_channel_it == rtp_data_channels_.end()) {
2080 // This is a new data channel.
2081 CreateRemoteRtpDataChannel(label, params.first_ssrc());
2082 } else {
2083 data_channel_it->second->SetReceiveSsrc(params.first_ssrc());
2084 }
2085 existing_channels.push_back(label);
2086 }
2087
2088 UpdateClosingRtpDataChannels(existing_channels, false);
2089}
2090
2091void PeerConnection::UpdateClosingRtpDataChannels(
2092 const std::vector<std::string>& active_channels,
2093 bool is_local_update) {
2094 auto it = rtp_data_channels_.begin();
2095 while (it != rtp_data_channels_.end()) {
2096 DataChannel* data_channel = it->second;
2097 if (std::find(active_channels.begin(), active_channels.end(),
2098 data_channel->label()) != active_channels.end()) {
2099 ++it;
2100 continue;
2101 }
2102
2103 if (is_local_update) {
2104 data_channel->SetSendSsrc(0);
2105 } else {
2106 data_channel->RemotePeerRequestClose();
2107 }
2108
2109 if (data_channel->state() == DataChannel::kClosed) {
2110 rtp_data_channels_.erase(it);
2111 it = rtp_data_channels_.begin();
2112 } else {
2113 ++it;
2114 }
2115 }
2116}
2117
2118void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label,
2119 uint32_t remote_ssrc) {
2120 rtc::scoped_refptr<DataChannel> channel(
2121 InternalCreateDataChannel(label, nullptr));
2122 if (!channel.get()) {
2123 LOG(LS_WARNING) << "Remote peer requested a DataChannel but"
2124 << "CreateDataChannel failed.";
2125 return;
2126 }
2127 channel->SetReceiveSsrc(remote_ssrc);
deadbeefa601f5c2016-06-06 14:27:39 -07002128 rtc::scoped_refptr<DataChannelInterface> proxy_channel =
2129 DataChannelProxy::Create(signaling_thread(), channel);
Taylor Brandstetter98cde262016-05-31 13:02:21 -07002130 // Call both the raw pointer and scoped_refptr versions of the method
2131 // for compatibility.
2132 observer_->OnDataChannel(proxy_channel.get());
2133 observer_->OnDataChannel(std::move(proxy_channel));
deadbeefab9b2d12015-10-14 11:33:11 -07002134}
2135
2136rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel(
2137 const std::string& label,
2138 const InternalDataChannelInit* config) {
2139 if (IsClosed()) {
2140 return nullptr;
2141 }
2142 if (session_->data_channel_type() == cricket::DCT_NONE) {
2143 LOG(LS_ERROR)
2144 << "InternalCreateDataChannel: Data is not supported in this call.";
2145 return nullptr;
2146 }
2147 InternalDataChannelInit new_config =
2148 config ? (*config) : InternalDataChannelInit();
2149 if (session_->data_channel_type() == cricket::DCT_SCTP) {
2150 if (new_config.id < 0) {
2151 rtc::SSLRole role;
deadbeefc0dad892017-01-04 20:28:21 -08002152 if ((session_->GetSslRole(session_->data_channel(), &role)) &&
deadbeefab9b2d12015-10-14 11:33:11 -07002153 !sid_allocator_.AllocateSid(role, &new_config.id)) {
2154 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
2155 return nullptr;
2156 }
2157 } else if (!sid_allocator_.ReserveSid(new_config.id)) {
2158 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
2159 << "because the id is already in use or out of range.";
2160 return nullptr;
2161 }
2162 }
2163
2164 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
2165 session_.get(), session_->data_channel_type(), label, new_config));
2166 if (!channel) {
2167 sid_allocator_.ReleaseSid(new_config.id);
2168 return nullptr;
2169 }
2170
2171 if (channel->data_channel_type() == cricket::DCT_RTP) {
2172 if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) {
2173 LOG(LS_ERROR) << "DataChannel with label " << channel->label()
2174 << " already exists.";
2175 return nullptr;
2176 }
2177 rtp_data_channels_[channel->label()] = channel;
2178 } else {
2179 RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP);
2180 sctp_data_channels_.push_back(channel);
2181 channel->SignalClosed.connect(this,
2182 &PeerConnection::OnSctpDataChannelClosed);
2183 }
2184
hbos82ebe022016-11-14 01:41:09 -08002185 SignalDataChannelCreated(channel.get());
deadbeefab9b2d12015-10-14 11:33:11 -07002186 return channel;
2187}
2188
2189bool PeerConnection::HasDataChannels() const {
zhihuang9763d562016-08-05 11:14:50 -07002190#ifdef HAVE_QUIC
2191 return !rtp_data_channels_.empty() || !sctp_data_channels_.empty() ||
2192 (session_->quic_data_transport() &&
2193 session_->quic_data_transport()->HasDataChannels());
2194#else
deadbeefab9b2d12015-10-14 11:33:11 -07002195 return !rtp_data_channels_.empty() || !sctp_data_channels_.empty();
zhihuang9763d562016-08-05 11:14:50 -07002196#endif // HAVE_QUIC
deadbeefab9b2d12015-10-14 11:33:11 -07002197}
2198
2199void PeerConnection::AllocateSctpSids(rtc::SSLRole role) {
2200 for (const auto& channel : sctp_data_channels_) {
2201 if (channel->id() < 0) {
2202 int sid;
2203 if (!sid_allocator_.AllocateSid(role, &sid)) {
2204 LOG(LS_ERROR) << "Failed to allocate SCTP sid.";
2205 continue;
2206 }
2207 channel->SetSctpSid(sid);
2208 }
2209 }
2210}
2211
2212void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) {
deadbeefbd292462015-12-14 18:15:29 -08002213 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002214 for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end();
2215 ++it) {
2216 if (it->get() == channel) {
2217 if (channel->id() >= 0) {
2218 sid_allocator_.ReleaseSid(channel->id());
2219 }
deadbeefbd292462015-12-14 18:15:29 -08002220 // Since this method is triggered by a signal from the DataChannel,
2221 // we can't free it directly here; we need to free it asynchronously.
2222 sctp_data_channels_to_free_.push_back(*it);
deadbeefab9b2d12015-10-14 11:33:11 -07002223 sctp_data_channels_.erase(it);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002224 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS,
2225 nullptr);
deadbeefab9b2d12015-10-14 11:33:11 -07002226 return;
2227 }
2228 }
2229}
2230
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -07002231void PeerConnection::OnVoiceChannelCreated() {
2232 SetChannelOnSendersAndReceivers<AudioRtpSender, AudioRtpReceiver>(
2233 session_->voice_channel(), senders_, receivers_,
2234 cricket::MEDIA_TYPE_AUDIO);
2235}
2236
deadbeefab9b2d12015-10-14 11:33:11 -07002237void PeerConnection::OnVoiceChannelDestroyed() {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -07002238 SetChannelOnSendersAndReceivers<AudioRtpSender, AudioRtpReceiver,
2239 cricket::VoiceChannel>(
2240 nullptr, senders_, receivers_, cricket::MEDIA_TYPE_AUDIO);
2241}
2242
2243void PeerConnection::OnVideoChannelCreated() {
2244 SetChannelOnSendersAndReceivers<VideoRtpSender, VideoRtpReceiver>(
2245 session_->video_channel(), senders_, receivers_,
2246 cricket::MEDIA_TYPE_VIDEO);
deadbeefab9b2d12015-10-14 11:33:11 -07002247}
2248
2249void PeerConnection::OnVideoChannelDestroyed() {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -07002250 SetChannelOnSendersAndReceivers<VideoRtpSender, VideoRtpReceiver,
2251 cricket::VideoChannel>(
2252 nullptr, senders_, receivers_, cricket::MEDIA_TYPE_VIDEO);
deadbeefab9b2d12015-10-14 11:33:11 -07002253}
2254
2255void PeerConnection::OnDataChannelCreated() {
2256 for (const auto& channel : sctp_data_channels_) {
2257 channel->OnTransportChannelCreated();
2258 }
2259}
2260
2261void PeerConnection::OnDataChannelDestroyed() {
2262 // Use a temporary copy of the RTP/SCTP DataChannel list because the
2263 // DataChannel may callback to us and try to modify the list.
2264 std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs;
2265 temp_rtp_dcs.swap(rtp_data_channels_);
2266 for (const auto& kv : temp_rtp_dcs) {
2267 kv.second->OnTransportChannelDestroyed();
2268 }
2269
2270 std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs;
2271 temp_sctp_dcs.swap(sctp_data_channels_);
2272 for (const auto& channel : temp_sctp_dcs) {
2273 channel->OnTransportChannelDestroyed();
2274 }
2275}
2276
2277void PeerConnection::OnDataChannelOpenMessage(
2278 const std::string& label,
2279 const InternalDataChannelInit& config) {
2280 rtc::scoped_refptr<DataChannel> channel(
2281 InternalCreateDataChannel(label, &config));
2282 if (!channel.get()) {
2283 LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message.";
2284 return;
2285 }
2286
deadbeefa601f5c2016-06-06 14:27:39 -07002287 rtc::scoped_refptr<DataChannelInterface> proxy_channel =
2288 DataChannelProxy::Create(signaling_thread(), channel);
Taylor Brandstetter98cde262016-05-31 13:02:21 -07002289 // Call both the raw pointer and scoped_refptr versions of the method
2290 // for compatibility.
2291 observer_->OnDataChannel(proxy_channel.get());
2292 observer_->OnDataChannel(std::move(proxy_channel));
deadbeefab9b2d12015-10-14 11:33:11 -07002293}
2294
deadbeefa601f5c2016-06-06 14:27:39 -07002295RtpSenderInternal* PeerConnection::FindSenderById(const std::string& id) {
2296 auto it = std::find_if(
2297 senders_.begin(), senders_.end(),
2298 [id](const rtc::scoped_refptr<
2299 RtpSenderProxyWithInternal<RtpSenderInternal>>& sender) {
2300 return sender->id() == id;
2301 });
2302 return it != senders_.end() ? (*it)->internal() : nullptr;
deadbeeffac06552015-11-25 11:26:01 -08002303}
2304
deadbeefa601f5c2016-06-06 14:27:39 -07002305std::vector<
2306 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>::iterator
deadbeef70ab1a12015-09-28 16:53:55 -07002307PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) {
2308 return std::find_if(
2309 senders_.begin(), senders_.end(),
deadbeefa601f5c2016-06-06 14:27:39 -07002310 [track](const rtc::scoped_refptr<
2311 RtpSenderProxyWithInternal<RtpSenderInternal>>& sender) {
deadbeef70ab1a12015-09-28 16:53:55 -07002312 return sender->track() == track;
2313 });
2314}
2315
deadbeefa601f5c2016-06-06 14:27:39 -07002316std::vector<rtc::scoped_refptr<
2317 RtpReceiverProxyWithInternal<RtpReceiverInternal>>>::iterator
perkjd61bf802016-03-24 03:16:19 -07002318PeerConnection::FindReceiverForTrack(const std::string& track_id) {
deadbeef70ab1a12015-09-28 16:53:55 -07002319 return std::find_if(
2320 receivers_.begin(), receivers_.end(),
deadbeefa601f5c2016-06-06 14:27:39 -07002321 [track_id](const rtc::scoped_refptr<
2322 RtpReceiverProxyWithInternal<RtpReceiverInternal>>& receiver) {
perkjd61bf802016-03-24 03:16:19 -07002323 return receiver->id() == track_id;
deadbeef70ab1a12015-09-28 16:53:55 -07002324 });
2325}
2326
deadbeefab9b2d12015-10-14 11:33:11 -07002327PeerConnection::TrackInfos* PeerConnection::GetRemoteTracks(
2328 cricket::MediaType media_type) {
2329 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
2330 media_type == cricket::MEDIA_TYPE_VIDEO);
2331 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &remote_audio_tracks_
2332 : &remote_video_tracks_;
2333}
2334
2335PeerConnection::TrackInfos* PeerConnection::GetLocalTracks(
2336 cricket::MediaType media_type) {
2337 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
2338 media_type == cricket::MEDIA_TYPE_VIDEO);
2339 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_tracks_
2340 : &local_video_tracks_;
2341}
2342
2343const PeerConnection::TrackInfo* PeerConnection::FindTrackInfo(
2344 const PeerConnection::TrackInfos& infos,
2345 const std::string& stream_label,
2346 const std::string track_id) const {
2347 for (const TrackInfo& track_info : infos) {
2348 if (track_info.stream_label == stream_label &&
2349 track_info.track_id == track_id) {
2350 return &track_info;
2351 }
2352 }
2353 return nullptr;
2354}
2355
2356DataChannel* PeerConnection::FindDataChannelBySid(int sid) const {
2357 for (const auto& channel : sctp_data_channels_) {
2358 if (channel->id() == sid) {
2359 return channel;
2360 }
2361 }
2362 return nullptr;
2363}
2364
deadbeef91dd5672016-05-18 16:55:30 -07002365bool PeerConnection::InitializePortAllocator_n(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002366 const RTCConfiguration& configuration) {
2367 cricket::ServerAddresses stun_servers;
2368 std::vector<cricket::RelayServerConfig> turn_servers;
deadbeef1e234612016-12-24 01:43:32 -08002369 if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002370 return false;
2371 }
2372
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07002373 port_allocator_->Initialize();
2374
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002375 // To handle both internal and externally created port allocator, we will
2376 // enable BUNDLE here.
2377 int portallocator_flags = port_allocator_->flags();
2378 portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2379 cricket::PORTALLOCATOR_ENABLE_IPV6;
2380 // If the disable-IPv6 flag was specified, we'll not override it
2381 // by experiment.
2382 if (configuration.disable_ipv6) {
2383 portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
2384 } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") ==
2385 "Disabled") {
2386 portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
2387 }
2388
2389 if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
2390 portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
2391 LOG(LS_INFO) << "TCP candidates are disabled.";
2392 }
2393
honghaiz60347052016-05-31 18:29:12 -07002394 if (configuration.candidate_network_policy ==
2395 kCandidateNetworkPolicyLowCost) {
2396 portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS;
2397 LOG(LS_INFO) << "Do not gather candidates on high-cost networks";
2398 }
2399
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002400 port_allocator_->set_flags(portallocator_flags);
2401 // No step delay is used while allocating ports.
2402 port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
2403 port_allocator_->set_candidate_filter(
2404 ConvertIceTransportTypeToCandidateFilter(configuration.type));
2405
2406 // Call this last since it may create pooled allocator sessions using the
2407 // properties set above.
2408 port_allocator_->SetConfiguration(stun_servers, turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002409 configuration.ice_candidate_pool_size,
2410 configuration.prune_turn_ports);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002411 return true;
2412}
2413
deadbeef91dd5672016-05-18 16:55:30 -07002414bool PeerConnection::ReconfigurePortAllocator_n(
deadbeef1e234612016-12-24 01:43:32 -08002415 const RTCConfiguration& configuration) {
2416 cricket::ServerAddresses stun_servers;
2417 std::vector<cricket::RelayServerConfig> turn_servers;
2418 if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
2419 return false;
2420 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002421 port_allocator_->set_candidate_filter(
deadbeef1e234612016-12-24 01:43:32 -08002422 ConvertIceTransportTypeToCandidateFilter(configuration.type));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002423 // Call this last since it may create pooled allocator sessions using the
2424 // candidate filter set above.
deadbeef6de92f92016-12-12 18:49:32 -08002425 return port_allocator_->SetConfiguration(
deadbeef1e234612016-12-24 01:43:32 -08002426 stun_servers, turn_servers, configuration.ice_candidate_pool_size,
2427 configuration.prune_turn_ports);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002428}
2429
ivoc14d5dbe2016-07-04 07:06:55 -07002430bool PeerConnection::StartRtcEventLog_w(rtc::PlatformFile file,
2431 int64_t max_size_bytes) {
skvlad11a9cbf2016-10-07 11:53:05 -07002432 return event_log_->StartLogging(file, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -07002433}
2434
2435void PeerConnection::StopRtcEventLog_w() {
skvlad11a9cbf2016-10-07 11:53:05 -07002436 event_log_->StopLogging();
ivoc14d5dbe2016-07-04 07:06:55 -07002437}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002438} // namespace webrtc