blob: a076f3ca3a67996889120c639bec7966e81e33a8 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include "talk/session/media/channelmanager.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/audiotrack.h"
20#include "webrtc/api/dtmfsender.h"
21#include "webrtc/api/jsepicecandidate.h"
22#include "webrtc/api/jsepsessiondescription.h"
23#include "webrtc/api/mediaconstraintsinterface.h"
24#include "webrtc/api/mediastream.h"
25#include "webrtc/api/mediastreamobserver.h"
26#include "webrtc/api/mediastreamproxy.h"
27#include "webrtc/api/mediastreamtrackproxy.h"
28#include "webrtc/api/remoteaudiosource.h"
29#include "webrtc/api/remotevideocapturer.h"
30#include "webrtc/api/rtpreceiver.h"
31#include "webrtc/api/rtpsender.h"
32#include "webrtc/api/streamcollection.h"
33#include "webrtc/api/videosource.h"
34#include "webrtc/api/videotrack.h"
tfarina5237aaf2015-11-10 23:44:30 -080035#include "webrtc/base/arraysize.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000036#include "webrtc/base/logging.h"
37#include "webrtc/base/stringencode.h"
deadbeefab9b2d12015-10-14 11:33:11 -070038#include "webrtc/base/stringutils.h"
Peter Boström1a9d6152015-12-08 22:15:17 +010039#include "webrtc/base/trace_event.h"
kjellandera96e2d72016-02-04 23:52:28 -080040#include "webrtc/media/sctp/sctpdataengine.h"
tfarina5237aaf2015-11-10 23:44:30 -080041#include "webrtc/p2p/client/basicportallocator.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:
384 explicit RemoteMediaStreamFactory(rtc::Thread* signaling_thread,
385 cricket::ChannelManager* channel_manager)
386 : signaling_thread_(signaling_thread),
387 channel_manager_(channel_manager) {}
388
389 rtc::scoped_refptr<MediaStreamInterface> CreateMediaStream(
390 const std::string& stream_label) {
391 return MediaStreamProxy::Create(signaling_thread_,
392 MediaStream::Create(stream_label));
393 }
394
Tommif888bb52015-12-12 01:37:01 +0100395 AudioTrackInterface* AddAudioTrack(uint32_t ssrc,
396 AudioProviderInterface* provider,
397 webrtc::MediaStreamInterface* stream,
deadbeefab9b2d12015-10-14 11:33:11 -0700398 const std::string& track_id) {
tommi6eca7e32015-12-15 04:27:11 -0800399 return AddTrack<AudioTrackInterface, AudioTrack, AudioTrackProxy>(
Tommif888bb52015-12-12 01:37:01 +0100400 stream, track_id, RemoteAudioSource::Create(ssrc, provider));
deadbeefab9b2d12015-10-14 11:33:11 -0700401 }
402
403 VideoTrackInterface* AddVideoTrack(webrtc::MediaStreamInterface* stream,
404 const std::string& track_id) {
405 return AddTrack<VideoTrackInterface, VideoTrack, VideoTrackProxy>(
406 stream, track_id,
407 VideoSource::Create(channel_manager_, new RemoteVideoCapturer(),
tommi6eca7e32015-12-15 04:27:11 -0800408 nullptr, true)
deadbeefab9b2d12015-10-14 11:33:11 -0700409 .get());
410 }
411
412 private:
413 template <typename TI, typename T, typename TP, typename S>
414 TI* AddTrack(MediaStreamInterface* stream,
415 const std::string& track_id,
Tommif888bb52015-12-12 01:37:01 +0100416 const S& source) {
deadbeefab9b2d12015-10-14 11:33:11 -0700417 rtc::scoped_refptr<TI> track(
418 TP::Create(signaling_thread_, T::Create(track_id, source)));
419 track->set_state(webrtc::MediaStreamTrackInterface::kLive);
420 if (stream->AddTrack(track)) {
421 return track;
422 }
423 return nullptr;
424 }
425
426 rtc::Thread* signaling_thread_;
427 cricket::ChannelManager* channel_manager_;
428};
429
430bool ConvertRtcOptionsForOffer(
431 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
432 cricket::MediaSessionOptions* session_options) {
433 typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
434 if (!IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) ||
435 !IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video)) {
436 return false;
437 }
438
deadbeefc80741f2015-10-22 13:14:45 -0700439 if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) {
deadbeefab9b2d12015-10-14 11:33:11 -0700440 session_options->recv_audio = (rtc_options.offer_to_receive_audio > 0);
441 }
deadbeefc80741f2015-10-22 13:14:45 -0700442 if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) {
deadbeefab9b2d12015-10-14 11:33:11 -0700443 session_options->recv_video = (rtc_options.offer_to_receive_video > 0);
444 }
445
446 session_options->vad_enabled = rtc_options.voice_activity_detection;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800447 session_options->audio_transport_options.ice_restart =
448 rtc_options.ice_restart;
449 session_options->video_transport_options.ice_restart =
450 rtc_options.ice_restart;
451 session_options->data_transport_options.ice_restart = rtc_options.ice_restart;
deadbeefc80741f2015-10-22 13:14:45 -0700452 session_options->bundle_enabled = rtc_options.use_rtp_mux;
deadbeefab9b2d12015-10-14 11:33:11 -0700453
454 return true;
455}
456
457bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints,
458 cricket::MediaSessionOptions* session_options) {
459 bool value = false;
460 size_t mandatory_constraints_satisfied = 0;
461
462 // kOfferToReceiveAudio defaults to true according to spec.
463 if (!FindConstraint(constraints,
464 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
465 &mandatory_constraints_satisfied) ||
466 value) {
467 session_options->recv_audio = true;
468 }
469
470 // kOfferToReceiveVideo defaults to false according to spec. But
471 // if it is an answer and video is offered, we should still accept video
472 // per default.
473 value = false;
474 if (!FindConstraint(constraints,
475 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
476 &mandatory_constraints_satisfied) ||
477 value) {
478 session_options->recv_video = true;
479 }
480
481 if (FindConstraint(constraints,
482 MediaConstraintsInterface::kVoiceActivityDetection, &value,
483 &mandatory_constraints_satisfied)) {
484 session_options->vad_enabled = value;
485 }
486
487 if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value,
488 &mandatory_constraints_satisfied)) {
489 session_options->bundle_enabled = value;
490 } else {
491 // kUseRtpMux defaults to true according to spec.
492 session_options->bundle_enabled = true;
493 }
deadbeefab9b2d12015-10-14 11:33:11 -0700494
495 if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart,
496 &value, &mandatory_constraints_satisfied)) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800497 session_options->audio_transport_options.ice_restart = value;
498 session_options->video_transport_options.ice_restart = value;
499 session_options->data_transport_options.ice_restart = value;
deadbeefab9b2d12015-10-14 11:33:11 -0700500 } else {
501 // kIceRestart defaults to false according to spec.
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800502 session_options->audio_transport_options.ice_restart = false;
503 session_options->video_transport_options.ice_restart = false;
504 session_options->data_transport_options.ice_restart = false;
deadbeefab9b2d12015-10-14 11:33:11 -0700505 }
506
507 if (!constraints) {
508 return true;
509 }
510 return mandatory_constraints_satisfied == constraints->GetMandatory().size();
511}
512
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200513bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800514 cricket::ServerAddresses* stun_servers,
515 std::vector<cricket::RelayServerConfig>* turn_servers) {
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200516 for (const webrtc::PeerConnectionInterface::IceServer& server : servers) {
517 if (!server.urls.empty()) {
518 for (const std::string& url : server.urls) {
Joachim Bauchd935f912015-05-29 22:14:21 +0200519 if (url.empty()) {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700520 LOG(LS_ERROR) << "Empty uri.";
521 return false;
Joachim Bauchd935f912015-05-29 22:14:21 +0200522 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800523 if (!ParseIceServerUrl(server, url, stun_servers, turn_servers)) {
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200524 return false;
525 }
526 }
527 } else if (!server.uri.empty()) {
528 // Fallback to old .uri if new .urls isn't present.
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800529 if (!ParseIceServerUrl(server, server.uri, stun_servers, turn_servers)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 return false;
Joachim Bauch7c4e7452015-05-28 23:06:30 +0200531 }
532 } else {
deadbeef0a6c4ca2015-10-06 11:38:28 -0700533 LOG(LS_ERROR) << "Empty uri.";
534 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 }
536 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800537 // Candidates must have unique priorities, so that connectivity checks
538 // are performed in a well-defined order.
539 int priority = static_cast<int>(turn_servers->size() - 1);
540 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
541 // First in the list gets highest priority.
542 turn_server.priority = priority--;
543 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 return true;
545}
546
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547PeerConnection::PeerConnection(PeerConnectionFactory* factory)
548 : factory_(factory),
549 observer_(NULL),
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000550 uma_observer_(NULL),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 signaling_state_(kStable),
552 ice_state_(kIceNew),
553 ice_connection_state_(kIceConnectionNew),
deadbeefab9b2d12015-10-14 11:33:11 -0700554 ice_gathering_state_(kIceGatheringNew),
555 local_streams_(StreamCollection::Create()),
556 remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
558PeerConnection::~PeerConnection() {
Peter Boström1a9d6152015-12-08 22:15:17 +0100559 TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection");
deadbeef0a6c4ca2015-10-06 11:38:28 -0700560 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef70ab1a12015-09-28 16:53:55 -0700561 // Need to detach RTP senders/receivers from WebRtcSession,
562 // since it's about to be destroyed.
563 for (const auto& sender : senders_) {
564 sender->Stop();
565 }
566 for (const auto& receiver : receivers_) {
567 receiver->Stop();
568 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569}
570
571bool PeerConnection::Initialize(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000572 const PeerConnectionInterface::RTCConfiguration& configuration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 const MediaConstraintsInterface* constraints,
deadbeef653b8e02015-11-11 12:55:10 -0800574 rtc::scoped_ptr<cricket::PortAllocator> allocator,
575 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
576 PeerConnectionObserver* observer) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100577 TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
deadbeef653b8e02015-11-11 12:55:10 -0800578 RTC_DCHECK(observer != nullptr);
579 if (!observer) {
580 return false;
581 }
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000582 observer_ = observer;
583
kwiberg0eb15ed2015-12-17 03:04:15 -0800584 port_allocator_ = std::move(allocator);
deadbeef653b8e02015-11-11 12:55:10 -0800585
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800586 cricket::ServerAddresses stun_servers;
587 std::vector<cricket::RelayServerConfig> turn_servers;
588 if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 return false;
590 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800591 port_allocator_->SetIceServers(stun_servers, turn_servers);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000592
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 // To handle both internal and externally created port allocator, we will
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000594 // enable BUNDLE here.
braveyao@webrtc.org1732df62014-10-27 03:01:37 +0000595 int portallocator_flags = port_allocator_->flags();
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700596 portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000597 cricket::PORTALLOCATOR_ENABLE_IPV6;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000598 bool value;
guoweis@webrtc.org97ed3932014-09-19 21:06:12 +0000599 // If IPv6 flag was specified, we'll not override it by experiment.
deadbeefab9b2d12015-10-14 11:33:11 -0700600 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6,
601 &value, nullptr)) {
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000602 if (!value) {
603 portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
guoweis@webrtc.org97ed3932014-09-19 21:06:12 +0000604 }
guoweis@webrtc.org2c1bcea2014-09-23 16:23:02 +0000605 } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") ==
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000606 "Disabled") {
607 portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000608 }
609
Jiayang Liucac1b382015-04-30 12:35:24 -0700610 if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
611 portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
612 LOG(LS_INFO) << "TCP candidates are disabled.";
613 }
614
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000615 port_allocator_->set_flags(portallocator_flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 // No step delay is used while allocating ports.
617 port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
618
stefanc1aeaf02015-10-15 07:26:07 -0700619 media_controller_.reset(factory_->CreateMediaController());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620
stefanc1aeaf02015-10-15 07:26:07 -0700621 remote_stream_factory_.reset(new RemoteMediaStreamFactory(
622 factory_->signaling_thread(), media_controller_->channel_manager()));
623
624 session_.reset(
625 new WebRtcSession(media_controller_.get(), factory_->signaling_thread(),
626 factory_->worker_thread(), port_allocator_.get()));
deadbeefab9b2d12015-10-14 11:33:11 -0700627 stats_.reset(new StatsCollector(this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628
629 // Initialize the WebRtcSession. It creates transport channels etc.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000630 if (!session_->Initialize(factory_->options(), constraints,
kwiberg0eb15ed2015-12-17 03:04:15 -0800631 std::move(dtls_identity_store), configuration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 return false;
deadbeefab9b2d12015-10-14 11:33:11 -0700633 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 // Register PeerConnection as receiver of local ice candidates.
636 // All the callbacks will be posted to the application from PeerConnection.
637 session_->RegisterIceObserver(this);
638 session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange);
deadbeefab9b2d12015-10-14 11:33:11 -0700639 session_->SignalVoiceChannelDestroyed.connect(
640 this, &PeerConnection::OnVoiceChannelDestroyed);
641 session_->SignalVideoChannelDestroyed.connect(
642 this, &PeerConnection::OnVideoChannelDestroyed);
643 session_->SignalDataChannelCreated.connect(
644 this, &PeerConnection::OnDataChannelCreated);
645 session_->SignalDataChannelDestroyed.connect(
646 this, &PeerConnection::OnDataChannelDestroyed);
647 session_->SignalDataChannelOpenMessage.connect(
648 this, &PeerConnection::OnDataChannelOpenMessage);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 return true;
650}
651
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000652rtc::scoped_refptr<StreamCollectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653PeerConnection::local_streams() {
deadbeefab9b2d12015-10-14 11:33:11 -0700654 return local_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655}
656
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000657rtc::scoped_refptr<StreamCollectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658PeerConnection::remote_streams() {
deadbeefab9b2d12015-10-14 11:33:11 -0700659 return remote_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660}
661
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000662bool PeerConnection::AddStream(MediaStreamInterface* local_stream) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100663 TRACE_EVENT0("webrtc", "PeerConnection::AddStream");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 if (IsClosed()) {
665 return false;
666 }
deadbeefab9b2d12015-10-14 11:33:11 -0700667 if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 return false;
669 }
deadbeefab9b2d12015-10-14 11:33:11 -0700670
671 local_streams_->AddStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -0800672 MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
673 observer->SignalAudioTrackAdded.connect(this,
674 &PeerConnection::OnAudioTrackAdded);
675 observer->SignalAudioTrackRemoved.connect(
676 this, &PeerConnection::OnAudioTrackRemoved);
677 observer->SignalVideoTrackAdded.connect(this,
678 &PeerConnection::OnVideoTrackAdded);
679 observer->SignalVideoTrackRemoved.connect(
680 this, &PeerConnection::OnVideoTrackRemoved);
681 stream_observers_.push_back(rtc::scoped_ptr<MediaStreamObserver>(observer));
deadbeefab9b2d12015-10-14 11:33:11 -0700682
deadbeefab9b2d12015-10-14 11:33:11 -0700683 for (const auto& track : local_stream->GetAudioTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800684 OnAudioTrackAdded(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700685 }
686 for (const auto& track : local_stream->GetVideoTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800687 OnVideoTrackAdded(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700688 }
689
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000690 stats_->AddStream(local_stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 observer_->OnRenegotiationNeeded();
692 return true;
693}
694
695void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100696 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
deadbeefab9b2d12015-10-14 11:33:11 -0700697 for (const auto& track : local_stream->GetAudioTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800698 OnAudioTrackRemoved(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700699 }
700 for (const auto& track : local_stream->GetVideoTracks()) {
deadbeefeb459812015-12-15 19:24:43 -0800701 OnVideoTrackRemoved(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -0700702 }
703
704 local_streams_->RemoveStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -0800705 stream_observers_.erase(
706 std::remove_if(
707 stream_observers_.begin(), stream_observers_.end(),
708 [local_stream](const rtc::scoped_ptr<MediaStreamObserver>& observer) {
709 return observer->stream()->label().compare(local_stream->label()) ==
710 0;
711 }),
712 stream_observers_.end());
deadbeefab9b2d12015-10-14 11:33:11 -0700713
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 if (IsClosed()) {
715 return;
716 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 observer_->OnRenegotiationNeeded();
718}
719
deadbeefe1f9d832016-01-14 15:35:42 -0800720rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack(
721 MediaStreamTrackInterface* track,
722 std::vector<MediaStreamInterface*> streams) {
723 TRACE_EVENT0("webrtc", "PeerConnection::AddTrack");
724 if (IsClosed()) {
725 return nullptr;
726 }
727 if (streams.size() >= 2) {
728 LOG(LS_ERROR)
729 << "Adding a track with two streams is not currently supported.";
730 return nullptr;
731 }
732 // TODO(deadbeef): Support adding a track to two different senders.
733 if (FindSenderForTrack(track) != senders_.end()) {
734 LOG(LS_ERROR) << "Sender for track " << track->id() << " already exists.";
735 return nullptr;
736 }
737
738 // TODO(deadbeef): Support adding a track to multiple streams.
739 rtc::scoped_refptr<RtpSenderInterface> new_sender;
740 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
741 new_sender = RtpSenderProxy::Create(
742 signaling_thread(),
743 new AudioRtpSender(static_cast<AudioTrackInterface*>(track),
744 session_.get(), stats_.get()));
745 if (!streams.empty()) {
746 new_sender->set_stream_id(streams[0]->label());
747 }
748 const TrackInfo* track_info = FindTrackInfo(
749 local_audio_tracks_, new_sender->stream_id(), track->id());
750 if (track_info) {
751 new_sender->SetSsrc(track_info->ssrc);
752 }
753 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
754 new_sender = RtpSenderProxy::Create(
755 signaling_thread(),
756 new VideoRtpSender(static_cast<VideoTrackInterface*>(track),
757 session_.get()));
758 if (!streams.empty()) {
759 new_sender->set_stream_id(streams[0]->label());
760 }
761 const TrackInfo* track_info = FindTrackInfo(
762 local_video_tracks_, new_sender->stream_id(), track->id());
763 if (track_info) {
764 new_sender->SetSsrc(track_info->ssrc);
765 }
766 } else {
767 LOG(LS_ERROR) << "CreateSender called with invalid kind: " << track->kind();
768 return rtc::scoped_refptr<RtpSenderInterface>();
769 }
770
771 senders_.push_back(new_sender);
772 observer_->OnRenegotiationNeeded();
773 return new_sender;
774}
775
776bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) {
777 TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack");
778 if (IsClosed()) {
779 return false;
780 }
781
782 auto it = std::find(senders_.begin(), senders_.end(), sender);
783 if (it == senders_.end()) {
784 LOG(LS_ERROR) << "Couldn't find sender " << sender->id() << " to remove.";
785 return false;
786 }
787 (*it)->Stop();
788 senders_.erase(it);
789
790 observer_->OnRenegotiationNeeded();
791 return true;
792}
793
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000794rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 AudioTrackInterface* track) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100796 TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 if (!track) {
798 LOG(LS_ERROR) << "CreateDtmfSender - track is NULL.";
799 return NULL;
800 }
deadbeefab9b2d12015-10-14 11:33:11 -0700801 if (!local_streams_->FindAudioTrack(track->id())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track.";
803 return NULL;
804 }
805
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 rtc::scoped_refptr<DtmfSenderInterface> sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 DtmfSender::Create(track, signaling_thread(), session_.get()));
808 if (!sender.get()) {
809 LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create.";
810 return NULL;
811 }
812 return DtmfSenderProxy::Create(signaling_thread(), sender.get());
813}
814
deadbeeffac06552015-11-25 11:26:01 -0800815rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800816 const std::string& kind,
817 const std::string& stream_id) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100818 TRACE_EVENT0("webrtc", "PeerConnection::CreateSender");
deadbeefe1f9d832016-01-14 15:35:42 -0800819 rtc::scoped_refptr<RtpSenderInterface> new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800820 if (kind == MediaStreamTrackInterface::kAudioKind) {
deadbeefe1f9d832016-01-14 15:35:42 -0800821 new_sender = RtpSenderProxy::Create(
822 signaling_thread(), new AudioRtpSender(session_.get(), stats_.get()));
deadbeeffac06552015-11-25 11:26:01 -0800823 } else if (kind == MediaStreamTrackInterface::kVideoKind) {
deadbeefe1f9d832016-01-14 15:35:42 -0800824 new_sender = RtpSenderProxy::Create(signaling_thread(),
825 new VideoRtpSender(session_.get()));
deadbeeffac06552015-11-25 11:26:01 -0800826 } else {
827 LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind;
deadbeefe1f9d832016-01-14 15:35:42 -0800828 return new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800829 }
deadbeefbd7d8f72015-12-18 16:58:44 -0800830 if (!stream_id.empty()) {
831 new_sender->set_stream_id(stream_id);
832 }
deadbeeffac06552015-11-25 11:26:01 -0800833 senders_.push_back(new_sender);
deadbeefe1f9d832016-01-14 15:35:42 -0800834 return new_sender;
deadbeeffac06552015-11-25 11:26:01 -0800835}
836
deadbeef70ab1a12015-09-28 16:53:55 -0700837std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders()
838 const {
deadbeefe1f9d832016-01-14 15:35:42 -0800839 return senders_;
deadbeef70ab1a12015-09-28 16:53:55 -0700840}
841
842std::vector<rtc::scoped_refptr<RtpReceiverInterface>>
843PeerConnection::GetReceivers() const {
deadbeefe1f9d832016-01-14 15:35:42 -0800844 return receivers_;
deadbeef70ab1a12015-09-28 16:53:55 -0700845}
846
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847bool PeerConnection::GetStats(StatsObserver* observer,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000848 MediaStreamTrackInterface* track,
849 StatsOutputLevel level) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100850 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
deadbeef0a6c4ca2015-10-06 11:38:28 -0700851 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 if (!VERIFY(observer != NULL)) {
853 LOG(LS_ERROR) << "GetStats - observer is NULL.";
854 return false;
855 }
856
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000857 stats_->UpdateStats(level);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000858 signaling_thread()->Post(this, MSG_GETSTATS,
859 new GetStatsMsg(observer, track));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 return true;
861}
862
863PeerConnectionInterface::SignalingState PeerConnection::signaling_state() {
864 return signaling_state_;
865}
866
867PeerConnectionInterface::IceState PeerConnection::ice_state() {
868 return ice_state_;
869}
870
871PeerConnectionInterface::IceConnectionState
872PeerConnection::ice_connection_state() {
873 return ice_connection_state_;
874}
875
876PeerConnectionInterface::IceGatheringState
877PeerConnection::ice_gathering_state() {
878 return ice_gathering_state_;
879}
880
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000881rtc::scoped_refptr<DataChannelInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882PeerConnection::CreateDataChannel(
883 const std::string& label,
884 const DataChannelInit* config) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100885 TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel");
deadbeefab9b2d12015-10-14 11:33:11 -0700886 bool first_datachannel = !HasDataChannels();
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000887
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000888 rtc::scoped_ptr<InternalDataChannelInit> internal_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000889 if (config) {
890 internal_config.reset(new InternalDataChannelInit(*config));
891 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000892 rtc::scoped_refptr<DataChannelInterface> channel(
deadbeefab9b2d12015-10-14 11:33:11 -0700893 InternalCreateDataChannel(label, internal_config.get()));
894 if (!channel.get()) {
895 return nullptr;
896 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000898 // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or
899 // the first SCTP DataChannel.
900 if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) {
901 observer_->OnRenegotiationNeeded();
902 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000903
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 return DataChannelProxy::Create(signaling_thread(), channel.get());
905}
906
907void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
908 const MediaConstraintsInterface* constraints) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100909 TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer");
deadbeefab9b2d12015-10-14 11:33:11 -0700910 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
912 return;
913 }
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000914 RTCOfferAnswerOptions options;
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000915
916 bool value;
917 size_t mandatory_constraints = 0;
918
919 if (FindConstraint(constraints,
920 MediaConstraintsInterface::kOfferToReceiveAudio,
921 &value,
922 &mandatory_constraints)) {
923 options.offer_to_receive_audio =
924 value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0;
925 }
926
927 if (FindConstraint(constraints,
928 MediaConstraintsInterface::kOfferToReceiveVideo,
929 &value,
930 &mandatory_constraints)) {
931 options.offer_to_receive_video =
932 value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0;
933 }
934
935 if (FindConstraint(constraints,
936 MediaConstraintsInterface::kVoiceActivityDetection,
937 &value,
938 &mandatory_constraints)) {
939 options.voice_activity_detection = value;
940 }
941
942 if (FindConstraint(constraints,
943 MediaConstraintsInterface::kIceRestart,
944 &value,
945 &mandatory_constraints)) {
946 options.ice_restart = value;
947 }
948
949 if (FindConstraint(constraints,
950 MediaConstraintsInterface::kUseRtpMux,
951 &value,
952 &mandatory_constraints)) {
953 options.use_rtp_mux = value;
954 }
955
956 CreateOffer(observer, options);
957}
958
959void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
960 const RTCOfferAnswerOptions& options) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100961 TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer");
deadbeefab9b2d12015-10-14 11:33:11 -0700962 if (!VERIFY(observer != nullptr)) {
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000963 LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
964 return;
965 }
deadbeefab9b2d12015-10-14 11:33:11 -0700966
967 cricket::MediaSessionOptions session_options;
968 if (!GetOptionsForOffer(options, &session_options)) {
969 std::string error = "CreateOffer called with invalid options.";
970 LOG(LS_ERROR) << error;
971 PostCreateSessionDescriptionFailure(observer, error);
972 return;
973 }
974
975 session_->CreateOffer(observer, options, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976}
977
978void PeerConnection::CreateAnswer(
979 CreateSessionDescriptionObserver* observer,
980 const MediaConstraintsInterface* constraints) {
Peter Boström1a9d6152015-12-08 22:15:17 +0100981 TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer");
deadbeefab9b2d12015-10-14 11:33:11 -0700982 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
984 return;
985 }
deadbeefab9b2d12015-10-14 11:33:11 -0700986
987 cricket::MediaSessionOptions session_options;
988 if (!GetOptionsForAnswer(constraints, &session_options)) {
989 std::string error = "CreateAnswer called with invalid constraints.";
990 LOG(LS_ERROR) << error;
991 PostCreateSessionDescriptionFailure(observer, error);
992 return;
993 }
994
995 session_->CreateAnswer(observer, constraints, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996}
997
998void PeerConnection::SetLocalDescription(
999 SetSessionDescriptionObserver* observer,
1000 SessionDescriptionInterface* desc) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001001 TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription");
deadbeefab9b2d12015-10-14 11:33:11 -07001002 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
1004 return;
1005 }
1006 if (!desc) {
1007 PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL.");
1008 return;
1009 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 // Update stats here so that we have the most recent stats for tracks and
1011 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001012 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 std::string error;
1014 if (!session_->SetLocalDescription(desc, &error)) {
1015 PostSetSessionDescriptionFailure(observer, error);
1016 return;
1017 }
deadbeefab9b2d12015-10-14 11:33:11 -07001018
1019 // If setting the description decided our SSL role, allocate any necessary
1020 // SCTP sids.
1021 rtc::SSLRole role;
1022 if (session_->data_channel_type() == cricket::DCT_SCTP &&
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001023 session_->GetSslRole(session_->data_channel(), &role)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001024 AllocateSctpSids(role);
1025 }
1026
1027 // Update state and SSRC of local MediaStreams and DataChannels based on the
1028 // local session description.
1029 const cricket::ContentInfo* audio_content =
1030 GetFirstAudioContent(desc->description());
1031 if (audio_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001032 if (audio_content->rejected) {
1033 RemoveTracks(cricket::MEDIA_TYPE_AUDIO);
1034 } else {
1035 const cricket::AudioContentDescription* audio_desc =
1036 static_cast<const cricket::AudioContentDescription*>(
1037 audio_content->description);
1038 UpdateLocalTracks(audio_desc->streams(), audio_desc->type());
1039 }
deadbeefab9b2d12015-10-14 11:33:11 -07001040 }
1041
1042 const cricket::ContentInfo* video_content =
1043 GetFirstVideoContent(desc->description());
1044 if (video_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001045 if (video_content->rejected) {
1046 RemoveTracks(cricket::MEDIA_TYPE_VIDEO);
1047 } else {
1048 const cricket::VideoContentDescription* video_desc =
1049 static_cast<const cricket::VideoContentDescription*>(
1050 video_content->description);
1051 UpdateLocalTracks(video_desc->streams(), video_desc->type());
1052 }
deadbeefab9b2d12015-10-14 11:33:11 -07001053 }
1054
1055 const cricket::ContentInfo* data_content =
1056 GetFirstDataContent(desc->description());
1057 if (data_content) {
1058 const cricket::DataContentDescription* data_desc =
1059 static_cast<const cricket::DataContentDescription*>(
1060 data_content->description);
1061 if (rtc::starts_with(data_desc->protocol().data(),
1062 cricket::kMediaProtocolRtpPrefix)) {
1063 UpdateLocalRtpDataChannels(data_desc->streams());
1064 }
1065 }
1066
1067 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
deadbeefab9b2d12015-10-14 11:33:11 -07001069
deadbeefcbecd352015-09-23 11:50:27 -07001070 // MaybeStartGathering needs to be called after posting
1071 // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates
1072 // before signaling that SetLocalDescription completed.
1073 session_->MaybeStartGathering();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074}
1075
1076void PeerConnection::SetRemoteDescription(
1077 SetSessionDescriptionObserver* observer,
1078 SessionDescriptionInterface* desc) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001079 TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription");
deadbeefab9b2d12015-10-14 11:33:11 -07001080 if (!VERIFY(observer != nullptr)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
1082 return;
1083 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 if (!desc) {
1085 PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL.");
1086 return;
1087 }
1088 // Update stats here so that we have the most recent stats for tracks and
1089 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001090 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 std::string error;
1092 if (!session_->SetRemoteDescription(desc, &error)) {
1093 PostSetSessionDescriptionFailure(observer, error);
1094 return;
1095 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096
deadbeefab9b2d12015-10-14 11:33:11 -07001097 // If setting the description decided our SSL role, allocate any necessary
1098 // SCTP sids.
1099 rtc::SSLRole role;
1100 if (session_->data_channel_type() == cricket::DCT_SCTP &&
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001101 session_->GetSslRole(session_->data_channel(), &role)) {
deadbeefab9b2d12015-10-14 11:33:11 -07001102 AllocateSctpSids(role);
1103 }
1104
1105 const cricket::SessionDescription* remote_desc = desc->description();
deadbeefbda7e0b2015-12-08 17:13:40 -08001106 const cricket::ContentInfo* audio_content = GetFirstAudioContent(remote_desc);
1107 const cricket::ContentInfo* video_content = GetFirstVideoContent(remote_desc);
1108 const cricket::AudioContentDescription* audio_desc =
1109 GetFirstAudioContentDescription(remote_desc);
1110 const cricket::VideoContentDescription* video_desc =
1111 GetFirstVideoContentDescription(remote_desc);
1112 const cricket::DataContentDescription* data_desc =
1113 GetFirstDataContentDescription(remote_desc);
1114
1115 // Check if the descriptions include streams, just in case the peer supports
1116 // MSID, but doesn't indicate so with "a=msid-semantic".
1117 if (remote_desc->msid_supported() ||
1118 (audio_desc && !audio_desc->streams().empty()) ||
1119 (video_desc && !video_desc->streams().empty())) {
1120 remote_peer_supports_msid_ = true;
1121 }
deadbeefab9b2d12015-10-14 11:33:11 -07001122
1123 // We wait to signal new streams until we finish processing the description,
1124 // since only at that point will new streams have all their tracks.
1125 rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
1126
1127 // Find all audio rtp streams and create corresponding remote AudioTracks
1128 // and MediaStreams.
deadbeefab9b2d12015-10-14 11:33:11 -07001129 if (audio_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001130 if (audio_content->rejected) {
1131 RemoveTracks(cricket::MEDIA_TYPE_AUDIO);
1132 } else {
deadbeefbda7e0b2015-12-08 17:13:40 -08001133 bool default_audio_track_needed =
1134 !remote_peer_supports_msid_ &&
1135 MediaContentDirectionHasSend(audio_desc->direction());
1136 UpdateRemoteStreamsList(GetActiveStreams(audio_desc),
1137 default_audio_track_needed, audio_desc->type(),
deadbeeffaac4972015-11-12 15:33:07 -08001138 new_streams);
deadbeeffaac4972015-11-12 15:33:07 -08001139 }
deadbeefab9b2d12015-10-14 11:33:11 -07001140 }
1141
1142 // Find all video rtp streams and create corresponding remote VideoTracks
1143 // and MediaStreams.
deadbeefab9b2d12015-10-14 11:33:11 -07001144 if (video_content) {
deadbeeffaac4972015-11-12 15:33:07 -08001145 if (video_content->rejected) {
1146 RemoveTracks(cricket::MEDIA_TYPE_VIDEO);
1147 } else {
deadbeefbda7e0b2015-12-08 17:13:40 -08001148 bool default_video_track_needed =
1149 !remote_peer_supports_msid_ &&
1150 MediaContentDirectionHasSend(video_desc->direction());
1151 UpdateRemoteStreamsList(GetActiveStreams(video_desc),
1152 default_video_track_needed, video_desc->type(),
deadbeeffaac4972015-11-12 15:33:07 -08001153 new_streams);
deadbeeffaac4972015-11-12 15:33:07 -08001154 }
deadbeefab9b2d12015-10-14 11:33:11 -07001155 }
1156
1157 // Update the DataChannels with the information from the remote peer.
deadbeefbda7e0b2015-12-08 17:13:40 -08001158 if (data_desc) {
1159 if (rtc::starts_with(data_desc->protocol().data(),
deadbeefab9b2d12015-10-14 11:33:11 -07001160 cricket::kMediaProtocolRtpPrefix)) {
deadbeefbda7e0b2015-12-08 17:13:40 -08001161 UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc));
deadbeefab9b2d12015-10-14 11:33:11 -07001162 }
1163 }
1164
1165 // Iterate new_streams and notify the observer about new MediaStreams.
1166 for (size_t i = 0; i < new_streams->count(); ++i) {
1167 MediaStreamInterface* new_stream = new_streams->at(i);
1168 stats_->AddStream(new_stream);
1169 observer_->OnAddStream(new_stream);
1170 }
1171
deadbeefbda7e0b2015-12-08 17:13:40 -08001172 UpdateEndedRemoteMediaStreams();
deadbeefab9b2d12015-10-14 11:33:11 -07001173
1174 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
1175 signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
deadbeeffc648b62015-10-13 16:42:33 -07001176}
1177
deadbeefa67696b2015-09-29 11:56:26 -07001178bool PeerConnection::SetConfiguration(const RTCConfiguration& config) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001179 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001180 if (port_allocator_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001181 cricket::ServerAddresses stun_servers;
1182 std::vector<cricket::RelayServerConfig> turn_servers;
1183 if (!ParseIceServers(config.servers, &stun_servers, &turn_servers)) {
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001184 return false;
1185 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001186 port_allocator_->SetIceServers(stun_servers, turn_servers);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001187 }
honghaiz1f429e32015-09-28 07:57:34 -07001188 session_->SetIceConfig(session_->ParseIceConfig(config));
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001189 return session_->SetIceTransports(config.type);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001190}
1191
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192bool PeerConnection::AddIceCandidate(
1193 const IceCandidateInterface* ice_candidate) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001194 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 return session_->ProcessIceMessage(ice_candidate);
1196}
1197
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001198void PeerConnection::RegisterUMAObserver(UMAObserver* observer) {
Peter Boström1a9d6152015-12-08 22:15:17 +01001199 TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver");
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001200 uma_observer_ = observer;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001201
1202 if (session_) {
1203 session_->set_metrics_observer(uma_observer_);
1204 }
1205
mallinath@webrtc.orgd37bcfa2014-05-12 23:10:18 +00001206 // Send information about IPv4/IPv6 status.
1207 if (uma_observer_ && port_allocator_) {
1208 if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) {
Guo-wei Shiehdfbe6792015-09-03 17:12:07 -07001209 uma_observer_->IncrementEnumCounter(
1210 kEnumCounterAddressFamily, kPeerConnection_IPv6,
1211 kPeerConnectionAddressFamilyCounter_Max);
mallinath@webrtc.orgb445f262014-05-23 22:19:37 +00001212 } else {
Guo-wei Shiehdfbe6792015-09-03 17:12:07 -07001213 uma_observer_->IncrementEnumCounter(
1214 kEnumCounterAddressFamily, kPeerConnection_IPv4,
1215 kPeerConnectionAddressFamilyCounter_Max);
mallinath@webrtc.orgd37bcfa2014-05-12 23:10:18 +00001216 }
1217 }
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +00001218}
1219
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220const SessionDescriptionInterface* PeerConnection::local_description() const {
1221 return session_->local_description();
1222}
1223
1224const SessionDescriptionInterface* PeerConnection::remote_description() const {
1225 return session_->remote_description();
1226}
1227
1228void PeerConnection::Close() {
Peter Boström1a9d6152015-12-08 22:15:17 +01001229 TRACE_EVENT0("webrtc", "PeerConnection::Close");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 // Update stats here so that we have the most recent stats for tracks and
1231 // streams before the channels are closed.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001232 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233
deadbeefd59daf82015-10-14 15:02:44 -07001234 session_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235}
1236
deadbeefd59daf82015-10-14 15:02:44 -07001237void PeerConnection::OnSessionStateChange(WebRtcSession* /*session*/,
1238 WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 switch (state) {
deadbeefd59daf82015-10-14 15:02:44 -07001240 case WebRtcSession::STATE_INIT:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 ChangeSignalingState(PeerConnectionInterface::kStable);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001242 break;
deadbeefd59daf82015-10-14 15:02:44 -07001243 case WebRtcSession::STATE_SENTOFFER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer);
1245 break;
deadbeefd59daf82015-10-14 15:02:44 -07001246 case WebRtcSession::STATE_SENTPRANSWER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer);
1248 break;
deadbeefd59daf82015-10-14 15:02:44 -07001249 case WebRtcSession::STATE_RECEIVEDOFFER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer);
1251 break;
deadbeefd59daf82015-10-14 15:02:44 -07001252 case WebRtcSession::STATE_RECEIVEDPRANSWER:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer);
1254 break;
deadbeefd59daf82015-10-14 15:02:44 -07001255 case WebRtcSession::STATE_INPROGRESS:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 ChangeSignalingState(PeerConnectionInterface::kStable);
1257 break;
deadbeefd59daf82015-10-14 15:02:44 -07001258 case WebRtcSession::STATE_CLOSED:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 ChangeSignalingState(PeerConnectionInterface::kClosed);
1260 break;
1261 default:
1262 break;
1263 }
1264}
1265
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001266void PeerConnection::OnMessage(rtc::Message* msg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 switch (msg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 case MSG_SET_SESSIONDESCRIPTION_SUCCESS: {
1269 SetSessionDescriptionMsg* param =
1270 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
1271 param->observer->OnSuccess();
1272 delete param;
1273 break;
1274 }
1275 case MSG_SET_SESSIONDESCRIPTION_FAILED: {
1276 SetSessionDescriptionMsg* param =
1277 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
1278 param->observer->OnFailure(param->error);
1279 delete param;
1280 break;
1281 }
deadbeefab9b2d12015-10-14 11:33:11 -07001282 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
1283 CreateSessionDescriptionMsg* param =
1284 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
1285 param->observer->OnFailure(param->error);
1286 delete param;
1287 break;
1288 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 case MSG_GETSTATS: {
1290 GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +00001291 StatsReports reports;
1292 stats_->GetStats(param->track, &reports);
1293 param->observer->OnComplete(reports);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 delete param;
1295 break;
1296 }
deadbeefbd292462015-12-14 18:15:29 -08001297 case MSG_FREE_DATACHANNELS: {
1298 sctp_data_channels_to_free_.clear();
1299 break;
1300 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 default:
deadbeef0a6c4ca2015-10-06 11:38:28 -07001302 RTC_DCHECK(false && "Not implemented");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 break;
1304 }
1305}
1306
deadbeefab9b2d12015-10-14 11:33:11 -07001307void PeerConnection::CreateAudioReceiver(MediaStreamInterface* stream,
1308 AudioTrackInterface* audio_track,
1309 uint32_t ssrc) {
deadbeefe1f9d832016-01-14 15:35:42 -08001310 receivers_.push_back(RtpReceiverProxy::Create(
1311 signaling_thread(),
1312 new AudioRtpReceiver(audio_track, ssrc, session_.get())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313}
1314
deadbeefab9b2d12015-10-14 11:33:11 -07001315void PeerConnection::CreateVideoReceiver(MediaStreamInterface* stream,
1316 VideoTrackInterface* video_track,
1317 uint32_t ssrc) {
deadbeefe1f9d832016-01-14 15:35:42 -08001318 receivers_.push_back(RtpReceiverProxy::Create(
1319 signaling_thread(),
1320 new VideoRtpReceiver(video_track, ssrc, session_.get())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321}
1322
deadbeef70ab1a12015-09-28 16:53:55 -07001323// TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote
1324// description.
deadbeefab9b2d12015-10-14 11:33:11 -07001325void PeerConnection::DestroyAudioReceiver(MediaStreamInterface* stream,
1326 AudioTrackInterface* audio_track) {
deadbeef70ab1a12015-09-28 16:53:55 -07001327 auto it = FindReceiverForTrack(audio_track);
1328 if (it == receivers_.end()) {
1329 LOG(LS_WARNING) << "RtpReceiver for track with id " << audio_track->id()
1330 << " doesn't exist.";
1331 } else {
1332 (*it)->Stop();
1333 receivers_.erase(it);
1334 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
1336
deadbeefab9b2d12015-10-14 11:33:11 -07001337void PeerConnection::DestroyVideoReceiver(MediaStreamInterface* stream,
1338 VideoTrackInterface* video_track) {
deadbeef70ab1a12015-09-28 16:53:55 -07001339 auto it = FindReceiverForTrack(video_track);
1340 if (it == receivers_.end()) {
1341 LOG(LS_WARNING) << "RtpReceiver for track with id " << video_track->id()
1342 << " doesn't exist.";
1343 } else {
1344 (*it)->Stop();
1345 receivers_.erase(it);
1346 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347}
deadbeef70ab1a12015-09-28 16:53:55 -07001348
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349void PeerConnection::OnIceConnectionChange(
1350 PeerConnectionInterface::IceConnectionState new_state) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001351 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001352 // After transitioning to "closed", ignore any additional states from
1353 // WebRtcSession (such as "disconnected").
deadbeefab9b2d12015-10-14 11:33:11 -07001354 if (IsClosed()) {
deadbeefcbecd352015-09-23 11:50:27 -07001355 return;
1356 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 ice_connection_state_ = new_state;
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001358 observer_->OnIceConnectionChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359}
1360
1361void PeerConnection::OnIceGatheringChange(
1362 PeerConnectionInterface::IceGatheringState new_state) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001363 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 if (IsClosed()) {
1365 return;
1366 }
1367 ice_gathering_state_ = new_state;
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001368 observer_->OnIceGatheringChange(ice_gathering_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369}
1370
1371void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001372 RTC_DCHECK(signaling_thread()->IsCurrent());
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +00001373 observer_->OnIceCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374}
1375
Peter Thatcher54360512015-07-08 11:08:35 -07001376void PeerConnection::OnIceConnectionReceivingChange(bool receiving) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07001377 RTC_DCHECK(signaling_thread()->IsCurrent());
Peter Thatcher54360512015-07-08 11:08:35 -07001378 observer_->OnIceConnectionReceivingChange(receiving);
1379}
1380
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381void PeerConnection::ChangeSignalingState(
1382 PeerConnectionInterface::SignalingState signaling_state) {
1383 signaling_state_ = signaling_state;
1384 if (signaling_state == kClosed) {
1385 ice_connection_state_ = kIceConnectionClosed;
1386 observer_->OnIceConnectionChange(ice_connection_state_);
1387 if (ice_gathering_state_ != kIceGatheringComplete) {
1388 ice_gathering_state_ = kIceGatheringComplete;
1389 observer_->OnIceGatheringChange(ice_gathering_state_);
1390 }
1391 }
1392 observer_->OnSignalingChange(signaling_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393}
1394
deadbeefeb459812015-12-15 19:24:43 -08001395void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track,
1396 MediaStreamInterface* stream) {
1397 auto sender = FindSenderForTrack(track);
1398 if (sender != senders_.end()) {
1399 // We already have a sender for this track, so just change the stream_id
1400 // so that it's correct in the next call to CreateOffer.
1401 (*sender)->set_stream_id(stream->label());
1402 return;
1403 }
1404
1405 // Normal case; we've never seen this track before.
deadbeefe1f9d832016-01-14 15:35:42 -08001406 rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create(
1407 signaling_thread(),
1408 new AudioRtpSender(track, stream->label(), session_.get(), stats_.get()));
deadbeefeb459812015-12-15 19:24:43 -08001409 senders_.push_back(new_sender);
1410 // If the sender has already been configured in SDP, we call SetSsrc,
1411 // which will connect the sender to the underlying transport. This can
1412 // occur if a local session description that contains the ID of the sender
1413 // is set before AddStream is called. It can also occur if the local
1414 // session description is not changed and RemoveStream is called, and
1415 // later AddStream is called again with the same stream.
1416 const TrackInfo* track_info =
1417 FindTrackInfo(local_audio_tracks_, stream->label(), track->id());
1418 if (track_info) {
1419 new_sender->SetSsrc(track_info->ssrc);
1420 }
1421}
1422
1423// TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
1424// indefinitely, when we have unified plan SDP.
1425void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track,
1426 MediaStreamInterface* stream) {
1427 auto sender = FindSenderForTrack(track);
1428 if (sender == senders_.end()) {
1429 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1430 << " doesn't exist.";
1431 return;
1432 }
1433 (*sender)->Stop();
1434 senders_.erase(sender);
1435}
1436
1437void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track,
1438 MediaStreamInterface* stream) {
1439 auto sender = FindSenderForTrack(track);
1440 if (sender != senders_.end()) {
1441 // We already have a sender for this track, so just change the stream_id
1442 // so that it's correct in the next call to CreateOffer.
1443 (*sender)->set_stream_id(stream->label());
1444 return;
1445 }
1446
1447 // Normal case; we've never seen this track before.
deadbeefe1f9d832016-01-14 15:35:42 -08001448 rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create(
1449 signaling_thread(),
1450 new VideoRtpSender(track, stream->label(), session_.get()));
deadbeefeb459812015-12-15 19:24:43 -08001451 senders_.push_back(new_sender);
1452 const TrackInfo* track_info =
1453 FindTrackInfo(local_video_tracks_, stream->label(), track->id());
1454 if (track_info) {
1455 new_sender->SetSsrc(track_info->ssrc);
1456 }
1457}
1458
1459void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track,
1460 MediaStreamInterface* stream) {
1461 auto sender = FindSenderForTrack(track);
1462 if (sender == senders_.end()) {
1463 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1464 << " doesn't exist.";
1465 return;
1466 }
1467 (*sender)->Stop();
1468 senders_.erase(sender);
1469}
1470
deadbeefab9b2d12015-10-14 11:33:11 -07001471void PeerConnection::PostSetSessionDescriptionFailure(
1472 SetSessionDescriptionObserver* observer,
1473 const std::string& error) {
1474 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
1475 msg->error = error;
1476 signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_FAILED, msg);
1477}
1478
1479void PeerConnection::PostCreateSessionDescriptionFailure(
1480 CreateSessionDescriptionObserver* observer,
1481 const std::string& error) {
1482 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
1483 msg->error = error;
1484 signaling_thread()->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
1485}
1486
1487bool PeerConnection::GetOptionsForOffer(
1488 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
1489 cricket::MediaSessionOptions* session_options) {
deadbeefab9b2d12015-10-14 11:33:11 -07001490 if (!ConvertRtcOptionsForOffer(rtc_options, session_options)) {
1491 return false;
1492 }
1493
deadbeeffac06552015-11-25 11:26:01 -08001494 AddSendStreams(session_options, senders_, rtp_data_channels_);
deadbeefc80741f2015-10-22 13:14:45 -07001495 // Offer to receive audio/video if the constraint is not set and there are
1496 // send streams, or we're currently receiving.
1497 if (rtc_options.offer_to_receive_audio == RTCOfferAnswerOptions::kUndefined) {
1498 session_options->recv_audio =
1499 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO) ||
1500 !remote_audio_tracks_.empty();
1501 }
1502 if (rtc_options.offer_to_receive_video == RTCOfferAnswerOptions::kUndefined) {
1503 session_options->recv_video =
1504 session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO) ||
1505 !remote_video_tracks_.empty();
1506 }
1507 session_options->bundle_enabled =
1508 session_options->bundle_enabled &&
1509 (session_options->has_audio() || session_options->has_video() ||
1510 session_options->has_data());
1511
deadbeefab9b2d12015-10-14 11:33:11 -07001512 if (session_->data_channel_type() == cricket::DCT_SCTP && HasDataChannels()) {
1513 session_options->data_channel_type = cricket::DCT_SCTP;
1514 }
1515 return true;
1516}
1517
1518bool PeerConnection::GetOptionsForAnswer(
1519 const MediaConstraintsInterface* constraints,
1520 cricket::MediaSessionOptions* session_options) {
deadbeefab9b2d12015-10-14 11:33:11 -07001521 session_options->recv_audio = false;
1522 session_options->recv_video = false;
deadbeefab9b2d12015-10-14 11:33:11 -07001523 if (!ParseConstraintsForAnswer(constraints, session_options)) {
1524 return false;
1525 }
1526
deadbeeffac06552015-11-25 11:26:01 -08001527 AddSendStreams(session_options, senders_, rtp_data_channels_);
deadbeefc80741f2015-10-22 13:14:45 -07001528 session_options->bundle_enabled =
1529 session_options->bundle_enabled &&
1530 (session_options->has_audio() || session_options->has_video() ||
1531 session_options->has_data());
1532
deadbeefab9b2d12015-10-14 11:33:11 -07001533 // RTP data channel is handled in MediaSessionOptions::AddStream. SCTP streams
1534 // are not signaled in the SDP so does not go through that path and must be
1535 // handled here.
1536 if (session_->data_channel_type() == cricket::DCT_SCTP) {
1537 session_options->data_channel_type = cricket::DCT_SCTP;
1538 }
1539 return true;
1540}
1541
deadbeeffaac4972015-11-12 15:33:07 -08001542void PeerConnection::RemoveTracks(cricket::MediaType media_type) {
1543 UpdateLocalTracks(std::vector<cricket::StreamParams>(), media_type);
deadbeefbda7e0b2015-12-08 17:13:40 -08001544 UpdateRemoteStreamsList(std::vector<cricket::StreamParams>(), false,
1545 media_type, nullptr);
deadbeeffaac4972015-11-12 15:33:07 -08001546}
1547
deadbeefab9b2d12015-10-14 11:33:11 -07001548void PeerConnection::UpdateRemoteStreamsList(
1549 const cricket::StreamParamsVec& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -08001550 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -07001551 cricket::MediaType media_type,
1552 StreamCollection* new_streams) {
1553 TrackInfos* current_tracks = GetRemoteTracks(media_type);
1554
1555 // Find removed tracks. I.e., tracks where the track id or ssrc don't match
deadbeeffac06552015-11-25 11:26:01 -08001556 // the new StreamParam.
deadbeefab9b2d12015-10-14 11:33:11 -07001557 auto track_it = current_tracks->begin();
1558 while (track_it != current_tracks->end()) {
1559 const TrackInfo& info = *track_it;
1560 const cricket::StreamParams* params =
1561 cricket::GetStreamBySsrc(streams, info.ssrc);
deadbeefbda7e0b2015-12-08 17:13:40 -08001562 bool track_exists = params && params->id == info.track_id;
1563 // If this is a default track, and we still need it, don't remove it.
1564 if ((info.stream_label == kDefaultStreamLabel && default_track_needed) ||
1565 track_exists) {
1566 ++track_it;
1567 } else {
deadbeefab9b2d12015-10-14 11:33:11 -07001568 OnRemoteTrackRemoved(info.stream_label, info.track_id, media_type);
1569 track_it = current_tracks->erase(track_it);
deadbeefab9b2d12015-10-14 11:33:11 -07001570 }
1571 }
1572
1573 // Find new and active tracks.
1574 for (const cricket::StreamParams& params : streams) {
1575 // The sync_label is the MediaStream label and the |stream.id| is the
1576 // track id.
1577 const std::string& stream_label = params.sync_label;
1578 const std::string& track_id = params.id;
1579 uint32_t ssrc = params.first_ssrc();
1580
1581 rtc::scoped_refptr<MediaStreamInterface> stream =
1582 remote_streams_->find(stream_label);
1583 if (!stream) {
1584 // This is a new MediaStream. Create a new remote MediaStream.
1585 stream = remote_stream_factory_->CreateMediaStream(stream_label);
1586 remote_streams_->AddStream(stream);
1587 new_streams->AddStream(stream);
1588 }
1589
1590 const TrackInfo* track_info =
1591 FindTrackInfo(*current_tracks, stream_label, track_id);
1592 if (!track_info) {
1593 current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc));
1594 OnRemoteTrackSeen(stream_label, track_id, ssrc, media_type);
1595 }
1596 }
deadbeefbda7e0b2015-12-08 17:13:40 -08001597
1598 // Add default track if necessary.
1599 if (default_track_needed) {
1600 rtc::scoped_refptr<MediaStreamInterface> default_stream =
1601 remote_streams_->find(kDefaultStreamLabel);
1602 if (!default_stream) {
1603 // Create the new default MediaStream.
1604 default_stream =
1605 remote_stream_factory_->CreateMediaStream(kDefaultStreamLabel);
1606 remote_streams_->AddStream(default_stream);
1607 new_streams->AddStream(default_stream);
1608 }
1609 std::string default_track_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
1610 ? kDefaultAudioTrackLabel
1611 : kDefaultVideoTrackLabel;
1612 const TrackInfo* default_track_info =
1613 FindTrackInfo(*current_tracks, kDefaultStreamLabel, default_track_id);
1614 if (!default_track_info) {
1615 current_tracks->push_back(
1616 TrackInfo(kDefaultStreamLabel, default_track_id, 0));
1617 OnRemoteTrackSeen(kDefaultStreamLabel, default_track_id, 0, media_type);
1618 }
1619 }
deadbeefab9b2d12015-10-14 11:33:11 -07001620}
1621
1622void PeerConnection::OnRemoteTrackSeen(const std::string& stream_label,
1623 const std::string& track_id,
1624 uint32_t ssrc,
1625 cricket::MediaType media_type) {
1626 MediaStreamInterface* stream = remote_streams_->find(stream_label);
1627
1628 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Tommif888bb52015-12-12 01:37:01 +01001629 AudioTrackInterface* audio_track = remote_stream_factory_->AddAudioTrack(
1630 ssrc, session_.get(), stream, track_id);
deadbeefab9b2d12015-10-14 11:33:11 -07001631 CreateAudioReceiver(stream, audio_track, ssrc);
1632 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1633 VideoTrackInterface* video_track =
1634 remote_stream_factory_->AddVideoTrack(stream, track_id);
1635 CreateVideoReceiver(stream, video_track, ssrc);
1636 } else {
1637 RTC_DCHECK(false && "Invalid media type");
1638 }
1639}
1640
1641void PeerConnection::OnRemoteTrackRemoved(const std::string& stream_label,
1642 const std::string& track_id,
1643 cricket::MediaType media_type) {
1644 MediaStreamInterface* stream = remote_streams_->find(stream_label);
1645
1646 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1647 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1648 stream->FindAudioTrack(track_id);
1649 if (audio_track) {
1650 audio_track->set_state(webrtc::MediaStreamTrackInterface::kEnded);
1651 stream->RemoveTrack(audio_track);
1652 DestroyAudioReceiver(stream, audio_track);
1653 }
1654 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1655 rtc::scoped_refptr<VideoTrackInterface> video_track =
1656 stream->FindVideoTrack(track_id);
1657 if (video_track) {
1658 video_track->set_state(webrtc::MediaStreamTrackInterface::kEnded);
1659 stream->RemoveTrack(video_track);
1660 DestroyVideoReceiver(stream, video_track);
1661 }
1662 } else {
1663 ASSERT(false && "Invalid media type");
1664 }
1665}
1666
1667void PeerConnection::UpdateEndedRemoteMediaStreams() {
1668 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
1669 for (size_t i = 0; i < remote_streams_->count(); ++i) {
1670 MediaStreamInterface* stream = remote_streams_->at(i);
1671 if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
1672 streams_to_remove.push_back(stream);
1673 }
1674 }
1675
1676 for (const auto& stream : streams_to_remove) {
1677 remote_streams_->RemoveStream(stream);
1678 observer_->OnRemoveStream(stream);
1679 }
1680}
1681
deadbeefab9b2d12015-10-14 11:33:11 -07001682void PeerConnection::EndRemoteTracks(cricket::MediaType media_type) {
1683 TrackInfos* current_tracks = GetRemoteTracks(media_type);
1684 for (TrackInfos::iterator track_it = current_tracks->begin();
1685 track_it != current_tracks->end(); ++track_it) {
1686 const TrackInfo& info = *track_it;
1687 MediaStreamInterface* stream = remote_streams_->find(info.stream_label);
1688 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1689 AudioTrackInterface* track = stream->FindAudioTrack(info.track_id);
1690 // There's no guarantee the track is still available, e.g. the track may
1691 // have been removed from the stream by javascript.
1692 if (track) {
1693 track->set_state(webrtc::MediaStreamTrackInterface::kEnded);
1694 }
1695 }
1696 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1697 VideoTrackInterface* track = stream->FindVideoTrack(info.track_id);
1698 // There's no guarantee the track is still available, e.g. the track may
1699 // have been removed from the stream by javascript.
1700 if (track) {
1701 track->set_state(webrtc::MediaStreamTrackInterface::kEnded);
1702 }
1703 }
1704 }
1705}
1706
1707void PeerConnection::UpdateLocalTracks(
1708 const std::vector<cricket::StreamParams>& streams,
1709 cricket::MediaType media_type) {
1710 TrackInfos* current_tracks = GetLocalTracks(media_type);
1711
1712 // Find removed tracks. I.e., tracks where the track id, stream label or ssrc
1713 // don't match the new StreamParam.
1714 TrackInfos::iterator track_it = current_tracks->begin();
1715 while (track_it != current_tracks->end()) {
1716 const TrackInfo& info = *track_it;
1717 const cricket::StreamParams* params =
1718 cricket::GetStreamBySsrc(streams, info.ssrc);
1719 if (!params || params->id != info.track_id ||
1720 params->sync_label != info.stream_label) {
1721 OnLocalTrackRemoved(info.stream_label, info.track_id, info.ssrc,
1722 media_type);
1723 track_it = current_tracks->erase(track_it);
1724 } else {
1725 ++track_it;
1726 }
1727 }
1728
1729 // Find new and active tracks.
1730 for (const cricket::StreamParams& params : streams) {
1731 // The sync_label is the MediaStream label and the |stream.id| is the
1732 // track id.
1733 const std::string& stream_label = params.sync_label;
1734 const std::string& track_id = params.id;
1735 uint32_t ssrc = params.first_ssrc();
1736 const TrackInfo* track_info =
1737 FindTrackInfo(*current_tracks, stream_label, track_id);
1738 if (!track_info) {
1739 current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc));
1740 OnLocalTrackSeen(stream_label, track_id, params.first_ssrc(), media_type);
1741 }
1742 }
1743}
1744
1745void PeerConnection::OnLocalTrackSeen(const std::string& stream_label,
1746 const std::string& track_id,
1747 uint32_t ssrc,
1748 cricket::MediaType media_type) {
deadbeeffac06552015-11-25 11:26:01 -08001749 RtpSenderInterface* sender = FindSenderById(track_id);
1750 if (!sender) {
1751 LOG(LS_WARNING) << "An unknown RtpSender with id " << track_id
1752 << " has been configured in the local description.";
deadbeefab9b2d12015-10-14 11:33:11 -07001753 return;
1754 }
1755
deadbeeffac06552015-11-25 11:26:01 -08001756 if (sender->media_type() != media_type) {
1757 LOG(LS_WARNING) << "An RtpSender has been configured in the local"
1758 << " description with an unexpected media type.";
1759 return;
deadbeefab9b2d12015-10-14 11:33:11 -07001760 }
deadbeeffac06552015-11-25 11:26:01 -08001761
1762 sender->set_stream_id(stream_label);
1763 sender->SetSsrc(ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -07001764}
1765
1766void PeerConnection::OnLocalTrackRemoved(const std::string& stream_label,
1767 const std::string& track_id,
1768 uint32_t ssrc,
1769 cricket::MediaType media_type) {
deadbeeffac06552015-11-25 11:26:01 -08001770 RtpSenderInterface* sender = FindSenderById(track_id);
1771 if (!sender) {
1772 // This is the normal case. I.e., RemoveStream has been called and the
deadbeefab9b2d12015-10-14 11:33:11 -07001773 // SessionDescriptions has been renegotiated.
1774 return;
1775 }
deadbeeffac06552015-11-25 11:26:01 -08001776
1777 // A sender has been removed from the SessionDescription but it's still
1778 // associated with the PeerConnection. This only occurs if the SDP doesn't
1779 // match with the calls to CreateSender, AddStream and RemoveStream.
1780 if (sender->media_type() != media_type) {
1781 LOG(LS_WARNING) << "An RtpSender has been configured in the local"
1782 << " description with an unexpected media type.";
1783 return;
deadbeefab9b2d12015-10-14 11:33:11 -07001784 }
deadbeeffac06552015-11-25 11:26:01 -08001785
1786 sender->SetSsrc(0);
deadbeefab9b2d12015-10-14 11:33:11 -07001787}
1788
1789void PeerConnection::UpdateLocalRtpDataChannels(
1790 const cricket::StreamParamsVec& streams) {
1791 std::vector<std::string> existing_channels;
1792
1793 // Find new and active data channels.
1794 for (const cricket::StreamParams& params : streams) {
1795 // |it->sync_label| is actually the data channel label. The reason is that
1796 // we use the same naming of data channels as we do for
1797 // MediaStreams and Tracks.
1798 // For MediaStreams, the sync_label is the MediaStream label and the
1799 // track label is the same as |streamid|.
1800 const std::string& channel_label = params.sync_label;
1801 auto data_channel_it = rtp_data_channels_.find(channel_label);
1802 if (!VERIFY(data_channel_it != rtp_data_channels_.end())) {
1803 continue;
1804 }
1805 // Set the SSRC the data channel should use for sending.
1806 data_channel_it->second->SetSendSsrc(params.first_ssrc());
1807 existing_channels.push_back(data_channel_it->first);
1808 }
1809
1810 UpdateClosingRtpDataChannels(existing_channels, true);
1811}
1812
1813void PeerConnection::UpdateRemoteRtpDataChannels(
1814 const cricket::StreamParamsVec& streams) {
1815 std::vector<std::string> existing_channels;
1816
1817 // Find new and active data channels.
1818 for (const cricket::StreamParams& params : streams) {
1819 // The data channel label is either the mslabel or the SSRC if the mslabel
1820 // does not exist. Ex a=ssrc:444330170 mslabel:test1.
1821 std::string label = params.sync_label.empty()
1822 ? rtc::ToString(params.first_ssrc())
1823 : params.sync_label;
1824 auto data_channel_it = rtp_data_channels_.find(label);
1825 if (data_channel_it == rtp_data_channels_.end()) {
1826 // This is a new data channel.
1827 CreateRemoteRtpDataChannel(label, params.first_ssrc());
1828 } else {
1829 data_channel_it->second->SetReceiveSsrc(params.first_ssrc());
1830 }
1831 existing_channels.push_back(label);
1832 }
1833
1834 UpdateClosingRtpDataChannels(existing_channels, false);
1835}
1836
1837void PeerConnection::UpdateClosingRtpDataChannels(
1838 const std::vector<std::string>& active_channels,
1839 bool is_local_update) {
1840 auto it = rtp_data_channels_.begin();
1841 while (it != rtp_data_channels_.end()) {
1842 DataChannel* data_channel = it->second;
1843 if (std::find(active_channels.begin(), active_channels.end(),
1844 data_channel->label()) != active_channels.end()) {
1845 ++it;
1846 continue;
1847 }
1848
1849 if (is_local_update) {
1850 data_channel->SetSendSsrc(0);
1851 } else {
1852 data_channel->RemotePeerRequestClose();
1853 }
1854
1855 if (data_channel->state() == DataChannel::kClosed) {
1856 rtp_data_channels_.erase(it);
1857 it = rtp_data_channels_.begin();
1858 } else {
1859 ++it;
1860 }
1861 }
1862}
1863
1864void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label,
1865 uint32_t remote_ssrc) {
1866 rtc::scoped_refptr<DataChannel> channel(
1867 InternalCreateDataChannel(label, nullptr));
1868 if (!channel.get()) {
1869 LOG(LS_WARNING) << "Remote peer requested a DataChannel but"
1870 << "CreateDataChannel failed.";
1871 return;
1872 }
1873 channel->SetReceiveSsrc(remote_ssrc);
1874 observer_->OnDataChannel(
1875 DataChannelProxy::Create(signaling_thread(), channel));
1876}
1877
1878rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel(
1879 const std::string& label,
1880 const InternalDataChannelInit* config) {
1881 if (IsClosed()) {
1882 return nullptr;
1883 }
1884 if (session_->data_channel_type() == cricket::DCT_NONE) {
1885 LOG(LS_ERROR)
1886 << "InternalCreateDataChannel: Data is not supported in this call.";
1887 return nullptr;
1888 }
1889 InternalDataChannelInit new_config =
1890 config ? (*config) : InternalDataChannelInit();
1891 if (session_->data_channel_type() == cricket::DCT_SCTP) {
1892 if (new_config.id < 0) {
1893 rtc::SSLRole role;
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001894 if ((session_->GetSslRole(session_->data_channel(), &role)) &&
deadbeefab9b2d12015-10-14 11:33:11 -07001895 !sid_allocator_.AllocateSid(role, &new_config.id)) {
1896 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1897 return nullptr;
1898 }
1899 } else if (!sid_allocator_.ReserveSid(new_config.id)) {
1900 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1901 << "because the id is already in use or out of range.";
1902 return nullptr;
1903 }
1904 }
1905
1906 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
1907 session_.get(), session_->data_channel_type(), label, new_config));
1908 if (!channel) {
1909 sid_allocator_.ReleaseSid(new_config.id);
1910 return nullptr;
1911 }
1912
1913 if (channel->data_channel_type() == cricket::DCT_RTP) {
1914 if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) {
1915 LOG(LS_ERROR) << "DataChannel with label " << channel->label()
1916 << " already exists.";
1917 return nullptr;
1918 }
1919 rtp_data_channels_[channel->label()] = channel;
1920 } else {
1921 RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP);
1922 sctp_data_channels_.push_back(channel);
1923 channel->SignalClosed.connect(this,
1924 &PeerConnection::OnSctpDataChannelClosed);
1925 }
1926
1927 return channel;
1928}
1929
1930bool PeerConnection::HasDataChannels() const {
1931 return !rtp_data_channels_.empty() || !sctp_data_channels_.empty();
1932}
1933
1934void PeerConnection::AllocateSctpSids(rtc::SSLRole role) {
1935 for (const auto& channel : sctp_data_channels_) {
1936 if (channel->id() < 0) {
1937 int sid;
1938 if (!sid_allocator_.AllocateSid(role, &sid)) {
1939 LOG(LS_ERROR) << "Failed to allocate SCTP sid.";
1940 continue;
1941 }
1942 channel->SetSctpSid(sid);
1943 }
1944 }
1945}
1946
1947void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) {
deadbeefbd292462015-12-14 18:15:29 -08001948 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07001949 for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end();
1950 ++it) {
1951 if (it->get() == channel) {
1952 if (channel->id() >= 0) {
1953 sid_allocator_.ReleaseSid(channel->id());
1954 }
deadbeefbd292462015-12-14 18:15:29 -08001955 // Since this method is triggered by a signal from the DataChannel,
1956 // we can't free it directly here; we need to free it asynchronously.
1957 sctp_data_channels_to_free_.push_back(*it);
deadbeefab9b2d12015-10-14 11:33:11 -07001958 sctp_data_channels_.erase(it);
deadbeefbd292462015-12-14 18:15:29 -08001959 signaling_thread()->Post(this, MSG_FREE_DATACHANNELS, nullptr);
deadbeefab9b2d12015-10-14 11:33:11 -07001960 return;
1961 }
1962 }
1963}
1964
1965void PeerConnection::OnVoiceChannelDestroyed() {
1966 EndRemoteTracks(cricket::MEDIA_TYPE_AUDIO);
1967}
1968
1969void PeerConnection::OnVideoChannelDestroyed() {
1970 EndRemoteTracks(cricket::MEDIA_TYPE_VIDEO);
1971}
1972
1973void PeerConnection::OnDataChannelCreated() {
1974 for (const auto& channel : sctp_data_channels_) {
1975 channel->OnTransportChannelCreated();
1976 }
1977}
1978
1979void PeerConnection::OnDataChannelDestroyed() {
1980 // Use a temporary copy of the RTP/SCTP DataChannel list because the
1981 // DataChannel may callback to us and try to modify the list.
1982 std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs;
1983 temp_rtp_dcs.swap(rtp_data_channels_);
1984 for (const auto& kv : temp_rtp_dcs) {
1985 kv.second->OnTransportChannelDestroyed();
1986 }
1987
1988 std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs;
1989 temp_sctp_dcs.swap(sctp_data_channels_);
1990 for (const auto& channel : temp_sctp_dcs) {
1991 channel->OnTransportChannelDestroyed();
1992 }
1993}
1994
1995void PeerConnection::OnDataChannelOpenMessage(
1996 const std::string& label,
1997 const InternalDataChannelInit& config) {
1998 rtc::scoped_refptr<DataChannel> channel(
1999 InternalCreateDataChannel(label, &config));
2000 if (!channel.get()) {
2001 LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message.";
2002 return;
2003 }
2004
2005 observer_->OnDataChannel(
2006 DataChannelProxy::Create(signaling_thread(), channel));
2007}
2008
deadbeeffac06552015-11-25 11:26:01 -08002009RtpSenderInterface* PeerConnection::FindSenderById(const std::string& id) {
2010 auto it =
2011 std::find_if(senders_.begin(), senders_.end(),
2012 [id](const rtc::scoped_refptr<RtpSenderInterface>& sender) {
2013 return sender->id() == id;
2014 });
2015 return it != senders_.end() ? it->get() : nullptr;
2016}
2017
deadbeef70ab1a12015-09-28 16:53:55 -07002018std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
2019PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) {
2020 return std::find_if(
2021 senders_.begin(), senders_.end(),
2022 [track](const rtc::scoped_refptr<RtpSenderInterface>& sender) {
2023 return sender->track() == track;
2024 });
2025}
2026
2027std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
2028PeerConnection::FindReceiverForTrack(MediaStreamTrackInterface* track) {
2029 return std::find_if(
2030 receivers_.begin(), receivers_.end(),
2031 [track](const rtc::scoped_refptr<RtpReceiverInterface>& receiver) {
2032 return receiver->track() == track;
2033 });
2034}
2035
deadbeefab9b2d12015-10-14 11:33:11 -07002036PeerConnection::TrackInfos* PeerConnection::GetRemoteTracks(
2037 cricket::MediaType media_type) {
2038 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
2039 media_type == cricket::MEDIA_TYPE_VIDEO);
2040 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &remote_audio_tracks_
2041 : &remote_video_tracks_;
2042}
2043
2044PeerConnection::TrackInfos* PeerConnection::GetLocalTracks(
2045 cricket::MediaType media_type) {
2046 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
2047 media_type == cricket::MEDIA_TYPE_VIDEO);
2048 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_tracks_
2049 : &local_video_tracks_;
2050}
2051
2052const PeerConnection::TrackInfo* PeerConnection::FindTrackInfo(
2053 const PeerConnection::TrackInfos& infos,
2054 const std::string& stream_label,
2055 const std::string track_id) const {
2056 for (const TrackInfo& track_info : infos) {
2057 if (track_info.stream_label == stream_label &&
2058 track_info.track_id == track_id) {
2059 return &track_info;
2060 }
2061 }
2062 return nullptr;
2063}
2064
2065DataChannel* PeerConnection::FindDataChannelBySid(int sid) const {
2066 for (const auto& channel : sctp_data_channels_) {
2067 if (channel->id() == sid) {
2068 return channel;
2069 }
2070 }
2071 return nullptr;
2072}
2073
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074} // namespace webrtc