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