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