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