blob: 54e76d46c95bf20329ee93f6e903aa77fafa8ce7 [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"
28#include "webrtc/api/remotevideocapturer.h"
29#include "webrtc/api/rtpreceiver.h"
30#include "webrtc/api/rtpsender.h"
31#include "webrtc/api/streamcollection.h"
perkja3ede6c2016-03-08 01:27:48 +010032#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010033#include "webrtc/api/videotrack.h"
tfarina5237aaf2015-11-10 23:44:30 -080034#include "webrtc/base/arraysize.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"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/media/sctp/sctpdataengine.h"
tfarina5237aaf2015-11-10 23:44:30 -080040#include "webrtc/p2p/client/basicportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010041#include "webrtc/pc/channelmanager.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010042#include "webrtc/system_wrappers/include/field_trial.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44namespace {
45
deadbeefab9b2d12015-10-14 11:33:11 -070046using webrtc::DataChannel;
47using webrtc::MediaConstraintsInterface;
48using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049using webrtc::PeerConnectionInterface;
deadbeeffac06552015-11-25 11:26:01 -080050using webrtc::RtpSenderInterface;
deadbeefab9b2d12015-10-14 11:33:11 -070051using webrtc::StreamCollection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
deadbeefab9b2d12015-10-14 11:33:11 -070053static const char kDefaultStreamLabel[] = "default";
54static const char kDefaultAudioTrackLabel[] = "defaulta0";
55static const char kDefaultVideoTrackLabel[] = "defaultv0";
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057// The min number of tokens must present in Turn host uri.
58// e.g. user@turn.example.org
59static const size_t kTurnHostTokensNum = 2;
60// Number of tokens must be preset when TURN uri has transport param.
61static const size_t kTurnTransportTokensNum = 2;
62// The default stun port.
wu@webrtc.org91053e72013-08-10 07:18:04 +000063static const int kDefaultStunPort = 3478;
64static const int kDefaultStunTlsPort = 5349;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065static const char kTransport[] = "transport";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
67// NOTE: Must be in the same order as the ServiceType enum.
deadbeef0a6c4ca2015-10-06 11:38:28 -070068static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
deadbeef0a6c4ca2015-10-06 11:38:28 -070070// NOTE: A loop below assumes that the first value of this enum is 0 and all
71// other values are incremental.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072enum ServiceType {
deadbeef0a6c4ca2015-10-06 11:38:28 -070073 STUN = 0, // Indicates a STUN server.
74 STUNS, // Indicates a STUN server used with a TLS session.
75 TURN, // Indicates a TURN server
76 TURNS, // Indicates a TURN server used with a TLS session.
77 INVALID, // Unknown.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078};
tfarina5237aaf2015-11-10 23:44:30 -080079static_assert(INVALID == arraysize(kValidIceServiceTypes),
deadbeef0a6c4ca2015-10-06 11:38:28 -070080 "kValidIceServiceTypes must have as many strings as ServiceType "
81 "has values.");
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
83enum {
wu@webrtc.org91053e72013-08-10 07:18:04 +000084 MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 MSG_SET_SESSIONDESCRIPTION_FAILED,
deadbeefab9b2d12015-10-14 11:33:11 -070086 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 MSG_GETSTATS,
deadbeefbd292462015-12-14 18:15:29 -080088 MSG_FREE_DATACHANNELS,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089};
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091struct SetSessionDescriptionMsg : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 explicit SetSessionDescriptionMsg(
93 webrtc::SetSessionDescriptionObserver* observer)
94 : observer(observer) {
95 }
96
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097 rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 std::string error;
99};
100
deadbeefab9b2d12015-10-14 11:33:11 -0700101struct CreateSessionDescriptionMsg : public rtc::MessageData {
102 explicit CreateSessionDescriptionMsg(
103 webrtc::CreateSessionDescriptionObserver* observer)
104 : observer(observer) {}
105
106 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
107 std::string error;
108};
109
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000110struct GetStatsMsg : public rtc::MessageData {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000111 GetStatsMsg(webrtc::StatsObserver* observer,
112 webrtc::MediaStreamTrackInterface* track)
113 : observer(observer), track(track) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000115 rtc::scoped_refptr<webrtc::StatsObserver> observer;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000116 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117};
118
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000119// |in_str| should be of format
120// stunURI = scheme ":" stun-host [ ":" stun-port ]
121// scheme = "stun" / "stuns"
122// stun-host = IP-literal / IPv4address / reg-name
123// stun-port = *DIGIT
deadbeef0a6c4ca2015-10-06 11:38:28 -0700124//
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000125// draft-petithuguenin-behave-turn-uris-01
126// turnURI = scheme ":" turn-host [ ":" turn-port ]
127// turn-host = username@IP-literal / IPv4address / reg-name
128bool GetServiceTypeAndHostnameFromUri(const std::string& in_str,
129 ServiceType* service_type,
130 std::string* hostname) {
Tommi77d444a2015-04-24 15:38:38 +0200131 const std::string::size_type colonpos = in_str.find(':');
deadbeef0a6c4ca2015-10-06 11:38:28 -0700132 if (colonpos == std::string::npos) {
133 LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000134 return false;
135 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700136 if ((colonpos + 1) == in_str.length()) {
137 LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str;
138 return false;
139 }
140 *service_type = INVALID;
tfarina5237aaf2015-11-10 23:44:30 -0800141 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700142 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000143 *service_type = static_cast<ServiceType>(i);
144 break;
145 }
146 }
147 if (*service_type == INVALID) {
148 return false;
149 }
150 *hostname = in_str.substr(colonpos + 1, std::string::npos);
151 return true;
152}
153
deadbeef0a6c4ca2015-10-06 11:38:28 -0700154bool ParsePort(const std::string& in_str, int* port) {
155 // Make sure port only contains digits. FromString doesn't check this.
156 for (const char& c : in_str) {
157 if (!std::isdigit(c)) {
158 return false;
159 }
160 }
161 return rtc::FromString(in_str, port);
162}
163
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000164// This method parses IPv6 and IPv4 literal strings, along with hostnames in
165// standard hostname:port format.
166// Consider following formats as correct.
167// |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port,
deadbeef0a6c4ca2015-10-06 11:38:28 -0700168// |hostname|, |[IPv6 address]|, |IPv4 address|.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000169bool ParseHostnameAndPortFromString(const std::string& in_str,
170 std::string* host,
171 int* port) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700172 RTC_DCHECK(host->empty());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000173 if (in_str.at(0) == '[') {
174 std::string::size_type closebracket = in_str.rfind(']');
175 if (closebracket != std::string::npos) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000176 std::string::size_type colonpos = in_str.find(':', closebracket);
177 if (std::string::npos != colonpos) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700178 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos),
179 port)) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000180 return false;
181 }
182 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700183 *host = in_str.substr(1, closebracket - 1);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000184 } else {
185 return false;
186 }
187 } else {
188 std::string::size_type colonpos = in_str.find(':');
189 if (std::string::npos != colonpos) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700190 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000191 return false;
192 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700193 *host = in_str.substr(0, colonpos);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000194 } else {
195 *host = in_str;
196 }
197 }
deadbeef0a6c4ca2015-10-06 11:38:28 -0700198 return !host->empty();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000199}
200
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800201// Adds a STUN or TURN server to the appropriate list,
deadbeef0a6c4ca2015-10-06 11:38:28 -0700202// by parsing |url| and using the username/password in |server|.
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200203bool ParseIceServerUrl(const PeerConnectionInterface::IceServer& server,
204 const std::string& url,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800205 cricket::ServerAddresses* stun_servers,
206 std::vector<cricket::RelayServerConfig>* turn_servers) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 // draft-nandakumar-rtcweb-stun-uri-01
208 // stunURI = scheme ":" stun-host [ ":" stun-port ]
209 // scheme = "stun" / "stuns"
210 // stun-host = IP-literal / IPv4address / reg-name
211 // stun-port = *DIGIT
212
213 // draft-petithuguenin-behave-turn-uris-01
214 // turnURI = scheme ":" turn-host [ ":" turn-port ]
215 // [ "?transport=" transport ]
216 // scheme = "turn" / "turns"
217 // transport = "udp" / "tcp" / transport-ext
218 // transport-ext = 1*unreserved
219 // turn-host = IP-literal / IPv4address / reg-name
220 // turn-port = *DIGIT
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800221 RTC_DCHECK(stun_servers != nullptr);
222 RTC_DCHECK(turn_servers != nullptr);
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200223 std::vector<std::string> tokens;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800224 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700225 RTC_DCHECK(!url.empty());
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200226 rtc::tokenize(url, '?', &tokens);
227 std::string uri_without_transport = tokens[0];
228 // Let's look into transport= param, if it exists.
229 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present.
230 std::string uri_transport_param = tokens[1];
231 rtc::tokenize(uri_transport_param, '=', &tokens);
232 if (tokens[0] == kTransport) {
233 // As per above grammar transport param will be consist of lower case
234 // letters.
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800235 if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) ||
236 (turn_transport_type != cricket::PROTO_UDP &&
237 turn_transport_type != cricket::PROTO_TCP)) {
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200238 LOG(LS_WARNING) << "Transport param should always be udp or tcp.";
deadbeef0a6c4ca2015-10-06 11:38:28 -0700239 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 }
241 }
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200244 std::string hoststring;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700245 ServiceType service_type;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200246 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport,
247 &service_type,
248 &hoststring)) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700249 LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
250 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200251 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000252
deadbeef0a6c4ca2015-10-06 11:38:28 -0700253 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
254 RTC_DCHECK(!hoststring.empty());
Tommi77d444a2015-04-24 15:38:38 +0200255
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200256 // Let's break hostname.
257 tokens.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -0700258 rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens);
259
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200260 std::string username(server.username);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700261 if (tokens.size() > kTurnHostTokensNum) {
262 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
263 return false;
264 }
265 if (tokens.size() == kTurnHostTokensNum) {
266 if (tokens[0].empty() || tokens[1].empty()) {
267 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
268 return false;
269 }
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200270 username.assign(rtc::s_url_decode(tokens[0]));
271 hoststring = tokens[1];
272 } else {
273 hoststring = tokens[0];
274 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000275
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200276 int port = kDefaultStunPort;
277 if (service_type == TURNS) {
278 port = kDefaultStunTlsPort;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800279 turn_transport_type = cricket::PROTO_TCP;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200280 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000281
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200282 std::string address;
283 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700284 LOG(WARNING) << "Invalid hostname format: " << uri_without_transport;
285 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200286 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000287
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200288 if (port <= 0 || port > 0xffff) {
289 LOG(WARNING) << "Invalid port: " << port;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700290 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200291 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200293 switch (service_type) {
294 case STUN:
295 case STUNS:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800296 stun_servers->insert(rtc::SocketAddress(address, port));
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200297 break;
298 case TURN:
299 case TURNS: {
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200300 bool secure = (service_type == TURNS);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800301 turn_servers->push_back(
302 cricket::RelayServerConfig(address, port, username, server.password,
303 turn_transport_type, secure));
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200304 break;
305 }
306 case INVALID:
307 default:
308 LOG(WARNING) << "Configuration not supported: " << url;
309 return false;
310 }
311 return true;
312}
313
deadbeefab9b2d12015-10-14 11:33:11 -0700314// Check if we can send |new_stream| on a PeerConnection.
315bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
316 webrtc::MediaStreamInterface* new_stream) {
317 if (!new_stream || !current_streams) {
318 return false;
319 }
320 if (current_streams->find(new_stream->label()) != nullptr) {
321 LOG(LS_ERROR) << "MediaStream with label " << new_stream->label()
322 << " is already added.";
323 return false;
324 }
325 return true;
326}
327
328bool MediaContentDirectionHasSend(cricket::MediaContentDirection dir) {
329 return dir == cricket::MD_SENDONLY || dir == cricket::MD_SENDRECV;
330}
331
deadbeef5e97fb52015-10-15 12:49:08 -0700332// If the direction is "recvonly" or "inactive", treat the description
333// as containing no streams.
334// See: https://code.google.com/p/webrtc/issues/detail?id=5054
335std::vector<cricket::StreamParams> GetActiveStreams(
336 const cricket::MediaContentDescription* desc) {
337 return MediaContentDirectionHasSend(desc->direction())
338 ? desc->streams()
339 : std::vector<cricket::StreamParams>();
340}
341
deadbeefab9b2d12015-10-14 11:33:11 -0700342bool IsValidOfferToReceiveMedia(int value) {
343 typedef PeerConnectionInterface::RTCOfferAnswerOptions Options;
344 return (value >= Options::kUndefined) &&
345 (value <= Options::kMaxOfferToReceiveMedia);
346}
347
348// Add the stream and RTP data channel info to |session_options|.
deadbeeffac06552015-11-25 11:26:01 -0800349void AddSendStreams(
350 cricket::MediaSessionOptions* session_options,
351 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
352 const std::map<std::string, rtc::scoped_refptr<DataChannel>>&
353 rtp_data_channels) {
deadbeefab9b2d12015-10-14 11:33:11 -0700354 session_options->streams.clear();
deadbeeffac06552015-11-25 11:26:01 -0800355 for (const auto& sender : senders) {
356 session_options->AddSendStream(sender->media_type(), sender->id(),
357 sender->stream_id());
deadbeefab9b2d12015-10-14 11:33:11 -0700358 }
359
360 // Check for data channels.
361 for (const auto& kv : rtp_data_channels) {
362 const DataChannel* channel = kv.second;
363 if (channel->state() == DataChannel::kConnecting ||
364 channel->state() == DataChannel::kOpen) {
365 // |streamid| and |sync_label| are both set to the DataChannel label
366 // here so they can be signaled the same way as MediaStreams and Tracks.
367 // For MediaStreams, the sync_label is the MediaStream label and the
368 // track label is the same as |streamid|.
369 const std::string& streamid = channel->label();
370 const std::string& sync_label = channel->label();
371 session_options->AddSendStream(cricket::MEDIA_TYPE_DATA, streamid,
372 sync_label);
373 }
374 }
375}
376
deadbeef0a6c4ca2015-10-06 11:38:28 -0700377} // namespace
378
379namespace webrtc {
380
deadbeefab9b2d12015-10-14 11:33:11 -0700381// Factory class for creating remote MediaStreams and MediaStreamTracks.
382class RemoteMediaStreamFactory {
383 public:
perkjf0dcfe22016-03-10 18:32:00 +0100384 explicit RemoteMediaStreamFactory(rtc::Thread* signaling_thread)
385 : signaling_thread_(signaling_thread) {}
deadbeefab9b2d12015-10-14 11:33:11 -0700386
387 rtc::scoped_refptr<MediaStreamInterface> CreateMediaStream(
388 const std::string& stream_label) {
389 return MediaStreamProxy::Create(signaling_thread_,
390 MediaStream::Create(stream_label));
391 }
392
Tommif888bb52015-12-12 01:37:01 +0100393 AudioTrackInterface* AddAudioTrack(uint32_t ssrc,
394 AudioProviderInterface* provider,
395 webrtc::MediaStreamInterface* stream,
deadbeefab9b2d12015-10-14 11:33:11 -0700396 const std::string& track_id) {
tommi6eca7e32015-12-15 04:27:11 -0800397 return AddTrack<AudioTrackInterface, AudioTrack, AudioTrackProxy>(
Tommif888bb52015-12-12 01:37:01 +0100398 stream, track_id, RemoteAudioSource::Create(ssrc, provider));
deadbeefab9b2d12015-10-14 11:33:11 -0700399 }
400
deadbeefab9b2d12015-10-14 11:33:11 -0700401 private:
402 template <typename TI, typename T, typename TP, typename S>
403 TI* AddTrack(MediaStreamInterface* stream,
404 const std::string& track_id,
Tommif888bb52015-12-12 01:37:01 +0100405 const S& source) {
deadbeefab9b2d12015-10-14 11:33:11 -0700406 rtc::scoped_refptr<TI> track(
407 TP::Create(signaling_thread_, T::Create(track_id, source)));
408 track->set_state(webrtc::MediaStreamTrackInterface::kLive);
409 if (stream->AddTrack(track)) {
410 return track;
411 }
412 return nullptr;
413 }
414
415 rtc::Thread* signaling_thread_;
deadbeefab9b2d12015-10-14 11:33:11 -0700416};
417
htaa2a49d92016-03-04 02:51:39 -0800418bool ExtractMediaSessionOptions(
deadbeefab9b2d12015-10-14 11:33:11 -0700419 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
htaaac2dea2016-03-10 13:35:55 -0800420 bool is_offer,
deadbeefab9b2d12015-10-14 11:33:11 -0700421 cricket::MediaSessionOptions* session_options) {
422 typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
423 if (!IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) ||
424 !IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video)) {
425 return false;
426 }
427
htaaac2dea2016-03-10 13:35:55 -0800428 // If constraints don't prevent us, we always accept video.
deadbeefc80741f2015-10-22 13:14:45 -0700429 if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) {
deadbeefab9b2d12015-10-14 11:33:11 -0700430 session_options->recv_audio = (rtc_options.offer_to_receive_audio > 0);
htaaac2dea2016-03-10 13:35:55 -0800431 } else {
432 session_options->recv_audio = true;
deadbeefab9b2d12015-10-14 11:33:11 -0700433 }
htaaac2dea2016-03-10 13:35:55 -0800434 // For offers, we only offer video if we have it or it's forced by options.
435 // For answers, we will always accept video (if offered).
deadbeefc80741f2015-10-22 13:14:45 -0700436 if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) {
deadbeefab9b2d12015-10-14 11:33:11 -0700437 session_options->recv_video = (rtc_options.offer_to_receive_video > 0);
htaaac2dea2016-03-10 13:35:55 -0800438 } else if (is_offer) {
439 session_options->recv_video = false;
440 } else {
441 session_options->recv_video = true;
deadbeefab9b2d12015-10-14 11:33:11 -0700442 }
443
444 session_options->vad_enabled = rtc_options.voice_activity_detection;
deadbeefc80741f2015-10-22 13:14:45 -0700445 session_options->bundle_enabled = rtc_options.use_rtp_mux;
deadbeef0ed85b22016-02-23 17:24:52 -0800446 for (auto& kv : session_options->transport_options) {
447 kv.second.ice_restart = rtc_options.ice_restart;
448 }
deadbeefab9b2d12015-10-14 11:33:11 -0700449
450 return true;
451}
452
453bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints,
454 cricket::MediaSessionOptions* session_options) {
455 bool value = false;
456 size_t mandatory_constraints_satisfied = 0;
457
458 // kOfferToReceiveAudio defaults to true according to spec.
459 if (!FindConstraint(constraints,
460 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
461 &mandatory_constraints_satisfied) ||
462 value) {
463 session_options->recv_audio = true;
464 }
465
466 // kOfferToReceiveVideo defaults to false according to spec. But
467 // if it is an answer and video is offered, we should still accept video
468 // per default.
469 value = false;
470 if (!FindConstraint(constraints,
471 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
472 &mandatory_constraints_satisfied) ||
473 value) {
474 session_options->recv_video = true;
475 }
476
477 if (FindConstraint(constraints,
478 MediaConstraintsInterface::kVoiceActivityDetection, &value,
479 &mandatory_constraints_satisfied)) {
480 session_options->vad_enabled = value;
481 }
482
483 if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value,
484 &mandatory_constraints_satisfied)) {
485 session_options->bundle_enabled = value;
486 } else {
487 // kUseRtpMux defaults to true according to spec.
488 session_options->bundle_enabled = true;
489 }
deadbeefab9b2d12015-10-14 11:33:11 -0700490
deadbeef0ed85b22016-02-23 17:24:52 -0800491 bool ice_restart = false;
deadbeefab9b2d12015-10-14 11:33:11 -0700492 if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart,
493 &value, &mandatory_constraints_satisfied)) {
deadbeefab9b2d12015-10-14 11:33:11 -0700494 // kIceRestart defaults to false according to spec.
deadbeef0ed85b22016-02-23 17:24:52 -0800495 ice_restart = true;
496 }
497 for (auto& kv : session_options->transport_options) {
498 kv.second.ice_restart = ice_restart;
deadbeefab9b2d12015-10-14 11:33:11 -0700499 }
500
501 if (!constraints) {
502 return true;
503 }
504 return mandatory_constraints_satisfied == constraints->GetMandatory().size();
505}
506
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200507bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800508 cricket::ServerAddresses* stun_servers,
509 std::vector<cricket::RelayServerConfig>* turn_servers) {
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200510 for (const webrtc::PeerConnectionInterface::IceServer& server : servers) {
511 if (!server.urls.empty()) {
512 for (const std::string& url : server.urls) {
Joachim Bauchd935f912015-05-29 22:14:21 +0200513 if (url.empty()) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700514 LOG(LS_ERROR) << "Empty uri.";
515 return false;
Joachim Bauchd935f912015-05-29 22:14:21 +0200516 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800517 if (!ParseIceServerUrl(server, url, stun_servers, turn_servers)) {
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200518 return false;
519 }
520 }
521 } else if (!server.uri.empty()) {
522 // Fallback to old .uri if new .urls isn't present.
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800523 if (!ParseIceServerUrl(server, server.uri, stun_servers, turn_servers)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200525 }
526 } else {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700527 LOG(LS_ERROR) << "Empty uri.";
528 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 }
530 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800531 // Candidates must have unique priorities, so that connectivity checks
532 // are performed in a well-defined order.
533 int priority = static_cast<int>(turn_servers->size() - 1);
534 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
535 // First in the list gets highest priority.
536 turn_server.priority = priority--;
537 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 return true;
539}
540
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541PeerConnection::PeerConnection(PeerConnectionFactory* factory)
542 : factory_(factory),
543 observer_(NULL),
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000544 uma_observer_(NULL),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 signaling_state_(kStable),
546 ice_state_(kIceNew),
547 ice_connection_state_(kIceConnectionNew),
deadbeefab9b2d12015-10-14 11:33:11 -0700548 ice_gathering_state_(kIceGatheringNew),
549 local_streams_(StreamCollection::Create()),
550 remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551
552PeerConnection::~PeerConnection() {
Peter Boström1a9d6152015-12-08 22:15:17 +0100553 TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection");
deadbeef0a6c4ca2015-10-06 11:38:28 -0700554 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef70ab1a12015-09-28 16:53:55 -0700555 // Need to detach RTP senders/receivers from WebRtcSession,
556 // since it's about to be destroyed.
557 for (const auto& sender : senders_) {
558 sender->Stop();
559 }
560 for (const auto& receiver : receivers_) {
561 receiver->Stop();
562 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563}
564
565bool PeerConnection::Initialize(
htaa2a49d92016-03-04 02:51:39 -0800566 const cricket::MediaConfig& media_config,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000567 const PeerConnectionInterface::RTCConfiguration& configuration,
deadbeef653b8e02015-11-11 12:55:10 -0800568 rtc::scoped_ptr<cricket::PortAllocator> allocator,
569 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
570 PeerConnectionObserver* observer) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100571 TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
deadbeef653b8e02015-11-11 12:55:10 -0800572 RTC_DCHECK(observer != nullptr);
573 if (!observer) {
574 return false;
575 }
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000576 observer_ = observer;
577
kwiberg0eb15ed2015-12-17 03:04:15 -0800578 port_allocator_ = std::move(allocator);
deadbeef653b8e02015-11-11 12:55:10 -0800579
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800580 cricket::ServerAddresses stun_servers;
581 std::vector<cricket::RelayServerConfig> turn_servers;
582 if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 return false;
584 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800585 port_allocator_->SetIceServers(stun_servers, turn_servers);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000586
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 // To handle both internal and externally created port allocator, we will
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000588 // enable BUNDLE here.
braveyao@webrtc.org1732df62014-10-27 03:01:37 +0000589 int portallocator_flags = port_allocator_->flags();
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700590 portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000591 cricket::PORTALLOCATOR_ENABLE_IPV6;
htaa2a49d92016-03-04 02:51:39 -0800592 // If the disable-IPv6 flag was specified, we'll not override it
593 // by experiment.
594 if (configuration.disable_ipv6) {
595 portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
guoweis@webrtc.org2c1bcea2014-09-23 16:23:02 +0000596 } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") ==
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000597 "Disabled") {
598 portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000599 }
600
Jiayang Liucac1b382015-04-30 12:35:24 -0700601 if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
602 portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
603 LOG(LS_INFO) << "TCP candidates are disabled.";
604 }
605
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000606 port_allocator_->set_flags(portallocator_flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 // No step delay is used while allocating ports.
608 port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
609
nisse51542be2016-02-12 02:27:06 -0800610 media_controller_.reset(factory_->CreateMediaController(media_config));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611
perkjf0dcfe22016-03-10 18:32:00 +0100612 remote_stream_factory_.reset(
613 new RemoteMediaStreamFactory(factory_->signaling_thread()));
stefanc1aeaf02015-10-15 07:26:07 -0700614
615 session_.reset(
616 new WebRtcSession(media_controller_.get(), factory_->signaling_thread(),
617 factory_->worker_thread(), port_allocator_.get()));
deadbeefab9b2d12015-10-14 11:33:11 -0700618 stats_.reset(new StatsCollector(this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619
620 // Initialize the WebRtcSession. It creates transport channels etc.
htaa2a49d92016-03-04 02:51:39 -0800621 if (!session_->Initialize(factory_->options(), std::move(dtls_identity_store),
622 configuration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 return false;
deadbeefab9b2d12015-10-14 11:33:11 -0700624 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // Register PeerConnection as receiver of local ice candidates.
627 // All the callbacks will be posted to the application from PeerConnection.
628 session_->RegisterIceObserver(this);
629 session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange);
deadbeefab9b2d12015-10-14 11:33:11 -0700630 session_->SignalVoiceChannelDestroyed.connect(
631 this, &PeerConnection::OnVoiceChannelDestroyed);
632 session_->SignalVideoChannelDestroyed.connect(
633 this, &PeerConnection::OnVideoChannelDestroyed);
634 session_->SignalDataChannelCreated.connect(
635 this, &PeerConnection::OnDataChannelCreated);
636 session_->SignalDataChannelDestroyed.connect(
637 this, &PeerConnection::OnDataChannelDestroyed);
638 session_->SignalDataChannelOpenMessage.connect(
639 this, &PeerConnection::OnDataChannelOpenMessage);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 return true;
641}
642
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000643rtc::scoped_refptr<StreamCollectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644PeerConnection::local_streams() {
deadbeefab9b2d12015-10-14 11:33:11 -0700645 return local_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646}
647
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000648rtc::scoped_refptr<StreamCollectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649PeerConnection::remote_streams() {
deadbeefab9b2d12015-10-14 11:33:11 -0700650 return remote_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651}
652
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000653bool PeerConnection::AddStream(MediaStreamInterface* local_stream) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100654 TRACE_EVENT0("webrtc", "PeerConnection::AddStream");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 if (IsClosed()) {
656 return false;
657 }
deadbeefab9b2d12015-10-14 11:33:11 -0700658 if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 return false;
660 }
deadbeefab9b2d12015-10-14 11:33:11 -0700661
662 local_streams_->AddStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -0800663 MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
664 observer->SignalAudioTrackAdded.connect(this,
665 &PeerConnection::OnAudioTrackAdded);
666 observer->SignalAudioTrackRemoved.connect(
667 this, &PeerConnection::OnAudioTrackRemoved);
668 observer->SignalVideoTrackAdded.connect(this,
669 &PeerConnection::OnVideoTrackAdded);
670 observer->SignalVideoTrackRemoved.connect(
671 this, &PeerConnection::OnVideoTrackRemoved);
672 stream_observers_.push_back(rtc::scoped_ptr<MediaStreamObserver>(observer));
deadbeefab9b2d12015-10-14 11:33:11 -0700673
deadbeefab9b2d12015-10-14 11:33:11 -0700674 for (const auto& track : local_stream->GetAudioTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800675 OnAudioTrackAdded(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700676 }
677 for (const auto& track : local_stream->GetVideoTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800678 OnVideoTrackAdded(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700679 }
680
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000681 stats_->AddStream(local_stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 observer_->OnRenegotiationNeeded();
683 return true;
684}
685
686void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100687 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
deadbeefab9b2d12015-10-14 11:33:11 -0700688 for (const auto& track : local_stream->GetAudioTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800689 OnAudioTrackRemoved(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700690 }
691 for (const auto& track : local_stream->GetVideoTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800692 OnVideoTrackRemoved(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700693 }
694
695 local_streams_->RemoveStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -0800696 stream_observers_.erase(
697 std::remove_if(
698 stream_observers_.begin(), stream_observers_.end(),
699 [local_stream](const rtc::scoped_ptr<MediaStreamObserver>& observer) {
700 return observer->stream()->label().compare(local_stream->label()) ==
701 0;
702 }),
703 stream_observers_.end());
deadbeefab9b2d12015-10-14 11:33:11 -0700704
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 if (IsClosed()) {
706 return;
707 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 observer_->OnRenegotiationNeeded();
709}
710
deadbeefe1f9d832016-01-14 15:35:42 -0800711rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack(
712 MediaStreamTrackInterface* track,
713 std::vector<MediaStreamInterface*> streams) {
714 TRACE_EVENT0("webrtc", "PeerConnection::AddTrack");
715 if (IsClosed()) {
716 return nullptr;
717 }
718 if (streams.size() >= 2) {
719 LOG(LS_ERROR)
720 << "Adding a track with two streams is not currently supported.";
721 return nullptr;
722 }
723 // TODO(deadbeef): Support adding a track to two different senders.
724 if (FindSenderForTrack(track) != senders_.end()) {
725 LOG(LS_ERROR) << "Sender for track " << track->id() << " already exists.";
726 return nullptr;
727 }
728
729 // TODO(deadbeef): Support adding a track to multiple streams.
730 rtc::scoped_refptr<RtpSenderInterface> new_sender;
731 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
732 new_sender = RtpSenderProxy::Create(
733 signaling_thread(),
734 new AudioRtpSender(static_cast<AudioTrackInterface*>(track),
735 session_.get(), stats_.get()));
736 if (!streams.empty()) {
737 new_sender->set_stream_id(streams[0]->label());
738 }
739 const TrackInfo* track_info = FindTrackInfo(
740 local_audio_tracks_, new_sender->stream_id(), track->id());
741 if (track_info) {
742 new_sender->SetSsrc(track_info->ssrc);
743 }
744 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
745 new_sender = RtpSenderProxy::Create(
746 signaling_thread(),
747 new VideoRtpSender(static_cast<VideoTrackInterface*>(track),
748 session_.get()));
749 if (!streams.empty()) {
750 new_sender->set_stream_id(streams[0]->label());
751 }
752 const TrackInfo* track_info = FindTrackInfo(
753 local_video_tracks_, new_sender->stream_id(), track->id());
754 if (track_info) {
755 new_sender->SetSsrc(track_info->ssrc);
756 }
757 } else {
758 LOG(LS_ERROR) << "CreateSender called with invalid kind: " << track->kind();
759 return rtc::scoped_refptr<RtpSenderInterface>();
760 }
761
762 senders_.push_back(new_sender);
763 observer_->OnRenegotiationNeeded();
764 return new_sender;
765}
766
767bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) {
768 TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack");
769 if (IsClosed()) {
770 return false;
771 }
772
773 auto it = std::find(senders_.begin(), senders_.end(), sender);
774 if (it == senders_.end()) {
775 LOG(LS_ERROR) << "Couldn't find sender " << sender->id() << " to remove.";
776 return false;
777 }
778 (*it)->Stop();
779 senders_.erase(it);
780
781 observer_->OnRenegotiationNeeded();
782 return true;
783}
784
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000785rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 AudioTrackInterface* track) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100787 TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 if (!track) {
789 LOG(LS_ERROR) << "CreateDtmfSender - track is NULL.";
790 return NULL;
791 }
deadbeefab9b2d12015-10-14 11:33:11 -0700792 if (!local_streams_->FindAudioTrack(track->id())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track.";
794 return NULL;
795 }
796
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000797 rtc::scoped_refptr<DtmfSenderInterface> sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 DtmfSender::Create(track, signaling_thread(), session_.get()));
799 if (!sender.get()) {
800 LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create.";
801 return NULL;
802 }
803 return DtmfSenderProxy::Create(signaling_thread(), sender.get());
804}
805
deadbeeffac06552015-11-25 11:26:01 -0800806rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800807 const std::string& kind,
808 const std::string& stream_id) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100809 TRACE_EVENT0("webrtc", "PeerConnection::CreateSender");
deadbeefe1f9d832016-01-14 15:35:42 -0800810 rtc::scoped_refptr<RtpSenderInterface> new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800811 if (kind == MediaStreamTrackInterface::kAudioKind) {
deadbeefe1f9d832016-01-14 15:35:42 -0800812 new_sender = RtpSenderProxy::Create(
813 signaling_thread(), new AudioRtpSender(session_.get(), stats_.get()));
deadbeeffac06552015-11-25 11:26:01 -0800814 } else if (kind == MediaStreamTrackInterface::kVideoKind) {
deadbeefe1f9d832016-01-14 15:35:42 -0800815 new_sender = RtpSenderProxy::Create(signaling_thread(),
816 new VideoRtpSender(session_.get()));
deadbeeffac06552015-11-25 11:26:01 -0800817 } else {
818 LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind;
deadbeefe1f9d832016-01-14 15:35:42 -0800819 return new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800820 }
deadbeefbd7d8f72015-12-18 16:58:44 -0800821 if (!stream_id.empty()) {
822 new_sender->set_stream_id(stream_id);
823 }
deadbeeffac06552015-11-25 11:26:01 -0800824 senders_.push_back(new_sender);
deadbeefe1f9d832016-01-14 15:35:42 -0800825 return new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800826}
827
deadbeef70ab1a12015-09-28 16:53:55 -0700828std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders()
829 const {
deadbeefe1f9d832016-01-14 15:35:42 -0800830 return senders_;
deadbeef70ab1a12015-09-28 16:53:55 -0700831}
832
833std::vector<rtc::scoped_refptr<RtpReceiverInterface>>
834PeerConnection::GetReceivers() const {
deadbeefe1f9d832016-01-14 15:35:42 -0800835 return receivers_;
deadbeef70ab1a12015-09-28 16:53:55 -0700836}
837
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838bool PeerConnection::GetStats(StatsObserver* observer,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000839 MediaStreamTrackInterface* track,
840 StatsOutputLevel level) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100841 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
deadbeef0a6c4ca2015-10-06 11:38:28 -0700842 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 if (!VERIFY(observer != NULL)) {
844 LOG(LS_ERROR) << "GetStats - observer is NULL.";
845 return false;
846 }
847
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000848 stats_->UpdateStats(level);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000849 signaling_thread()->Post(this, MSG_GETSTATS,
850 new GetStatsMsg(observer, track));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 return true;
852}
853
854PeerConnectionInterface::SignalingState PeerConnection::signaling_state() {
855 return signaling_state_;
856}
857
858PeerConnectionInterface::IceState PeerConnection::ice_state() {
859 return ice_state_;
860}
861
862PeerConnectionInterface::IceConnectionState
863PeerConnection::ice_connection_state() {
864 return ice_connection_state_;
865}
866
867PeerConnectionInterface::IceGatheringState
868PeerConnection::ice_gathering_state() {
869 return ice_gathering_state_;
870}
871
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000872rtc::scoped_refptr<DataChannelInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873PeerConnection::CreateDataChannel(
874 const std::string& label,
875 const DataChannelInit* config) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100876 TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel");
deadbeefab9b2d12015-10-14 11:33:11 -0700877 bool first_datachannel = !HasDataChannels();
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000878
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000879 rtc::scoped_ptr<InternalDataChannelInit> internal_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000880 if (config) {
881 internal_config.reset(new InternalDataChannelInit(*config));
882 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000883 rtc::scoped_refptr<DataChannelInterface> channel(
deadbeefab9b2d12015-10-14 11:33:11 -0700884 InternalCreateDataChannel(label, internal_config.get()));
885 if (!channel.get()) {
886 return nullptr;
887 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000889 // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or
890 // the first SCTP DataChannel.
891 if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) {
892 observer_->OnRenegotiationNeeded();
893 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000894
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 return DataChannelProxy::Create(signaling_thread(), channel.get());
896}
897
898void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
899 const MediaConstraintsInterface* constraints) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100900 TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer");
deadbeefab9b2d12015-10-14 11:33:11 -0700901 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
903 return;
904 }
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000905 RTCOfferAnswerOptions options;
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000906
907 bool value;
908 size_t mandatory_constraints = 0;
909
910 if (FindConstraint(constraints,
911 MediaConstraintsInterface::kOfferToReceiveAudio,
912 &value,
913 &mandatory_constraints)) {
914 options.offer_to_receive_audio =
915 value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0;
916 }
917
918 if (FindConstraint(constraints,
919 MediaConstraintsInterface::kOfferToReceiveVideo,
920 &value,
921 &mandatory_constraints)) {
922 options.offer_to_receive_video =
923 value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0;
924 }
925
926 if (FindConstraint(constraints,
927 MediaConstraintsInterface::kVoiceActivityDetection,
928 &value,
929 &mandatory_constraints)) {
930 options.voice_activity_detection = value;
931 }
932
933 if (FindConstraint(constraints,
934 MediaConstraintsInterface::kIceRestart,
935 &value,
936 &mandatory_constraints)) {
937 options.ice_restart = value;
938 }
939
940 if (FindConstraint(constraints,
941 MediaConstraintsInterface::kUseRtpMux,
942 &value,
943 &mandatory_constraints)) {
944 options.use_rtp_mux = value;
945 }
946
947 CreateOffer(observer, options);
948}
949
950void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
951 const RTCOfferAnswerOptions& options) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100952 TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer");
deadbeefab9b2d12015-10-14 11:33:11 -0700953 if (!VERIFY(observer != nullptr)) {
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000954 LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
955 return;
956 }
deadbeefab9b2d12015-10-14 11:33:11 -0700957
958 cricket::MediaSessionOptions session_options;
959 if (!GetOptionsForOffer(options, &session_options)) {
960 std::string error = "CreateOffer called with invalid options.";
961 LOG(LS_ERROR) << error;
962 PostCreateSessionDescriptionFailure(observer, error);
963 return;
964 }
965
966 session_->CreateOffer(observer, options, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967}
968
969void PeerConnection::CreateAnswer(
970 CreateSessionDescriptionObserver* observer,
971 const MediaConstraintsInterface* constraints) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100972 TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer");
deadbeefab9b2d12015-10-14 11:33:11 -0700973 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
975 return;
976 }
deadbeefab9b2d12015-10-14 11:33:11 -0700977
978 cricket::MediaSessionOptions session_options;
979 if (!GetOptionsForAnswer(constraints, &session_options)) {
980 std::string error = "CreateAnswer called with invalid constraints.";
981 LOG(LS_ERROR) << error;
982 PostCreateSessionDescriptionFailure(observer, error);
983 return;
984 }
985
htaa2a49d92016-03-04 02:51:39 -0800986 session_->CreateAnswer(observer, session_options);
987}
988
989void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer,
990 const RTCOfferAnswerOptions& options) {
991 TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer");
992 if (!VERIFY(observer != nullptr)) {
993 LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
994 return;
995 }
996
997 cricket::MediaSessionOptions session_options;
998 if (!GetOptionsForAnswer(options, &session_options)) {
999 std::string error = "CreateAnswer called with invalid options.";
1000 LOG(LS_ERROR) << error;
1001 PostCreateSessionDescriptionFailure(observer, error);
1002 return;
1003 }
1004
1005 session_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006}
1007
1008void PeerConnection::SetLocalDescription(
1009 SetSessionDescriptionObserver* observer,
1010 SessionDescriptionInterface* desc) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001011 TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription");
deadbeefab9b2d12015-10-14 11:33:11 -07001012 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
1014 return;
1015 }
1016 if (!desc) {
1017 PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL.");
1018 return;
1019 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 // Update stats here so that we have the most recent stats for tracks and
1021 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001022 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 std::string error;
1024 if (!session_->SetLocalDescription(desc, &error)) {
1025 PostSetSessionDescriptionFailure(observer, error);
1026 return;
1027 }
deadbeefab9b2d12015-10-14 11:33:11 -07001028
1029 // If setting the description decided our SSL role, allocate any necessary
1030 // SCTP sids.
1031 rtc::SSLRole role;
1032 if (session_->data_channel_type() == cricket::DCT_SCTP &&
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001033 session_->GetSslRole(session_->data_channel(), &role)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001034 AllocateSctpSids(role);
1035 }
1036
1037 // Update state and SSRC of local MediaStreams and DataChannels based on the
1038 // local session description.
1039 const cricket::ContentInfo* audio_content =
1040 GetFirstAudioContent(desc->description());
1041 if (audio_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001042 if (audio_content->rejected) {
1043 RemoveTracks(cricket::MEDIA_TYPE_AUDIO);
1044 } else {
1045 const cricket::AudioContentDescription* audio_desc =
1046 static_cast<const cricket::AudioContentDescription*>(
1047 audio_content->description);
1048 UpdateLocalTracks(audio_desc->streams(), audio_desc->type());
1049 }
deadbeefab9b2d12015-10-14 11:33:11 -07001050 }
1051
1052 const cricket::ContentInfo* video_content =
1053 GetFirstVideoContent(desc->description());
1054 if (video_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001055 if (video_content->rejected) {
1056 RemoveTracks(cricket::MEDIA_TYPE_VIDEO);
1057 } else {
1058 const cricket::VideoContentDescription* video_desc =
1059 static_cast<const cricket::VideoContentDescription*>(
1060 video_content->description);
1061 UpdateLocalTracks(video_desc->streams(), video_desc->type());
1062 }
deadbeefab9b2d12015-10-14 11:33:11 -07001063 }
1064
1065 const cricket::ContentInfo* data_content =
1066 GetFirstDataContent(desc->description());
1067 if (data_content) {
1068 const cricket::DataContentDescription* data_desc =
1069 static_cast<const cricket::DataContentDescription*>(
1070 data_content->description);
1071 if (rtc::starts_with(data_desc->protocol().data(),
1072 cricket::kMediaProtocolRtpPrefix)) {
1073 UpdateLocalRtpDataChannels(data_desc->streams());
1074 }
1075 }
1076
1077 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
deadbeefab9b2d12015-10-14 11:33:11 -07001079
deadbeefcbecd352015-09-23 11:50:27 -07001080 // MaybeStartGathering needs to be called after posting
1081 // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates
1082 // before signaling that SetLocalDescription completed.
1083 session_->MaybeStartGathering();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084}
1085
1086void PeerConnection::SetRemoteDescription(
1087 SetSessionDescriptionObserver* observer,
1088 SessionDescriptionInterface* desc) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001089 TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription");
deadbeefab9b2d12015-10-14 11:33:11 -07001090 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
1092 return;
1093 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 if (!desc) {
1095 PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL.");
1096 return;
1097 }
1098 // Update stats here so that we have the most recent stats for tracks and
1099 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001100 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 std::string error;
1102 if (!session_->SetRemoteDescription(desc, &error)) {
1103 PostSetSessionDescriptionFailure(observer, error);
1104 return;
1105 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106
deadbeefab9b2d12015-10-14 11:33:11 -07001107 // If setting the description decided our SSL role, allocate any necessary
1108 // SCTP sids.
1109 rtc::SSLRole role;
1110 if (session_->data_channel_type() == cricket::DCT_SCTP &&
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001111 session_->GetSslRole(session_->data_channel(), &role)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001112 AllocateSctpSids(role);
1113 }
1114
1115 const cricket::SessionDescription* remote_desc = desc->description();
deadbeefbda7e0b2015-12-08 17:13:40 -08001116 const cricket::ContentInfo* audio_content = GetFirstAudioContent(remote_desc);
1117 const cricket::ContentInfo* video_content = GetFirstVideoContent(remote_desc);
1118 const cricket::AudioContentDescription* audio_desc =
1119 GetFirstAudioContentDescription(remote_desc);
1120 const cricket::VideoContentDescription* video_desc =
1121 GetFirstVideoContentDescription(remote_desc);
1122 const cricket::DataContentDescription* data_desc =
1123 GetFirstDataContentDescription(remote_desc);
1124
1125 // Check if the descriptions include streams, just in case the peer supports
1126 // MSID, but doesn't indicate so with "a=msid-semantic".
1127 if (remote_desc->msid_supported() ||
1128 (audio_desc && !audio_desc->streams().empty()) ||
1129 (video_desc && !video_desc->streams().empty())) {
1130 remote_peer_supports_msid_ = true;
1131 }
deadbeefab9b2d12015-10-14 11:33:11 -07001132
1133 // We wait to signal new streams until we finish processing the description,
1134 // since only at that point will new streams have all their tracks.
1135 rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
1136
1137 // Find all audio rtp streams and create corresponding remote AudioTracks
1138 // and MediaStreams.
deadbeefab9b2d12015-10-14 11:33:11 -07001139 if (audio_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001140 if (audio_content->rejected) {
1141 RemoveTracks(cricket::MEDIA_TYPE_AUDIO);
1142 } else {
deadbeefbda7e0b2015-12-08 17:13:40 -08001143 bool default_audio_track_needed =
1144 !remote_peer_supports_msid_ &&
1145 MediaContentDirectionHasSend(audio_desc->direction());
1146 UpdateRemoteStreamsList(GetActiveStreams(audio_desc),
1147 default_audio_track_needed, audio_desc->type(),
deadbeeffaac4972015-11-12 15:33:07 -08001148 new_streams);
deadbeeffaac4972015-11-12 15:33:07 -08001149 }
deadbeefab9b2d12015-10-14 11:33:11 -07001150 }
1151
1152 // Find all video rtp streams and create corresponding remote VideoTracks
1153 // and MediaStreams.
deadbeefab9b2d12015-10-14 11:33:11 -07001154 if (video_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001155 if (video_content->rejected) {
1156 RemoveTracks(cricket::MEDIA_TYPE_VIDEO);
1157 } else {
deadbeefbda7e0b2015-12-08 17:13:40 -08001158 bool default_video_track_needed =
1159 !remote_peer_supports_msid_ &&
1160 MediaContentDirectionHasSend(video_desc->direction());
1161 UpdateRemoteStreamsList(GetActiveStreams(video_desc),
1162 default_video_track_needed, video_desc->type(),
deadbeeffaac4972015-11-12 15:33:07 -08001163 new_streams);
deadbeeffaac4972015-11-12 15:33:07 -08001164 }
deadbeefab9b2d12015-10-14 11:33:11 -07001165 }
1166
1167 // Update the DataChannels with the information from the remote peer.
deadbeefbda7e0b2015-12-08 17:13:40 -08001168 if (data_desc) {
1169 if (rtc::starts_with(data_desc->protocol().data(),
deadbeefab9b2d12015-10-14 11:33:11 -07001170 cricket::kMediaProtocolRtpPrefix)) {
deadbeefbda7e0b2015-12-08 17:13:40 -08001171 UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc));
deadbeefab9b2d12015-10-14 11:33:11 -07001172 }
1173 }
1174
1175 // Iterate new_streams and notify the observer about new MediaStreams.
1176 for (size_t i = 0; i < new_streams->count(); ++i) {
1177 MediaStreamInterface* new_stream = new_streams->at(i);
1178 stats_->AddStream(new_stream);
1179 observer_->OnAddStream(new_stream);
1180 }
1181
deadbeefbda7e0b2015-12-08 17:13:40 -08001182 UpdateEndedRemoteMediaStreams();
deadbeefab9b2d12015-10-14 11:33:11 -07001183
1184 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
1185 signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
deadbeeffc648b62015-10-13 16:42:33 -07001186}
1187
deadbeefa67696b2015-09-29 11:56:26 -07001188bool PeerConnection::SetConfiguration(const RTCConfiguration& config) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001189 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001190 if (port_allocator_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001191 cricket::ServerAddresses stun_servers;
1192 std::vector<cricket::RelayServerConfig> turn_servers;
1193 if (!ParseIceServers(config.servers, &stun_servers, &turn_servers)) {
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001194 return false;
1195 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001196 port_allocator_->SetIceServers(stun_servers, turn_servers);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001197 }
honghaiz1f429e32015-09-28 07:57:34 -07001198 session_->SetIceConfig(session_->ParseIceConfig(config));
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001199 return session_->SetIceTransports(config.type);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001200}
1201
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202bool PeerConnection::AddIceCandidate(
1203 const IceCandidateInterface* ice_candidate) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001204 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 return session_->ProcessIceMessage(ice_candidate);
1206}
1207
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001208bool PeerConnection::RemoveIceCandidates(
1209 const std::vector<cricket::Candidate>& candidates) {
1210 TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates");
1211 return session_->RemoveRemoteIceCandidates(candidates);
1212}
1213
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001214void PeerConnection::RegisterUMAObserver(UMAObserver* observer) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001215 TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver");
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001216 uma_observer_ = observer;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001217
1218 if (session_) {
1219 session_->set_metrics_observer(uma_observer_);
1220 }
1221
mallinath@webrtc.orgd37bcfa2014-05-12 23:10:18 +00001222 // Send information about IPv4/IPv6 status.
1223 if (uma_observer_ && port_allocator_) {
1224 if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) {
Guo-wei Shiehdfbe6792015-09-03 17:12:07 -07001225 uma_observer_->IncrementEnumCounter(
1226 kEnumCounterAddressFamily, kPeerConnection_IPv6,
1227 kPeerConnectionAddressFamilyCounter_Max);
mallinath@webrtc.orgb445f262014-05-23 22:19:37 +00001228 } else {
Guo-wei Shiehdfbe6792015-09-03 17:12:07 -07001229 uma_observer_->IncrementEnumCounter(
1230 kEnumCounterAddressFamily, kPeerConnection_IPv4,
1231 kPeerConnectionAddressFamilyCounter_Max);
mallinath@webrtc.orgd37bcfa2014-05-12 23:10:18 +00001232 }
1233 }
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001234}
1235
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236const SessionDescriptionInterface* PeerConnection::local_description() const {
1237 return session_->local_description();
1238}
1239
1240const SessionDescriptionInterface* PeerConnection::remote_description() const {
1241 return session_->remote_description();
1242}
1243
1244void PeerConnection::Close() {
Peter Boström1a9d6152015-12-08 22:15:17 +01001245 TRACE_EVENT0("webrtc", "PeerConnection::Close");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 // Update stats here so that we have the most recent stats for tracks and
1247 // streams before the channels are closed.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001248 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249
deadbeefd59daf82015-10-14 15:02:44 -07001250 session_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251}
1252
deadbeefd59daf82015-10-14 15:02:44 -07001253void PeerConnection::OnSessionStateChange(WebRtcSession* /*session*/,
1254 WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 switch (state) {
deadbeefd59daf82015-10-14 15:02:44 -07001256 case WebRtcSession::STATE_INIT:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 ChangeSignalingState(PeerConnectionInterface::kStable);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001258 break;
deadbeefd59daf82015-10-14 15:02:44 -07001259 case WebRtcSession::STATE_SENTOFFER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer);
1261 break;
deadbeefd59daf82015-10-14 15:02:44 -07001262 case WebRtcSession::STATE_SENTPRANSWER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer);
1264 break;
deadbeefd59daf82015-10-14 15:02:44 -07001265 case WebRtcSession::STATE_RECEIVEDOFFER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer);
1267 break;
deadbeefd59daf82015-10-14 15:02:44 -07001268 case WebRtcSession::STATE_RECEIVEDPRANSWER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer);
1270 break;
deadbeefd59daf82015-10-14 15:02:44 -07001271 case WebRtcSession::STATE_INPROGRESS:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 ChangeSignalingState(PeerConnectionInterface::kStable);
1273 break;
deadbeefd59daf82015-10-14 15:02:44 -07001274 case WebRtcSession::STATE_CLOSED:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 ChangeSignalingState(PeerConnectionInterface::kClosed);
1276 break;
1277 default:
1278 break;
1279 }
1280}
1281
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001282void PeerConnection::OnMessage(rtc::Message* msg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 switch (msg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 case MSG_SET_SESSIONDESCRIPTION_SUCCESS: {
1285 SetSessionDescriptionMsg* param =
1286 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
1287 param->observer->OnSuccess();
1288 delete param;
1289 break;
1290 }
1291 case MSG_SET_SESSIONDESCRIPTION_FAILED: {
1292 SetSessionDescriptionMsg* param =
1293 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
1294 param->observer->OnFailure(param->error);
1295 delete param;
1296 break;
1297 }
deadbeefab9b2d12015-10-14 11:33:11 -07001298 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
1299 CreateSessionDescriptionMsg* param =
1300 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
1301 param->observer->OnFailure(param->error);
1302 delete param;
1303 break;
1304 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 case MSG_GETSTATS: {
1306 GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +00001307 StatsReports reports;
1308 stats_->GetStats(param->track, &reports);
1309 param->observer->OnComplete(reports);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 delete param;
1311 break;
1312 }
deadbeefbd292462015-12-14 18:15:29 -08001313 case MSG_FREE_DATACHANNELS: {
1314 sctp_data_channels_to_free_.clear();
1315 break;
1316 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 default:
deadbeef0a6c4ca2015-10-06 11:38:28 -07001318 RTC_DCHECK(false && "Not implemented");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 break;
1320 }
1321}
1322
deadbeefab9b2d12015-10-14 11:33:11 -07001323void PeerConnection::CreateAudioReceiver(MediaStreamInterface* stream,
1324 AudioTrackInterface* audio_track,
1325 uint32_t ssrc) {
deadbeefe1f9d832016-01-14 15:35:42 -08001326 receivers_.push_back(RtpReceiverProxy::Create(
1327 signaling_thread(),
1328 new AudioRtpReceiver(audio_track, ssrc, session_.get())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329}
1330
deadbeefab9b2d12015-10-14 11:33:11 -07001331void PeerConnection::CreateVideoReceiver(MediaStreamInterface* stream,
perkjf0dcfe22016-03-10 18:32:00 +01001332 const std::string& track_id,
deadbeefab9b2d12015-10-14 11:33:11 -07001333 uint32_t ssrc) {
perkjf0dcfe22016-03-10 18:32:00 +01001334 VideoRtpReceiver* video_receiver = new VideoRtpReceiver(
1335 stream, track_id, factory_->worker_thread(), ssrc, session_.get());
1336 receivers_.push_back(
1337 RtpReceiverProxy::Create(signaling_thread(), video_receiver));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338}
1339
deadbeef70ab1a12015-09-28 16:53:55 -07001340// TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote
1341// description.
deadbeefab9b2d12015-10-14 11:33:11 -07001342void PeerConnection::DestroyAudioReceiver(MediaStreamInterface* stream,
1343 AudioTrackInterface* audio_track) {
deadbeef70ab1a12015-09-28 16:53:55 -07001344 auto it = FindReceiverForTrack(audio_track);
1345 if (it == receivers_.end()) {
1346 LOG(LS_WARNING) << "RtpReceiver for track with id " << audio_track->id()
1347 << " doesn't exist.";
1348 } else {
1349 (*it)->Stop();
1350 receivers_.erase(it);
1351 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352}
1353
deadbeefab9b2d12015-10-14 11:33:11 -07001354void PeerConnection::DestroyVideoReceiver(MediaStreamInterface* stream,
1355 VideoTrackInterface* video_track) {
deadbeef70ab1a12015-09-28 16:53:55 -07001356 auto it = FindReceiverForTrack(video_track);
1357 if (it == receivers_.end()) {
1358 LOG(LS_WARNING) << "RtpReceiver for track with id " << video_track->id()
1359 << " doesn't exist.";
1360 } else {
1361 (*it)->Stop();
1362 receivers_.erase(it);
1363 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364}
deadbeef70ab1a12015-09-28 16:53:55 -07001365
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366void PeerConnection::OnIceConnectionChange(
1367 PeerConnectionInterface::IceConnectionState new_state) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001368 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001369 // After transitioning to "closed", ignore any additional states from
1370 // WebRtcSession (such as "disconnected").
deadbeefab9b2d12015-10-14 11:33:11 -07001371 if (IsClosed()) {
deadbeefcbecd352015-09-23 11:50:27 -07001372 return;
1373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 ice_connection_state_ = new_state;
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001375 observer_->OnIceConnectionChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376}
1377
1378void PeerConnection::OnIceGatheringChange(
1379 PeerConnectionInterface::IceGatheringState new_state) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001380 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381 if (IsClosed()) {
1382 return;
1383 }
1384 ice_gathering_state_ = new_state;
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001385 observer_->OnIceGatheringChange(ice_gathering_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386}
1387
1388void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001389 RTC_DCHECK(signaling_thread()->IsCurrent());
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001390 observer_->OnIceCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391}
1392
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001393void PeerConnection::OnIceCandidatesRemoved(
1394 const std::vector<cricket::Candidate>& candidates) {
1395 RTC_DCHECK(signaling_thread()->IsCurrent());
1396 observer_->OnIceCandidatesRemoved(candidates);
1397}
1398
Peter Thatcher54360512015-07-08 11:08:35 -07001399void PeerConnection::OnIceConnectionReceivingChange(bool receiving) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001400 RTC_DCHECK(signaling_thread()->IsCurrent());
Peter Thatcher54360512015-07-08 11:08:35 -07001401 observer_->OnIceConnectionReceivingChange(receiving);
1402}
1403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404void PeerConnection::ChangeSignalingState(
1405 PeerConnectionInterface::SignalingState signaling_state) {
1406 signaling_state_ = signaling_state;
1407 if (signaling_state == kClosed) {
1408 ice_connection_state_ = kIceConnectionClosed;
1409 observer_->OnIceConnectionChange(ice_connection_state_);
1410 if (ice_gathering_state_ != kIceGatheringComplete) {
1411 ice_gathering_state_ = kIceGatheringComplete;
1412 observer_->OnIceGatheringChange(ice_gathering_state_);
1413 }
1414 }
1415 observer_->OnSignalingChange(signaling_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416}
1417
deadbeefeb459812015-12-15 19:24:43 -08001418void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track,
1419 MediaStreamInterface* stream) {
1420 auto sender = FindSenderForTrack(track);
1421 if (sender != senders_.end()) {
1422 // We already have a sender for this track, so just change the stream_id
1423 // so that it's correct in the next call to CreateOffer.
1424 (*sender)->set_stream_id(stream->label());
1425 return;
1426 }
1427
1428 // Normal case; we've never seen this track before.
deadbeefe1f9d832016-01-14 15:35:42 -08001429 rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create(
1430 signaling_thread(),
1431 new AudioRtpSender(track, stream->label(), session_.get(), stats_.get()));
deadbeefeb459812015-12-15 19:24:43 -08001432 senders_.push_back(new_sender);
1433 // If the sender has already been configured in SDP, we call SetSsrc,
1434 // which will connect the sender to the underlying transport. This can
1435 // occur if a local session description that contains the ID of the sender
1436 // is set before AddStream is called. It can also occur if the local
1437 // session description is not changed and RemoveStream is called, and
1438 // later AddStream is called again with the same stream.
1439 const TrackInfo* track_info =
1440 FindTrackInfo(local_audio_tracks_, stream->label(), track->id());
1441 if (track_info) {
1442 new_sender->SetSsrc(track_info->ssrc);
1443 }
1444}
1445
1446// TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
1447// indefinitely, when we have unified plan SDP.
1448void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track,
1449 MediaStreamInterface* stream) {
1450 auto sender = FindSenderForTrack(track);
1451 if (sender == senders_.end()) {
1452 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1453 << " doesn't exist.";
1454 return;
1455 }
1456 (*sender)->Stop();
1457 senders_.erase(sender);
1458}
1459
1460void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track,
1461 MediaStreamInterface* stream) {
1462 auto sender = FindSenderForTrack(track);
1463 if (sender != senders_.end()) {
1464 // We already have a sender for this track, so just change the stream_id
1465 // so that it's correct in the next call to CreateOffer.
1466 (*sender)->set_stream_id(stream->label());
1467 return;
1468 }
1469
1470 // Normal case; we've never seen this track before.
deadbeefe1f9d832016-01-14 15:35:42 -08001471 rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create(
1472 signaling_thread(),
1473 new VideoRtpSender(track, stream->label(), session_.get()));
deadbeefeb459812015-12-15 19:24:43 -08001474 senders_.push_back(new_sender);
1475 const TrackInfo* track_info =
1476 FindTrackInfo(local_video_tracks_, stream->label(), track->id());
1477 if (track_info) {
1478 new_sender->SetSsrc(track_info->ssrc);
1479 }
1480}
1481
1482void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track,
1483 MediaStreamInterface* stream) {
1484 auto sender = FindSenderForTrack(track);
1485 if (sender == senders_.end()) {
1486 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1487 << " doesn't exist.";
1488 return;
1489 }
1490 (*sender)->Stop();
1491 senders_.erase(sender);
1492}
1493
deadbeefab9b2d12015-10-14 11:33:11 -07001494void PeerConnection::PostSetSessionDescriptionFailure(
1495 SetSessionDescriptionObserver* observer,
1496 const std::string& error) {
1497 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
1498 msg->error = error;
1499 signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_FAILED, msg);
1500}
1501
1502void PeerConnection::PostCreateSessionDescriptionFailure(
1503 CreateSessionDescriptionObserver* observer,
1504 const std::string& error) {
1505 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
1506 msg->error = error;
1507 signaling_thread()->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
1508}
1509
1510bool PeerConnection::GetOptionsForOffer(
1511 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
1512 cricket::MediaSessionOptions* session_options) {
deadbeef0ed85b22016-02-23 17:24:52 -08001513 // TODO(deadbeef): Once we have transceivers, enumerate them here instead of
1514 // ContentInfos.
1515 if (session_->local_description()) {
1516 for (const cricket::ContentInfo& content :
1517 session_->local_description()->description()->contents()) {
1518 session_options->transport_options[content.name] =
1519 cricket::TransportOptions();
1520 }
1521 }
htaaac2dea2016-03-10 13:35:55 -08001522 if (!ExtractMediaSessionOptions(rtc_options, true, session_options)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001523 return false;
1524 }
1525
deadbeeffac06552015-11-25 11:26:01 -08001526 AddSendStreams(session_options, senders_, rtp_data_channels_);
deadbeefc80741f2015-10-22 13:14:45 -07001527 // Offer to receive audio/video if the constraint is not set and there are
1528 // send streams, or we're currently receiving.
1529 if (rtc_options.offer_to_receive_audio == RTCOfferAnswerOptions::kUndefined) {
1530 session_options->recv_audio =
1531 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO) ||
1532 !remote_audio_tracks_.empty();
1533 }
1534 if (rtc_options.offer_to_receive_video == RTCOfferAnswerOptions::kUndefined) {
1535 session_options->recv_video =
1536 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO) ||
1537 !remote_video_tracks_.empty();
1538 }
1539 session_options->bundle_enabled =
1540 session_options->bundle_enabled &&
1541 (session_options->has_audio() || session_options->has_video() ||
1542 session_options->has_data());
1543
deadbeefab9b2d12015-10-14 11:33:11 -07001544 if (session_->data_channel_type() == cricket::DCT_SCTP && HasDataChannels()) {
1545 session_options->data_channel_type = cricket::DCT_SCTP;
1546 }
1547 return true;
1548}
1549
htaa2a49d92016-03-04 02:51:39 -08001550void PeerConnection::FinishOptionsForAnswer(
deadbeefab9b2d12015-10-14 11:33:11 -07001551 cricket::MediaSessionOptions* session_options) {
deadbeef0ed85b22016-02-23 17:24:52 -08001552 // TODO(deadbeef): Once we have transceivers, enumerate them here instead of
1553 // ContentInfos.
1554 if (session_->remote_description()) {
1555 // Initialize the transport_options map.
1556 for (const cricket::ContentInfo& content :
1557 session_->remote_description()->description()->contents()) {
1558 session_options->transport_options[content.name] =
1559 cricket::TransportOptions();
1560 }
1561 }
deadbeeffac06552015-11-25 11:26:01 -08001562 AddSendStreams(session_options, senders_, rtp_data_channels_);
deadbeefc80741f2015-10-22 13:14:45 -07001563 session_options->bundle_enabled =
1564 session_options->bundle_enabled &&
1565 (session_options->has_audio() || session_options->has_video() ||
1566 session_options->has_data());
1567
deadbeefab9b2d12015-10-14 11:33:11 -07001568 // RTP data channel is handled in MediaSessionOptions::AddStream. SCTP streams
1569 // are not signaled in the SDP so does not go through that path and must be
1570 // handled here.
1571 if (session_->data_channel_type() == cricket::DCT_SCTP) {
1572 session_options->data_channel_type = cricket::DCT_SCTP;
1573 }
htaa2a49d92016-03-04 02:51:39 -08001574}
1575
1576bool PeerConnection::GetOptionsForAnswer(
1577 const MediaConstraintsInterface* constraints,
1578 cricket::MediaSessionOptions* session_options) {
1579 session_options->recv_audio = false;
1580 session_options->recv_video = false;
1581 if (!ParseConstraintsForAnswer(constraints, session_options)) {
1582 return false;
1583 }
1584 FinishOptionsForAnswer(session_options);
1585 return true;
1586}
1587
1588bool PeerConnection::GetOptionsForAnswer(
1589 const RTCOfferAnswerOptions& options,
1590 cricket::MediaSessionOptions* session_options) {
1591 session_options->recv_audio = false;
1592 session_options->recv_video = false;
htaaac2dea2016-03-10 13:35:55 -08001593 if (!ExtractMediaSessionOptions(options, false, session_options)) {
htaa2a49d92016-03-04 02:51:39 -08001594 return false;
1595 }
1596 FinishOptionsForAnswer(session_options);
deadbeefab9b2d12015-10-14 11:33:11 -07001597 return true;
1598}
1599
deadbeeffaac4972015-11-12 15:33:07 -08001600void PeerConnection::RemoveTracks(cricket::MediaType media_type) {
1601 UpdateLocalTracks(std::vector<cricket::StreamParams>(), media_type);
deadbeefbda7e0b2015-12-08 17:13:40 -08001602 UpdateRemoteStreamsList(std::vector<cricket::StreamParams>(), false,
1603 media_type, nullptr);
deadbeeffaac4972015-11-12 15:33:07 -08001604}
1605
deadbeefab9b2d12015-10-14 11:33:11 -07001606void PeerConnection::UpdateRemoteStreamsList(
1607 const cricket::StreamParamsVec& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -08001608 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -07001609 cricket::MediaType media_type,
1610 StreamCollection* new_streams) {
1611 TrackInfos* current_tracks = GetRemoteTracks(media_type);
1612
1613 // Find removed tracks. I.e., tracks where the track id or ssrc don't match
deadbeeffac06552015-11-25 11:26:01 -08001614 // the new StreamParam.
deadbeefab9b2d12015-10-14 11:33:11 -07001615 auto track_it = current_tracks->begin();
1616 while (track_it != current_tracks->end()) {
1617 const TrackInfo& info = *track_it;
1618 const cricket::StreamParams* params =
1619 cricket::GetStreamBySsrc(streams, info.ssrc);
deadbeefbda7e0b2015-12-08 17:13:40 -08001620 bool track_exists = params && params->id == info.track_id;
1621 // If this is a default track, and we still need it, don't remove it.
1622 if ((info.stream_label == kDefaultStreamLabel && default_track_needed) ||
1623 track_exists) {
1624 ++track_it;
1625 } else {
deadbeefab9b2d12015-10-14 11:33:11 -07001626 OnRemoteTrackRemoved(info.stream_label, info.track_id, media_type);
1627 track_it = current_tracks->erase(track_it);
deadbeefab9b2d12015-10-14 11:33:11 -07001628 }
1629 }
1630
1631 // Find new and active tracks.
1632 for (const cricket::StreamParams& params : streams) {
1633 // The sync_label is the MediaStream label and the |stream.id| is the
1634 // track id.
1635 const std::string& stream_label = params.sync_label;
1636 const std::string& track_id = params.id;
1637 uint32_t ssrc = params.first_ssrc();
1638
1639 rtc::scoped_refptr<MediaStreamInterface> stream =
1640 remote_streams_->find(stream_label);
1641 if (!stream) {
1642 // This is a new MediaStream. Create a new remote MediaStream.
1643 stream = remote_stream_factory_->CreateMediaStream(stream_label);
1644 remote_streams_->AddStream(stream);
1645 new_streams->AddStream(stream);
1646 }
1647
1648 const TrackInfo* track_info =
1649 FindTrackInfo(*current_tracks, stream_label, track_id);
1650 if (!track_info) {
1651 current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc));
1652 OnRemoteTrackSeen(stream_label, track_id, ssrc, media_type);
1653 }
1654 }
deadbeefbda7e0b2015-12-08 17:13:40 -08001655
1656 // Add default track if necessary.
1657 if (default_track_needed) {
1658 rtc::scoped_refptr<MediaStreamInterface> default_stream =
1659 remote_streams_->find(kDefaultStreamLabel);
1660 if (!default_stream) {
1661 // Create the new default MediaStream.
1662 default_stream =
1663 remote_stream_factory_->CreateMediaStream(kDefaultStreamLabel);
1664 remote_streams_->AddStream(default_stream);
1665 new_streams->AddStream(default_stream);
1666 }
1667 std::string default_track_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
1668 ? kDefaultAudioTrackLabel
1669 : kDefaultVideoTrackLabel;
1670 const TrackInfo* default_track_info =
1671 FindTrackInfo(*current_tracks, kDefaultStreamLabel, default_track_id);
1672 if (!default_track_info) {
1673 current_tracks->push_back(
1674 TrackInfo(kDefaultStreamLabel, default_track_id, 0));
1675 OnRemoteTrackSeen(kDefaultStreamLabel, default_track_id, 0, media_type);
1676 }
1677 }
deadbeefab9b2d12015-10-14 11:33:11 -07001678}
1679
1680void PeerConnection::OnRemoteTrackSeen(const std::string& stream_label,
1681 const std::string& track_id,
1682 uint32_t ssrc,
1683 cricket::MediaType media_type) {
1684 MediaStreamInterface* stream = remote_streams_->find(stream_label);
1685
1686 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Tommif888bb52015-12-12 01:37:01 +01001687 AudioTrackInterface* audio_track = remote_stream_factory_->AddAudioTrack(
1688 ssrc, session_.get(), stream, track_id);
deadbeefab9b2d12015-10-14 11:33:11 -07001689 CreateAudioReceiver(stream, audio_track, ssrc);
1690 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
perkjf0dcfe22016-03-10 18:32:00 +01001691 CreateVideoReceiver(stream, track_id, ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -07001692 } else {
1693 RTC_DCHECK(false && "Invalid media type");
1694 }
1695}
1696
1697void PeerConnection::OnRemoteTrackRemoved(const std::string& stream_label,
1698 const std::string& track_id,
1699 cricket::MediaType media_type) {
1700 MediaStreamInterface* stream = remote_streams_->find(stream_label);
1701
1702 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1703 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1704 stream->FindAudioTrack(track_id);
1705 if (audio_track) {
1706 audio_track->set_state(webrtc::MediaStreamTrackInterface::kEnded);
1707 stream->RemoveTrack(audio_track);
1708 DestroyAudioReceiver(stream, audio_track);
1709 }
1710 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1711 rtc::scoped_refptr<VideoTrackInterface> video_track =
1712 stream->FindVideoTrack(track_id);
1713 if (video_track) {
deadbeefab9b2d12015-10-14 11:33:11 -07001714 stream->RemoveTrack(video_track);
perkjf0dcfe22016-03-10 18:32:00 +01001715 // Stopping or destroying a VideoRtpReceiver will end the
1716 // VideoRtpReceiver::track().
deadbeefab9b2d12015-10-14 11:33:11 -07001717 DestroyVideoReceiver(stream, video_track);
1718 }
1719 } else {
1720 ASSERT(false && "Invalid media type");
1721 }
1722}
1723
1724void PeerConnection::UpdateEndedRemoteMediaStreams() {
1725 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
1726 for (size_t i = 0; i < remote_streams_->count(); ++i) {
1727 MediaStreamInterface* stream = remote_streams_->at(i);
1728 if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
1729 streams_to_remove.push_back(stream);
1730 }
1731 }
1732
1733 for (const auto& stream : streams_to_remove) {
1734 remote_streams_->RemoveStream(stream);
1735 observer_->OnRemoveStream(stream);
1736 }
1737}
1738
deadbeefab9b2d12015-10-14 11:33:11 -07001739void PeerConnection::EndRemoteTracks(cricket::MediaType media_type) {
1740 TrackInfos* current_tracks = GetRemoteTracks(media_type);
1741 for (TrackInfos::iterator track_it = current_tracks->begin();
1742 track_it != current_tracks->end(); ++track_it) {
1743 const TrackInfo& info = *track_it;
1744 MediaStreamInterface* stream = remote_streams_->find(info.stream_label);
1745 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1746 AudioTrackInterface* track = stream->FindAudioTrack(info.track_id);
1747 // There's no guarantee the track is still available, e.g. the track may
1748 // have been removed from the stream by javascript.
1749 if (track) {
1750 track->set_state(webrtc::MediaStreamTrackInterface::kEnded);
1751 }
1752 }
1753 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1754 VideoTrackInterface* track = stream->FindVideoTrack(info.track_id);
1755 // There's no guarantee the track is still available, e.g. the track may
1756 // have been removed from the stream by javascript.
1757 if (track) {
1758 track->set_state(webrtc::MediaStreamTrackInterface::kEnded);
1759 }
1760 }
1761 }
1762}
1763
1764void PeerConnection::UpdateLocalTracks(
1765 const std::vector<cricket::StreamParams>& streams,
1766 cricket::MediaType media_type) {
1767 TrackInfos* current_tracks = GetLocalTracks(media_type);
1768
1769 // Find removed tracks. I.e., tracks where the track id, stream label or ssrc
1770 // don't match the new StreamParam.
1771 TrackInfos::iterator track_it = current_tracks->begin();
1772 while (track_it != current_tracks->end()) {
1773 const TrackInfo& info = *track_it;
1774 const cricket::StreamParams* params =
1775 cricket::GetStreamBySsrc(streams, info.ssrc);
1776 if (!params || params->id != info.track_id ||
1777 params->sync_label != info.stream_label) {
1778 OnLocalTrackRemoved(info.stream_label, info.track_id, info.ssrc,
1779 media_type);
1780 track_it = current_tracks->erase(track_it);
1781 } else {
1782 ++track_it;
1783 }
1784 }
1785
1786 // Find new and active tracks.
1787 for (const cricket::StreamParams& params : streams) {
1788 // The sync_label is the MediaStream label and the |stream.id| is the
1789 // track id.
1790 const std::string& stream_label = params.sync_label;
1791 const std::string& track_id = params.id;
1792 uint32_t ssrc = params.first_ssrc();
1793 const TrackInfo* track_info =
1794 FindTrackInfo(*current_tracks, stream_label, track_id);
1795 if (!track_info) {
1796 current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc));
1797 OnLocalTrackSeen(stream_label, track_id, params.first_ssrc(), media_type);
1798 }
1799 }
1800}
1801
1802void PeerConnection::OnLocalTrackSeen(const std::string& stream_label,
1803 const std::string& track_id,
1804 uint32_t ssrc,
1805 cricket::MediaType media_type) {
deadbeeffac06552015-11-25 11:26:01 -08001806 RtpSenderInterface* sender = FindSenderById(track_id);
1807 if (!sender) {
1808 LOG(LS_WARNING) << "An unknown RtpSender with id " << track_id
1809 << " has been configured in the local description.";
deadbeefab9b2d12015-10-14 11:33:11 -07001810 return;
1811 }
1812
deadbeeffac06552015-11-25 11:26:01 -08001813 if (sender->media_type() != media_type) {
1814 LOG(LS_WARNING) << "An RtpSender has been configured in the local"
1815 << " description with an unexpected media type.";
1816 return;
deadbeefab9b2d12015-10-14 11:33:11 -07001817 }
deadbeeffac06552015-11-25 11:26:01 -08001818
1819 sender->set_stream_id(stream_label);
1820 sender->SetSsrc(ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -07001821}
1822
1823void PeerConnection::OnLocalTrackRemoved(const std::string& stream_label,
1824 const std::string& track_id,
1825 uint32_t ssrc,
1826 cricket::MediaType media_type) {
deadbeeffac06552015-11-25 11:26:01 -08001827 RtpSenderInterface* sender = FindSenderById(track_id);
1828 if (!sender) {
1829 // This is the normal case. I.e., RemoveStream has been called and the
deadbeefab9b2d12015-10-14 11:33:11 -07001830 // SessionDescriptions has been renegotiated.
1831 return;
1832 }
deadbeeffac06552015-11-25 11:26:01 -08001833
1834 // A sender has been removed from the SessionDescription but it's still
1835 // associated with the PeerConnection. This only occurs if the SDP doesn't
1836 // match with the calls to CreateSender, AddStream and RemoveStream.
1837 if (sender->media_type() != media_type) {
1838 LOG(LS_WARNING) << "An RtpSender has been configured in the local"
1839 << " description with an unexpected media type.";
1840 return;
deadbeefab9b2d12015-10-14 11:33:11 -07001841 }
deadbeeffac06552015-11-25 11:26:01 -08001842
1843 sender->SetSsrc(0);
deadbeefab9b2d12015-10-14 11:33:11 -07001844}
1845
1846void PeerConnection::UpdateLocalRtpDataChannels(
1847 const cricket::StreamParamsVec& streams) {
1848 std::vector<std::string> existing_channels;
1849
1850 // Find new and active data channels.
1851 for (const cricket::StreamParams& params : streams) {
1852 // |it->sync_label| is actually the data channel label. The reason is that
1853 // we use the same naming of data channels as we do for
1854 // MediaStreams and Tracks.
1855 // For MediaStreams, the sync_label is the MediaStream label and the
1856 // track label is the same as |streamid|.
1857 const std::string& channel_label = params.sync_label;
1858 auto data_channel_it = rtp_data_channels_.find(channel_label);
1859 if (!VERIFY(data_channel_it != rtp_data_channels_.end())) {
1860 continue;
1861 }
1862 // Set the SSRC the data channel should use for sending.
1863 data_channel_it->second->SetSendSsrc(params.first_ssrc());
1864 existing_channels.push_back(data_channel_it->first);
1865 }
1866
1867 UpdateClosingRtpDataChannels(existing_channels, true);
1868}
1869
1870void PeerConnection::UpdateRemoteRtpDataChannels(
1871 const cricket::StreamParamsVec& streams) {
1872 std::vector<std::string> existing_channels;
1873
1874 // Find new and active data channels.
1875 for (const cricket::StreamParams& params : streams) {
1876 // The data channel label is either the mslabel or the SSRC if the mslabel
1877 // does not exist. Ex a=ssrc:444330170 mslabel:test1.
1878 std::string label = params.sync_label.empty()
1879 ? rtc::ToString(params.first_ssrc())
1880 : params.sync_label;
1881 auto data_channel_it = rtp_data_channels_.find(label);
1882 if (data_channel_it == rtp_data_channels_.end()) {
1883 // This is a new data channel.
1884 CreateRemoteRtpDataChannel(label, params.first_ssrc());
1885 } else {
1886 data_channel_it->second->SetReceiveSsrc(params.first_ssrc());
1887 }
1888 existing_channels.push_back(label);
1889 }
1890
1891 UpdateClosingRtpDataChannels(existing_channels, false);
1892}
1893
1894void PeerConnection::UpdateClosingRtpDataChannels(
1895 const std::vector<std::string>& active_channels,
1896 bool is_local_update) {
1897 auto it = rtp_data_channels_.begin();
1898 while (it != rtp_data_channels_.end()) {
1899 DataChannel* data_channel = it->second;
1900 if (std::find(active_channels.begin(), active_channels.end(),
1901 data_channel->label()) != active_channels.end()) {
1902 ++it;
1903 continue;
1904 }
1905
1906 if (is_local_update) {
1907 data_channel->SetSendSsrc(0);
1908 } else {
1909 data_channel->RemotePeerRequestClose();
1910 }
1911
1912 if (data_channel->state() == DataChannel::kClosed) {
1913 rtp_data_channels_.erase(it);
1914 it = rtp_data_channels_.begin();
1915 } else {
1916 ++it;
1917 }
1918 }
1919}
1920
1921void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label,
1922 uint32_t remote_ssrc) {
1923 rtc::scoped_refptr<DataChannel> channel(
1924 InternalCreateDataChannel(label, nullptr));
1925 if (!channel.get()) {
1926 LOG(LS_WARNING) << "Remote peer requested a DataChannel but"
1927 << "CreateDataChannel failed.";
1928 return;
1929 }
1930 channel->SetReceiveSsrc(remote_ssrc);
1931 observer_->OnDataChannel(
1932 DataChannelProxy::Create(signaling_thread(), channel));
1933}
1934
1935rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel(
1936 const std::string& label,
1937 const InternalDataChannelInit* config) {
1938 if (IsClosed()) {
1939 return nullptr;
1940 }
1941 if (session_->data_channel_type() == cricket::DCT_NONE) {
1942 LOG(LS_ERROR)
1943 << "InternalCreateDataChannel: Data is not supported in this call.";
1944 return nullptr;
1945 }
1946 InternalDataChannelInit new_config =
1947 config ? (*config) : InternalDataChannelInit();
1948 if (session_->data_channel_type() == cricket::DCT_SCTP) {
1949 if (new_config.id < 0) {
1950 rtc::SSLRole role;
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001951 if ((session_->GetSslRole(session_->data_channel(), &role)) &&
deadbeefab9b2d12015-10-14 11:33:11 -07001952 !sid_allocator_.AllocateSid(role, &new_config.id)) {
1953 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1954 return nullptr;
1955 }
1956 } else if (!sid_allocator_.ReserveSid(new_config.id)) {
1957 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1958 << "because the id is already in use or out of range.";
1959 return nullptr;
1960 }
1961 }
1962
1963 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
1964 session_.get(), session_->data_channel_type(), label, new_config));
1965 if (!channel) {
1966 sid_allocator_.ReleaseSid(new_config.id);
1967 return nullptr;
1968 }
1969
1970 if (channel->data_channel_type() == cricket::DCT_RTP) {
1971 if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) {
1972 LOG(LS_ERROR) << "DataChannel with label " << channel->label()
1973 << " already exists.";
1974 return nullptr;
1975 }
1976 rtp_data_channels_[channel->label()] = channel;
1977 } else {
1978 RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP);
1979 sctp_data_channels_.push_back(channel);
1980 channel->SignalClosed.connect(this,
1981 &PeerConnection::OnSctpDataChannelClosed);
1982 }
1983
1984 return channel;
1985}
1986
1987bool PeerConnection::HasDataChannels() const {
1988 return !rtp_data_channels_.empty() || !sctp_data_channels_.empty();
1989}
1990
1991void PeerConnection::AllocateSctpSids(rtc::SSLRole role) {
1992 for (const auto& channel : sctp_data_channels_) {
1993 if (channel->id() < 0) {
1994 int sid;
1995 if (!sid_allocator_.AllocateSid(role, &sid)) {
1996 LOG(LS_ERROR) << "Failed to allocate SCTP sid.";
1997 continue;
1998 }
1999 channel->SetSctpSid(sid);
2000 }
2001 }
2002}
2003
2004void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) {
deadbeefbd292462015-12-14 18:15:29 -08002005 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002006 for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end();
2007 ++it) {
2008 if (it->get() == channel) {
2009 if (channel->id() >= 0) {
2010 sid_allocator_.ReleaseSid(channel->id());
2011 }
deadbeefbd292462015-12-14 18:15:29 -08002012 // Since this method is triggered by a signal from the DataChannel,
2013 // we can't free it directly here; we need to free it asynchronously.
2014 sctp_data_channels_to_free_.push_back(*it);
deadbeefab9b2d12015-10-14 11:33:11 -07002015 sctp_data_channels_.erase(it);
deadbeefbd292462015-12-14 18:15:29 -08002016 signaling_thread()->Post(this, MSG_FREE_DATACHANNELS, nullptr);
deadbeefab9b2d12015-10-14 11:33:11 -07002017 return;
2018 }
2019 }
2020}
2021
2022void PeerConnection::OnVoiceChannelDestroyed() {
2023 EndRemoteTracks(cricket::MEDIA_TYPE_AUDIO);
2024}
2025
2026void PeerConnection::OnVideoChannelDestroyed() {
2027 EndRemoteTracks(cricket::MEDIA_TYPE_VIDEO);
2028}
2029
2030void PeerConnection::OnDataChannelCreated() {
2031 for (const auto& channel : sctp_data_channels_) {
2032 channel->OnTransportChannelCreated();
2033 }
2034}
2035
2036void PeerConnection::OnDataChannelDestroyed() {
2037 // Use a temporary copy of the RTP/SCTP DataChannel list because the
2038 // DataChannel may callback to us and try to modify the list.
2039 std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs;
2040 temp_rtp_dcs.swap(rtp_data_channels_);
2041 for (const auto& kv : temp_rtp_dcs) {
2042 kv.second->OnTransportChannelDestroyed();
2043 }
2044
2045 std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs;
2046 temp_sctp_dcs.swap(sctp_data_channels_);
2047 for (const auto& channel : temp_sctp_dcs) {
2048 channel->OnTransportChannelDestroyed();
2049 }
2050}
2051
2052void PeerConnection::OnDataChannelOpenMessage(
2053 const std::string& label,
2054 const InternalDataChannelInit& config) {
2055 rtc::scoped_refptr<DataChannel> channel(
2056 InternalCreateDataChannel(label, &config));
2057 if (!channel.get()) {
2058 LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message.";
2059 return;
2060 }
2061
2062 observer_->OnDataChannel(
2063 DataChannelProxy::Create(signaling_thread(), channel));
2064}
2065
deadbeeffac06552015-11-25 11:26:01 -08002066RtpSenderInterface* PeerConnection::FindSenderById(const std::string& id) {
2067 auto it =
2068 std::find_if(senders_.begin(), senders_.end(),
2069 [id](const rtc::scoped_refptr<RtpSenderInterface>& sender) {
2070 return sender->id() == id;
2071 });
2072 return it != senders_.end() ? it->get() : nullptr;
2073}
2074
deadbeef70ab1a12015-09-28 16:53:55 -07002075std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
2076PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) {
2077 return std::find_if(
2078 senders_.begin(), senders_.end(),
2079 [track](const rtc::scoped_refptr<RtpSenderInterface>& sender) {
2080 return sender->track() == track;
2081 });
2082}
2083
2084std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
2085PeerConnection::FindReceiverForTrack(MediaStreamTrackInterface* track) {
2086 return std::find_if(
2087 receivers_.begin(), receivers_.end(),
2088 [track](const rtc::scoped_refptr<RtpReceiverInterface>& receiver) {
2089 return receiver->track() == track;
2090 });
2091}
2092
deadbeefab9b2d12015-10-14 11:33:11 -07002093PeerConnection::TrackInfos* PeerConnection::GetRemoteTracks(
2094 cricket::MediaType media_type) {
2095 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
2096 media_type == cricket::MEDIA_TYPE_VIDEO);
2097 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &remote_audio_tracks_
2098 : &remote_video_tracks_;
2099}
2100
2101PeerConnection::TrackInfos* PeerConnection::GetLocalTracks(
2102 cricket::MediaType media_type) {
2103 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
2104 media_type == cricket::MEDIA_TYPE_VIDEO);
2105 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_tracks_
2106 : &local_video_tracks_;
2107}
2108
2109const PeerConnection::TrackInfo* PeerConnection::FindTrackInfo(
2110 const PeerConnection::TrackInfos& infos,
2111 const std::string& stream_label,
2112 const std::string track_id) const {
2113 for (const TrackInfo& track_info : infos) {
2114 if (track_info.stream_label == stream_label &&
2115 track_info.track_id == track_id) {
2116 return &track_info;
2117 }
2118 }
2119 return nullptr;
2120}
2121
2122DataChannel* PeerConnection::FindDataChannelBySid(int sid) const {
2123 for (const auto& channel : sctp_data_channels_) {
2124 if (channel->id() == sid) {
2125 return channel;
2126 }
2127 }
2128 return nullptr;
2129}
2130
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131} // namespace webrtc