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