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" |
| 28 | #include "webrtc/api/remotevideocapturer.h" |
| 29 | #include "webrtc/api/rtpreceiver.h" |
| 30 | #include "webrtc/api/rtpsender.h" |
| 31 | #include "webrtc/api/streamcollection.h" |
| 32 | #include "webrtc/api/videosource.h" |
| 33 | #include "webrtc/api/videotrack.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 34 | #include "webrtc/base/arraysize.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" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 39 | #include "webrtc/media/sctp/sctpdataengine.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 40 | #include "webrtc/p2p/client/basicportallocator.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 | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 50 | using webrtc::RtpSenderInterface; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 51 | using webrtc::StreamCollection; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 53 | static const char kDefaultStreamLabel[] = "default"; |
| 54 | static const char kDefaultAudioTrackLabel[] = "defaulta0"; |
| 55 | static const char kDefaultVideoTrackLabel[] = "defaultv0"; |
| 56 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | // The min number of tokens must present in Turn host uri. |
| 58 | // e.g. user@turn.example.org |
| 59 | static const size_t kTurnHostTokensNum = 2; |
| 60 | // Number of tokens must be preset when TURN uri has transport param. |
| 61 | static const size_t kTurnTransportTokensNum = 2; |
| 62 | // The default stun port. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 63 | static const int kDefaultStunPort = 3478; |
| 64 | static const int kDefaultStunTlsPort = 5349; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | static const char kTransport[] = "transport"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | |
| 67 | // NOTE: Must be in the same order as the ServiceType enum. |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 68 | static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 70 | // NOTE: A loop below assumes that the first value of this enum is 0 and all |
| 71 | // other values are incremental. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | enum ServiceType { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 73 | STUN = 0, // Indicates a STUN server. |
| 74 | STUNS, // Indicates a STUN server used with a TLS session. |
| 75 | TURN, // Indicates a TURN server |
| 76 | TURNS, // Indicates a TURN server used with a TLS session. |
| 77 | INVALID, // Unknown. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | }; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 79 | static_assert(INVALID == arraysize(kValidIceServiceTypes), |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 80 | "kValidIceServiceTypes must have as many strings as ServiceType " |
| 81 | "has values."); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | |
| 83 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 84 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 86 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 88 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 91 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | explicit SetSessionDescriptionMsg( |
| 93 | webrtc::SetSessionDescriptionObserver* observer) |
| 94 | : observer(observer) { |
| 95 | } |
| 96 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 97 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | std::string error; |
| 99 | }; |
| 100 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 101 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 102 | explicit CreateSessionDescriptionMsg( |
| 103 | webrtc::CreateSessionDescriptionObserver* observer) |
| 104 | : observer(observer) {} |
| 105 | |
| 106 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 107 | std::string error; |
| 108 | }; |
| 109 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 110 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 111 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 112 | webrtc::MediaStreamTrackInterface* track) |
| 113 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 115 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 116 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 119 | // |in_str| should be of format |
| 120 | // stunURI = scheme ":" stun-host [ ":" stun-port ] |
| 121 | // scheme = "stun" / "stuns" |
| 122 | // stun-host = IP-literal / IPv4address / reg-name |
| 123 | // stun-port = *DIGIT |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 124 | // |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 125 | // draft-petithuguenin-behave-turn-uris-01 |
| 126 | // turnURI = scheme ":" turn-host [ ":" turn-port ] |
| 127 | // turn-host = username@IP-literal / IPv4address / reg-name |
| 128 | bool GetServiceTypeAndHostnameFromUri(const std::string& in_str, |
| 129 | ServiceType* service_type, |
| 130 | std::string* hostname) { |
Tommi | 77d444a | 2015-04-24 15:38:38 +0200 | [diff] [blame] | 131 | const std::string::size_type colonpos = in_str.find(':'); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 132 | if (colonpos == std::string::npos) { |
| 133 | LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 136 | if ((colonpos + 1) == in_str.length()) { |
| 137 | LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str; |
| 138 | return false; |
| 139 | } |
| 140 | *service_type = INVALID; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 141 | for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 142 | if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 143 | *service_type = static_cast<ServiceType>(i); |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | if (*service_type == INVALID) { |
| 148 | return false; |
| 149 | } |
| 150 | *hostname = in_str.substr(colonpos + 1, std::string::npos); |
| 151 | return true; |
| 152 | } |
| 153 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 154 | bool ParsePort(const std::string& in_str, int* port) { |
| 155 | // Make sure port only contains digits. FromString doesn't check this. |
| 156 | for (const char& c : in_str) { |
| 157 | if (!std::isdigit(c)) { |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | return rtc::FromString(in_str, port); |
| 162 | } |
| 163 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 164 | // This method parses IPv6 and IPv4 literal strings, along with hostnames in |
| 165 | // standard hostname:port format. |
| 166 | // Consider following formats as correct. |
| 167 | // |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port, |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 168 | // |hostname|, |[IPv6 address]|, |IPv4 address|. |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 169 | bool ParseHostnameAndPortFromString(const std::string& in_str, |
| 170 | std::string* host, |
| 171 | int* port) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 172 | RTC_DCHECK(host->empty()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 173 | if (in_str.at(0) == '[') { |
| 174 | std::string::size_type closebracket = in_str.rfind(']'); |
| 175 | if (closebracket != std::string::npos) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 176 | std::string::size_type colonpos = in_str.find(':', closebracket); |
| 177 | if (std::string::npos != colonpos) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 178 | if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos), |
| 179 | port)) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 183 | *host = in_str.substr(1, closebracket - 1); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 184 | } else { |
| 185 | return false; |
| 186 | } |
| 187 | } else { |
| 188 | std::string::size_type colonpos = in_str.find(':'); |
| 189 | if (std::string::npos != colonpos) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 190 | if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 191 | return false; |
| 192 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 193 | *host = in_str.substr(0, colonpos); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 194 | } else { |
| 195 | *host = in_str; |
| 196 | } |
| 197 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 198 | return !host->empty(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 201 | // Adds a STUN or TURN server to the appropriate list, |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 202 | // by parsing |url| and using the username/password in |server|. |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 203 | bool ParseIceServerUrl(const PeerConnectionInterface::IceServer& server, |
| 204 | const std::string& url, |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 205 | cricket::ServerAddresses* stun_servers, |
| 206 | std::vector<cricket::RelayServerConfig>* turn_servers) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | // draft-nandakumar-rtcweb-stun-uri-01 |
| 208 | // stunURI = scheme ":" stun-host [ ":" stun-port ] |
| 209 | // scheme = "stun" / "stuns" |
| 210 | // stun-host = IP-literal / IPv4address / reg-name |
| 211 | // stun-port = *DIGIT |
| 212 | |
| 213 | // draft-petithuguenin-behave-turn-uris-01 |
| 214 | // turnURI = scheme ":" turn-host [ ":" turn-port ] |
| 215 | // [ "?transport=" transport ] |
| 216 | // scheme = "turn" / "turns" |
| 217 | // transport = "udp" / "tcp" / transport-ext |
| 218 | // transport-ext = 1*unreserved |
| 219 | // turn-host = IP-literal / IPv4address / reg-name |
| 220 | // turn-port = *DIGIT |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 221 | RTC_DCHECK(stun_servers != nullptr); |
| 222 | RTC_DCHECK(turn_servers != nullptr); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 223 | std::vector<std::string> tokens; |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 224 | cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 225 | RTC_DCHECK(!url.empty()); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 226 | rtc::tokenize(url, '?', &tokens); |
| 227 | std::string uri_without_transport = tokens[0]; |
| 228 | // Let's look into transport= param, if it exists. |
| 229 | if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present. |
| 230 | std::string uri_transport_param = tokens[1]; |
| 231 | rtc::tokenize(uri_transport_param, '=', &tokens); |
| 232 | if (tokens[0] == kTransport) { |
| 233 | // As per above grammar transport param will be consist of lower case |
| 234 | // letters. |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 235 | if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) || |
| 236 | (turn_transport_type != cricket::PROTO_UDP && |
| 237 | turn_transport_type != cricket::PROTO_TCP)) { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 238 | LOG(LS_WARNING) << "Transport param should always be udp or tcp."; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 239 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 242 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 243 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 244 | std::string hoststring; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 245 | ServiceType service_type; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 246 | if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, |
| 247 | &service_type, |
| 248 | &hoststring)) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 249 | LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url; |
| 250 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 251 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 252 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 253 | // GetServiceTypeAndHostnameFromUri should never give an empty hoststring |
| 254 | RTC_DCHECK(!hoststring.empty()); |
Tommi | 77d444a | 2015-04-24 15:38:38 +0200 | [diff] [blame] | 255 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 256 | // Let's break hostname. |
| 257 | tokens.clear(); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 258 | rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens); |
| 259 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 260 | std::string username(server.username); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 261 | if (tokens.size() > kTurnHostTokensNum) { |
| 262 | LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; |
| 263 | return false; |
| 264 | } |
| 265 | if (tokens.size() == kTurnHostTokensNum) { |
| 266 | if (tokens[0].empty() || tokens[1].empty()) { |
| 267 | LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; |
| 268 | return false; |
| 269 | } |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 270 | username.assign(rtc::s_url_decode(tokens[0])); |
| 271 | hoststring = tokens[1]; |
| 272 | } else { |
| 273 | hoststring = tokens[0]; |
| 274 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 275 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 276 | int port = kDefaultStunPort; |
| 277 | if (service_type == TURNS) { |
| 278 | port = kDefaultStunTlsPort; |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 279 | turn_transport_type = cricket::PROTO_TCP; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 280 | } |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 281 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 282 | std::string address; |
| 283 | if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 284 | LOG(WARNING) << "Invalid hostname format: " << uri_without_transport; |
| 285 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 286 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 287 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 288 | if (port <= 0 || port > 0xffff) { |
| 289 | LOG(WARNING) << "Invalid port: " << port; |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 290 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 291 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 292 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 293 | switch (service_type) { |
| 294 | case STUN: |
| 295 | case STUNS: |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 296 | stun_servers->insert(rtc::SocketAddress(address, port)); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 297 | break; |
| 298 | case TURN: |
| 299 | case TURNS: { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 300 | bool secure = (service_type == TURNS); |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 301 | turn_servers->push_back( |
| 302 | cricket::RelayServerConfig(address, port, username, server.password, |
| 303 | turn_transport_type, secure)); |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | case INVALID: |
| 307 | default: |
| 308 | LOG(WARNING) << "Configuration not supported: " << url; |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 314 | // Check if we can send |new_stream| on a PeerConnection. |
| 315 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 316 | webrtc::MediaStreamInterface* new_stream) { |
| 317 | if (!new_stream || !current_streams) { |
| 318 | return false; |
| 319 | } |
| 320 | if (current_streams->find(new_stream->label()) != nullptr) { |
| 321 | LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 322 | << " is already added."; |
| 323 | return false; |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | bool MediaContentDirectionHasSend(cricket::MediaContentDirection dir) { |
| 329 | return dir == cricket::MD_SENDONLY || dir == cricket::MD_SENDRECV; |
| 330 | } |
| 331 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 332 | // If the direction is "recvonly" or "inactive", treat the description |
| 333 | // as containing no streams. |
| 334 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 335 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 336 | const cricket::MediaContentDescription* desc) { |
| 337 | return MediaContentDirectionHasSend(desc->direction()) |
| 338 | ? desc->streams() |
| 339 | : std::vector<cricket::StreamParams>(); |
| 340 | } |
| 341 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 342 | bool IsValidOfferToReceiveMedia(int value) { |
| 343 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 344 | return (value >= Options::kUndefined) && |
| 345 | (value <= Options::kMaxOfferToReceiveMedia); |
| 346 | } |
| 347 | |
| 348 | // Add the stream and RTP data channel info to |session_options|. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 349 | void AddSendStreams( |
| 350 | cricket::MediaSessionOptions* session_options, |
| 351 | const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders, |
| 352 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 353 | rtp_data_channels) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 354 | session_options->streams.clear(); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 355 | for (const auto& sender : senders) { |
| 356 | session_options->AddSendStream(sender->media_type(), sender->id(), |
| 357 | sender->stream_id()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Check for data channels. |
| 361 | for (const auto& kv : rtp_data_channels) { |
| 362 | const DataChannel* channel = kv.second; |
| 363 | if (channel->state() == DataChannel::kConnecting || |
| 364 | channel->state() == DataChannel::kOpen) { |
| 365 | // |streamid| and |sync_label| are both set to the DataChannel label |
| 366 | // here so they can be signaled the same way as MediaStreams and Tracks. |
| 367 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 368 | // track label is the same as |streamid|. |
| 369 | const std::string& streamid = channel->label(); |
| 370 | const std::string& sync_label = channel->label(); |
| 371 | session_options->AddSendStream(cricket::MEDIA_TYPE_DATA, streamid, |
| 372 | sync_label); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 377 | } // namespace |
| 378 | |
| 379 | namespace webrtc { |
| 380 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 381 | // Factory class for creating remote MediaStreams and MediaStreamTracks. |
| 382 | class RemoteMediaStreamFactory { |
| 383 | public: |
| 384 | explicit RemoteMediaStreamFactory(rtc::Thread* signaling_thread, |
| 385 | cricket::ChannelManager* channel_manager) |
| 386 | : signaling_thread_(signaling_thread), |
| 387 | channel_manager_(channel_manager) {} |
| 388 | |
| 389 | rtc::scoped_refptr<MediaStreamInterface> CreateMediaStream( |
| 390 | const std::string& stream_label) { |
| 391 | return MediaStreamProxy::Create(signaling_thread_, |
| 392 | MediaStream::Create(stream_label)); |
| 393 | } |
| 394 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 395 | AudioTrackInterface* AddAudioTrack(uint32_t ssrc, |
| 396 | AudioProviderInterface* provider, |
| 397 | webrtc::MediaStreamInterface* stream, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 398 | const std::string& track_id) { |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 399 | return AddTrack<AudioTrackInterface, AudioTrack, AudioTrackProxy>( |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 400 | stream, track_id, RemoteAudioSource::Create(ssrc, provider)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | VideoTrackInterface* AddVideoTrack(webrtc::MediaStreamInterface* stream, |
| 404 | const std::string& track_id) { |
| 405 | return AddTrack<VideoTrackInterface, VideoTrack, VideoTrackProxy>( |
| 406 | stream, track_id, |
| 407 | VideoSource::Create(channel_manager_, new RemoteVideoCapturer(), |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 408 | nullptr, true) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 409 | .get()); |
| 410 | } |
| 411 | |
| 412 | private: |
| 413 | template <typename TI, typename T, typename TP, typename S> |
| 414 | TI* AddTrack(MediaStreamInterface* stream, |
| 415 | const std::string& track_id, |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 416 | const S& source) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 417 | rtc::scoped_refptr<TI> track( |
| 418 | TP::Create(signaling_thread_, T::Create(track_id, source))); |
| 419 | track->set_state(webrtc::MediaStreamTrackInterface::kLive); |
| 420 | if (stream->AddTrack(track)) { |
| 421 | return track; |
| 422 | } |
| 423 | return nullptr; |
| 424 | } |
| 425 | |
| 426 | rtc::Thread* signaling_thread_; |
| 427 | cricket::ChannelManager* channel_manager_; |
| 428 | }; |
| 429 | |
| 430 | bool ConvertRtcOptionsForOffer( |
| 431 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 432 | cricket::MediaSessionOptions* session_options) { |
| 433 | typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions; |
| 434 | if (!IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) || |
| 435 | !IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video)) { |
| 436 | return false; |
| 437 | } |
| 438 | |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 439 | if (rtc_options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 440 | session_options->recv_audio = (rtc_options.offer_to_receive_audio > 0); |
| 441 | } |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 442 | if (rtc_options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 443 | session_options->recv_video = (rtc_options.offer_to_receive_video > 0); |
| 444 | } |
| 445 | |
| 446 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 447 | session_options->audio_transport_options.ice_restart = |
| 448 | rtc_options.ice_restart; |
| 449 | session_options->video_transport_options.ice_restart = |
| 450 | rtc_options.ice_restart; |
| 451 | session_options->data_transport_options.ice_restart = rtc_options.ice_restart; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 452 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 453 | |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints, |
| 458 | cricket::MediaSessionOptions* session_options) { |
| 459 | bool value = false; |
| 460 | size_t mandatory_constraints_satisfied = 0; |
| 461 | |
| 462 | // kOfferToReceiveAudio defaults to true according to spec. |
| 463 | if (!FindConstraint(constraints, |
| 464 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 465 | &mandatory_constraints_satisfied) || |
| 466 | value) { |
| 467 | session_options->recv_audio = true; |
| 468 | } |
| 469 | |
| 470 | // kOfferToReceiveVideo defaults to false according to spec. But |
| 471 | // if it is an answer and video is offered, we should still accept video |
| 472 | // per default. |
| 473 | value = false; |
| 474 | if (!FindConstraint(constraints, |
| 475 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 476 | &mandatory_constraints_satisfied) || |
| 477 | value) { |
| 478 | session_options->recv_video = true; |
| 479 | } |
| 480 | |
| 481 | if (FindConstraint(constraints, |
| 482 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 483 | &mandatory_constraints_satisfied)) { |
| 484 | session_options->vad_enabled = value; |
| 485 | } |
| 486 | |
| 487 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 488 | &mandatory_constraints_satisfied)) { |
| 489 | session_options->bundle_enabled = value; |
| 490 | } else { |
| 491 | // kUseRtpMux defaults to true according to spec. |
| 492 | session_options->bundle_enabled = true; |
| 493 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 494 | |
| 495 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 496 | &value, &mandatory_constraints_satisfied)) { |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 497 | session_options->audio_transport_options.ice_restart = value; |
| 498 | session_options->video_transport_options.ice_restart = value; |
| 499 | session_options->data_transport_options.ice_restart = value; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 500 | } else { |
| 501 | // kIceRestart defaults to false according to spec. |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 502 | session_options->audio_transport_options.ice_restart = false; |
| 503 | session_options->video_transport_options.ice_restart = false; |
| 504 | session_options->data_transport_options.ice_restart = false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | if (!constraints) { |
| 508 | return true; |
| 509 | } |
| 510 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 511 | } |
| 512 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 513 | bool ParseIceServers(const PeerConnectionInterface::IceServers& servers, |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 514 | cricket::ServerAddresses* stun_servers, |
| 515 | std::vector<cricket::RelayServerConfig>* turn_servers) { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 516 | for (const webrtc::PeerConnectionInterface::IceServer& server : servers) { |
| 517 | if (!server.urls.empty()) { |
| 518 | for (const std::string& url : server.urls) { |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 519 | if (url.empty()) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 520 | LOG(LS_ERROR) << "Empty uri."; |
| 521 | return false; |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 522 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 523 | if (!ParseIceServerUrl(server, url, stun_servers, turn_servers)) { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 524 | return false; |
| 525 | } |
| 526 | } |
| 527 | } else if (!server.uri.empty()) { |
| 528 | // Fallback to old .uri if new .urls isn't present. |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 529 | if (!ParseIceServerUrl(server, server.uri, stun_servers, turn_servers)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 530 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 531 | } |
| 532 | } else { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 533 | LOG(LS_ERROR) << "Empty uri."; |
| 534 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 537 | // Candidates must have unique priorities, so that connectivity checks |
| 538 | // are performed in a well-defined order. |
| 539 | int priority = static_cast<int>(turn_servers->size() - 1); |
| 540 | for (cricket::RelayServerConfig& turn_server : *turn_servers) { |
| 541 | // First in the list gets highest priority. |
| 542 | turn_server.priority = priority--; |
| 543 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 544 | return true; |
| 545 | } |
| 546 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 547 | PeerConnection::PeerConnection(PeerConnectionFactory* factory) |
| 548 | : factory_(factory), |
| 549 | observer_(NULL), |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 550 | uma_observer_(NULL), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 551 | signaling_state_(kStable), |
| 552 | ice_state_(kIceNew), |
| 553 | ice_connection_state_(kIceConnectionNew), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 554 | ice_gathering_state_(kIceGatheringNew), |
| 555 | local_streams_(StreamCollection::Create()), |
| 556 | remote_streams_(StreamCollection::Create()) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 557 | |
| 558 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 559 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 560 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 561 | // Need to detach RTP senders/receivers from WebRtcSession, |
| 562 | // since it's about to be destroyed. |
| 563 | for (const auto& sender : senders_) { |
| 564 | sender->Stop(); |
| 565 | } |
| 566 | for (const auto& receiver : receivers_) { |
| 567 | receiver->Stop(); |
| 568 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 572 | const PeerConnectionInterface::RTCConfiguration& configuration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 573 | const MediaConstraintsInterface* constraints, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 574 | rtc::scoped_ptr<cricket::PortAllocator> allocator, |
| 575 | rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
| 576 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 577 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 578 | RTC_DCHECK(observer != nullptr); |
| 579 | if (!observer) { |
| 580 | return false; |
| 581 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 582 | observer_ = observer; |
| 583 | |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 584 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 585 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 586 | cricket::ServerAddresses stun_servers; |
| 587 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 588 | if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 589 | return false; |
| 590 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 591 | port_allocator_->SetIceServers(stun_servers, turn_servers); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 592 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 593 | // To handle both internal and externally created port allocator, we will |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 594 | // enable BUNDLE here. |
braveyao@webrtc.org | 1732df6 | 2014-10-27 03:01:37 +0000 | [diff] [blame] | 595 | int portallocator_flags = port_allocator_->flags(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 596 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 597 | cricket::PORTALLOCATOR_ENABLE_IPV6; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 598 | bool value; |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 599 | // If IPv6 flag was specified, we'll not override it by experiment. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 600 | if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, |
| 601 | &value, nullptr)) { |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 602 | if (!value) { |
| 603 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 604 | } |
guoweis@webrtc.org | 2c1bcea | 2014-09-23 16:23:02 +0000 | [diff] [blame] | 605 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") == |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 606 | "Disabled") { |
| 607 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 610 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 611 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
| 612 | LOG(LS_INFO) << "TCP candidates are disabled."; |
| 613 | } |
| 614 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 615 | port_allocator_->set_flags(portallocator_flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 616 | // No step delay is used while allocating ports. |
| 617 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 618 | |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 619 | // We rely on default values when constraints aren't found. |
| 620 | cricket::MediaConfig media_config; |
| 621 | |
| 622 | media_config.disable_prerenderer_smoothing = |
| 623 | configuration.disable_prerenderer_smoothing; |
| 624 | |
| 625 | // Find DSCP constraint. |
| 626 | FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, |
| 627 | &media_config.enable_dscp, NULL); |
| 628 | // Find constraints for cpu overuse detection. |
| 629 | FindConstraint(constraints, MediaConstraintsInterface::kCpuOveruseDetection, |
| 630 | &media_config.enable_cpu_overuse_detection, NULL); |
| 631 | |
| 632 | media_controller_.reset(factory_->CreateMediaController(media_config)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 633 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 634 | remote_stream_factory_.reset(new RemoteMediaStreamFactory( |
| 635 | factory_->signaling_thread(), media_controller_->channel_manager())); |
| 636 | |
| 637 | session_.reset( |
| 638 | new WebRtcSession(media_controller_.get(), factory_->signaling_thread(), |
| 639 | factory_->worker_thread(), port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 640 | stats_.reset(new StatsCollector(this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 641 | |
| 642 | // Initialize the WebRtcSession. It creates transport channels etc. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 643 | if (!session_->Initialize(factory_->options(), constraints, |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 644 | std::move(dtls_identity_store), configuration)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 645 | return false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 646 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 647 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 648 | // Register PeerConnection as receiver of local ice candidates. |
| 649 | // All the callbacks will be posted to the application from PeerConnection. |
| 650 | session_->RegisterIceObserver(this); |
| 651 | session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 652 | session_->SignalVoiceChannelDestroyed.connect( |
| 653 | this, &PeerConnection::OnVoiceChannelDestroyed); |
| 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); |
| 694 | stream_observers_.push_back(rtc::scoped_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(), |
| 721 | [local_stream](const rtc::scoped_ptr<MediaStreamObserver>& observer) { |
| 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. |
| 752 | rtc::scoped_refptr<RtpSenderInterface> new_sender; |
| 753 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 754 | new_sender = RtpSenderProxy::Create( |
| 755 | signaling_thread(), |
| 756 | new AudioRtpSender(static_cast<AudioTrackInterface*>(track), |
| 757 | session_.get(), stats_.get())); |
| 758 | if (!streams.empty()) { |
| 759 | new_sender->set_stream_id(streams[0]->label()); |
| 760 | } |
| 761 | const TrackInfo* track_info = FindTrackInfo( |
| 762 | local_audio_tracks_, new_sender->stream_id(), track->id()); |
| 763 | if (track_info) { |
| 764 | new_sender->SetSsrc(track_info->ssrc); |
| 765 | } |
| 766 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 767 | new_sender = RtpSenderProxy::Create( |
| 768 | signaling_thread(), |
| 769 | new VideoRtpSender(static_cast<VideoTrackInterface*>(track), |
| 770 | session_.get())); |
| 771 | if (!streams.empty()) { |
| 772 | new_sender->set_stream_id(streams[0]->label()); |
| 773 | } |
| 774 | const TrackInfo* track_info = FindTrackInfo( |
| 775 | local_video_tracks_, new_sender->stream_id(), track->id()); |
| 776 | if (track_info) { |
| 777 | new_sender->SetSsrc(track_info->ssrc); |
| 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 | } |
| 800 | (*it)->Stop(); |
| 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"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 810 | if (!track) { |
| 811 | LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
| 812 | return NULL; |
| 813 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 814 | if (!local_streams_->FindAudioTrack(track->id())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 815 | LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track."; |
| 816 | return NULL; |
| 817 | } |
| 818 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 819 | rtc::scoped_refptr<DtmfSenderInterface> sender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 820 | DtmfSender::Create(track, signaling_thread(), session_.get())); |
| 821 | if (!sender.get()) { |
| 822 | LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create."; |
| 823 | return NULL; |
| 824 | } |
| 825 | return DtmfSenderProxy::Create(signaling_thread(), sender.get()); |
| 826 | } |
| 827 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 828 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 829 | const std::string& kind, |
| 830 | const std::string& stream_id) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 831 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 832 | rtc::scoped_refptr<RtpSenderInterface> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 833 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 834 | new_sender = RtpSenderProxy::Create( |
| 835 | signaling_thread(), new AudioRtpSender(session_.get(), stats_.get())); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 836 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 837 | new_sender = RtpSenderProxy::Create(signaling_thread(), |
| 838 | new VideoRtpSender(session_.get())); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 839 | } else { |
| 840 | LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 841 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 842 | } |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 843 | if (!stream_id.empty()) { |
| 844 | new_sender->set_stream_id(stream_id); |
| 845 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 846 | senders_.push_back(new_sender); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 847 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 848 | } |
| 849 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 850 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 851 | const { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 852 | return senders_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 856 | PeerConnection::GetReceivers() const { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 857 | return receivers_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 858 | } |
| 859 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 860 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 861 | MediaStreamTrackInterface* track, |
| 862 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 863 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 864 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 865 | if (!VERIFY(observer != NULL)) { |
| 866 | LOG(LS_ERROR) << "GetStats - observer is NULL."; |
| 867 | return false; |
| 868 | } |
| 869 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 870 | stats_->UpdateStats(level); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 871 | signaling_thread()->Post(this, MSG_GETSTATS, |
| 872 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 873 | return true; |
| 874 | } |
| 875 | |
| 876 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 877 | return signaling_state_; |
| 878 | } |
| 879 | |
| 880 | PeerConnectionInterface::IceState PeerConnection::ice_state() { |
| 881 | return ice_state_; |
| 882 | } |
| 883 | |
| 884 | PeerConnectionInterface::IceConnectionState |
| 885 | PeerConnection::ice_connection_state() { |
| 886 | return ice_connection_state_; |
| 887 | } |
| 888 | |
| 889 | PeerConnectionInterface::IceGatheringState |
| 890 | PeerConnection::ice_gathering_state() { |
| 891 | return ice_gathering_state_; |
| 892 | } |
| 893 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 894 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 895 | PeerConnection::CreateDataChannel( |
| 896 | const std::string& label, |
| 897 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 898 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 899 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 900 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 901 | rtc::scoped_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 902 | if (config) { |
| 903 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 904 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 905 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 906 | InternalCreateDataChannel(label, internal_config.get())); |
| 907 | if (!channel.get()) { |
| 908 | return nullptr; |
| 909 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 910 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 911 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 912 | // the first SCTP DataChannel. |
| 913 | if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
| 914 | observer_->OnRenegotiationNeeded(); |
| 915 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 916 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 917 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 918 | } |
| 919 | |
| 920 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 921 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 922 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 923 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 924 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 925 | return; |
| 926 | } |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 927 | RTCOfferAnswerOptions options; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 928 | |
| 929 | bool value; |
| 930 | size_t mandatory_constraints = 0; |
| 931 | |
| 932 | if (FindConstraint(constraints, |
| 933 | MediaConstraintsInterface::kOfferToReceiveAudio, |
| 934 | &value, |
| 935 | &mandatory_constraints)) { |
| 936 | options.offer_to_receive_audio = |
| 937 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 938 | } |
| 939 | |
| 940 | if (FindConstraint(constraints, |
| 941 | MediaConstraintsInterface::kOfferToReceiveVideo, |
| 942 | &value, |
| 943 | &mandatory_constraints)) { |
| 944 | options.offer_to_receive_video = |
| 945 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 946 | } |
| 947 | |
| 948 | if (FindConstraint(constraints, |
| 949 | MediaConstraintsInterface::kVoiceActivityDetection, |
| 950 | &value, |
| 951 | &mandatory_constraints)) { |
| 952 | options.voice_activity_detection = value; |
| 953 | } |
| 954 | |
| 955 | if (FindConstraint(constraints, |
| 956 | MediaConstraintsInterface::kIceRestart, |
| 957 | &value, |
| 958 | &mandatory_constraints)) { |
| 959 | options.ice_restart = value; |
| 960 | } |
| 961 | |
| 962 | if (FindConstraint(constraints, |
| 963 | MediaConstraintsInterface::kUseRtpMux, |
| 964 | &value, |
| 965 | &mandatory_constraints)) { |
| 966 | options.use_rtp_mux = value; |
| 967 | } |
| 968 | |
| 969 | CreateOffer(observer, options); |
| 970 | } |
| 971 | |
| 972 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 973 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 974 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 975 | if (!VERIFY(observer != nullptr)) { |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 976 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 977 | return; |
| 978 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 979 | |
| 980 | cricket::MediaSessionOptions session_options; |
| 981 | if (!GetOptionsForOffer(options, &session_options)) { |
| 982 | std::string error = "CreateOffer called with invalid options."; |
| 983 | LOG(LS_ERROR) << error; |
| 984 | PostCreateSessionDescriptionFailure(observer, error); |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | session_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | void PeerConnection::CreateAnswer( |
| 992 | CreateSessionDescriptionObserver* observer, |
| 993 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 994 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 995 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 996 | LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 997 | return; |
| 998 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 999 | |
| 1000 | cricket::MediaSessionOptions session_options; |
| 1001 | if (!GetOptionsForAnswer(constraints, &session_options)) { |
| 1002 | std::string error = "CreateAnswer called with invalid constraints."; |
| 1003 | LOG(LS_ERROR) << error; |
| 1004 | PostCreateSessionDescriptionFailure(observer, error); |
| 1005 | return; |
| 1006 | } |
| 1007 | |
| 1008 | session_->CreateAnswer(observer, constraints, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | void PeerConnection::SetLocalDescription( |
| 1012 | SetSessionDescriptionObserver* observer, |
| 1013 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1014 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1015 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1016 | LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
| 1017 | return; |
| 1018 | } |
| 1019 | if (!desc) { |
| 1020 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1021 | return; |
| 1022 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1023 | // Update stats here so that we have the most recent stats for tracks and |
| 1024 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1025 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1026 | std::string error; |
| 1027 | if (!session_->SetLocalDescription(desc, &error)) { |
| 1028 | PostSetSessionDescriptionFailure(observer, error); |
| 1029 | return; |
| 1030 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1031 | |
| 1032 | // If setting the description decided our SSL role, allocate any necessary |
| 1033 | // SCTP sids. |
| 1034 | rtc::SSLRole role; |
| 1035 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 1036 | session_->GetSslRole(session_->data_channel(), &role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1037 | AllocateSctpSids(role); |
| 1038 | } |
| 1039 | |
| 1040 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 1041 | // local session description. |
| 1042 | const cricket::ContentInfo* audio_content = |
| 1043 | GetFirstAudioContent(desc->description()); |
| 1044 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1045 | if (audio_content->rejected) { |
| 1046 | RemoveTracks(cricket::MEDIA_TYPE_AUDIO); |
| 1047 | } else { |
| 1048 | const cricket::AudioContentDescription* audio_desc = |
| 1049 | static_cast<const cricket::AudioContentDescription*>( |
| 1050 | audio_content->description); |
| 1051 | UpdateLocalTracks(audio_desc->streams(), audio_desc->type()); |
| 1052 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | const cricket::ContentInfo* video_content = |
| 1056 | GetFirstVideoContent(desc->description()); |
| 1057 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1058 | if (video_content->rejected) { |
| 1059 | RemoveTracks(cricket::MEDIA_TYPE_VIDEO); |
| 1060 | } else { |
| 1061 | const cricket::VideoContentDescription* video_desc = |
| 1062 | static_cast<const cricket::VideoContentDescription*>( |
| 1063 | video_content->description); |
| 1064 | UpdateLocalTracks(video_desc->streams(), video_desc->type()); |
| 1065 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | const cricket::ContentInfo* data_content = |
| 1069 | GetFirstDataContent(desc->description()); |
| 1070 | if (data_content) { |
| 1071 | const cricket::DataContentDescription* data_desc = |
| 1072 | static_cast<const cricket::DataContentDescription*>( |
| 1073 | data_content->description); |
| 1074 | if (rtc::starts_with(data_desc->protocol().data(), |
| 1075 | cricket::kMediaProtocolRtpPrefix)) { |
| 1076 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1081 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1082 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1083 | // MaybeStartGathering needs to be called after posting |
| 1084 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1085 | // before signaling that SetLocalDescription completed. |
| 1086 | session_->MaybeStartGathering(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | void PeerConnection::SetRemoteDescription( |
| 1090 | SetSessionDescriptionObserver* observer, |
| 1091 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1092 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1093 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1094 | LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
| 1095 | return; |
| 1096 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1097 | if (!desc) { |
| 1098 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1099 | return; |
| 1100 | } |
| 1101 | // Update stats here so that we have the most recent stats for tracks and |
| 1102 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1103 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1104 | std::string error; |
| 1105 | if (!session_->SetRemoteDescription(desc, &error)) { |
| 1106 | PostSetSessionDescriptionFailure(observer, error); |
| 1107 | return; |
| 1108 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1109 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1110 | // If setting the description decided our SSL role, allocate any necessary |
| 1111 | // SCTP sids. |
| 1112 | rtc::SSLRole role; |
| 1113 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 1114 | session_->GetSslRole(session_->data_channel(), &role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1115 | AllocateSctpSids(role); |
| 1116 | } |
| 1117 | |
| 1118 | const cricket::SessionDescription* remote_desc = desc->description(); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1119 | const cricket::ContentInfo* audio_content = GetFirstAudioContent(remote_desc); |
| 1120 | const cricket::ContentInfo* video_content = GetFirstVideoContent(remote_desc); |
| 1121 | const cricket::AudioContentDescription* audio_desc = |
| 1122 | GetFirstAudioContentDescription(remote_desc); |
| 1123 | const cricket::VideoContentDescription* video_desc = |
| 1124 | GetFirstVideoContentDescription(remote_desc); |
| 1125 | const cricket::DataContentDescription* data_desc = |
| 1126 | GetFirstDataContentDescription(remote_desc); |
| 1127 | |
| 1128 | // Check if the descriptions include streams, just in case the peer supports |
| 1129 | // MSID, but doesn't indicate so with "a=msid-semantic". |
| 1130 | if (remote_desc->msid_supported() || |
| 1131 | (audio_desc && !audio_desc->streams().empty()) || |
| 1132 | (video_desc && !video_desc->streams().empty())) { |
| 1133 | remote_peer_supports_msid_ = true; |
| 1134 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1135 | |
| 1136 | // We wait to signal new streams until we finish processing the description, |
| 1137 | // since only at that point will new streams have all their tracks. |
| 1138 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 1139 | |
| 1140 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 1141 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1142 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1143 | if (audio_content->rejected) { |
| 1144 | RemoveTracks(cricket::MEDIA_TYPE_AUDIO); |
| 1145 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1146 | bool default_audio_track_needed = |
| 1147 | !remote_peer_supports_msid_ && |
| 1148 | MediaContentDirectionHasSend(audio_desc->direction()); |
| 1149 | UpdateRemoteStreamsList(GetActiveStreams(audio_desc), |
| 1150 | default_audio_track_needed, audio_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1151 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1152 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 1156 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1157 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1158 | if (video_content->rejected) { |
| 1159 | RemoveTracks(cricket::MEDIA_TYPE_VIDEO); |
| 1160 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1161 | bool default_video_track_needed = |
| 1162 | !remote_peer_supports_msid_ && |
| 1163 | MediaContentDirectionHasSend(video_desc->direction()); |
| 1164 | UpdateRemoteStreamsList(GetActiveStreams(video_desc), |
| 1165 | default_video_track_needed, video_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1166 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1167 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | // Update the DataChannels with the information from the remote peer. |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1171 | if (data_desc) { |
| 1172 | if (rtc::starts_with(data_desc->protocol().data(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1173 | cricket::kMediaProtocolRtpPrefix)) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1174 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 1179 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 1180 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 1181 | stats_->AddStream(new_stream); |
| 1182 | observer_->OnAddStream(new_stream); |
| 1183 | } |
| 1184 | |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1185 | UpdateEndedRemoteMediaStreams(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1186 | |
| 1187 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 1188 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame] | 1191 | bool PeerConnection::SetConfiguration(const RTCConfiguration& config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1192 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1193 | if (port_allocator_) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 1194 | cricket::ServerAddresses stun_servers; |
| 1195 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1196 | if (!ParseIceServers(config.servers, &stun_servers, &turn_servers)) { |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1197 | return false; |
| 1198 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 1199 | port_allocator_->SetIceServers(stun_servers, turn_servers); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1200 | } |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 1201 | session_->SetIceConfig(session_->ParseIceConfig(config)); |
mallinath@webrtc.org | 3d81b1b | 2014-09-09 14:38:10 +0000 | [diff] [blame] | 1202 | return session_->SetIceTransports(config.type); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1205 | bool PeerConnection::AddIceCandidate( |
| 1206 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1207 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1208 | return session_->ProcessIceMessage(ice_candidate); |
| 1209 | } |
| 1210 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1211 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1212 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1213 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1214 | |
| 1215 | if (session_) { |
| 1216 | session_->set_metrics_observer(uma_observer_); |
| 1217 | } |
| 1218 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1219 | // Send information about IPv4/IPv6 status. |
| 1220 | if (uma_observer_ && port_allocator_) { |
| 1221 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1222 | uma_observer_->IncrementEnumCounter( |
| 1223 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 1224 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 1225 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1226 | uma_observer_->IncrementEnumCounter( |
| 1227 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 1228 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1233 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
| 1234 | return session_->local_description(); |
| 1235 | } |
| 1236 | |
| 1237 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
| 1238 | return session_->remote_description(); |
| 1239 | } |
| 1240 | |
| 1241 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1242 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1243 | // Update stats here so that we have the most recent stats for tracks and |
| 1244 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1245 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1246 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1247 | session_->Close(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1250 | void PeerConnection::OnSessionStateChange(WebRtcSession* /*session*/, |
| 1251 | WebRtcSession::State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1252 | switch (state) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1253 | case WebRtcSession::STATE_INIT: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1254 | ChangeSignalingState(PeerConnectionInterface::kStable); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1255 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1256 | case WebRtcSession::STATE_SENTOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1257 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer); |
| 1258 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1259 | case WebRtcSession::STATE_SENTPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1260 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer); |
| 1261 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1262 | case WebRtcSession::STATE_RECEIVEDOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1263 | ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer); |
| 1264 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1265 | case WebRtcSession::STATE_RECEIVEDPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1266 | ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer); |
| 1267 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1268 | case WebRtcSession::STATE_INPROGRESS: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1269 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 1270 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1271 | case WebRtcSession::STATE_CLOSED: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1272 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 1273 | break; |
| 1274 | default: |
| 1275 | break; |
| 1276 | } |
| 1277 | } |
| 1278 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1279 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1280 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1281 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 1282 | SetSessionDescriptionMsg* param = |
| 1283 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1284 | param->observer->OnSuccess(); |
| 1285 | delete param; |
| 1286 | break; |
| 1287 | } |
| 1288 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 1289 | SetSessionDescriptionMsg* param = |
| 1290 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1291 | param->observer->OnFailure(param->error); |
| 1292 | delete param; |
| 1293 | break; |
| 1294 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1295 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 1296 | CreateSessionDescriptionMsg* param = |
| 1297 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 1298 | param->observer->OnFailure(param->error); |
| 1299 | delete param; |
| 1300 | break; |
| 1301 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1302 | case MSG_GETSTATS: { |
| 1303 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1304 | StatsReports reports; |
| 1305 | stats_->GetStats(param->track, &reports); |
| 1306 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1307 | delete param; |
| 1308 | break; |
| 1309 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1310 | case MSG_FREE_DATACHANNELS: { |
| 1311 | sctp_data_channels_to_free_.clear(); |
| 1312 | break; |
| 1313 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1314 | default: |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1315 | RTC_DCHECK(false && "Not implemented"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1320 | void PeerConnection::CreateAudioReceiver(MediaStreamInterface* stream, |
| 1321 | AudioTrackInterface* audio_track, |
| 1322 | uint32_t ssrc) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1323 | receivers_.push_back(RtpReceiverProxy::Create( |
| 1324 | signaling_thread(), |
| 1325 | new AudioRtpReceiver(audio_track, ssrc, session_.get()))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1328 | void PeerConnection::CreateVideoReceiver(MediaStreamInterface* stream, |
| 1329 | VideoTrackInterface* video_track, |
| 1330 | uint32_t ssrc) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1331 | receivers_.push_back(RtpReceiverProxy::Create( |
| 1332 | signaling_thread(), |
| 1333 | new VideoRtpReceiver(video_track, ssrc, session_.get()))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1336 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 1337 | // description. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1338 | void PeerConnection::DestroyAudioReceiver(MediaStreamInterface* stream, |
| 1339 | AudioTrackInterface* audio_track) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1340 | auto it = FindReceiverForTrack(audio_track); |
| 1341 | if (it == receivers_.end()) { |
| 1342 | LOG(LS_WARNING) << "RtpReceiver for track with id " << audio_track->id() |
| 1343 | << " doesn't exist."; |
| 1344 | } else { |
| 1345 | (*it)->Stop(); |
| 1346 | receivers_.erase(it); |
| 1347 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1350 | void PeerConnection::DestroyVideoReceiver(MediaStreamInterface* stream, |
| 1351 | VideoTrackInterface* video_track) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1352 | auto it = FindReceiverForTrack(video_track); |
| 1353 | if (it == receivers_.end()) { |
| 1354 | LOG(LS_WARNING) << "RtpReceiver for track with id " << video_track->id() |
| 1355 | << " doesn't exist."; |
| 1356 | } else { |
| 1357 | (*it)->Stop(); |
| 1358 | receivers_.erase(it); |
| 1359 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1360 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1361 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1362 | void PeerConnection::OnIceConnectionChange( |
| 1363 | PeerConnectionInterface::IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1364 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1365 | // After transitioning to "closed", ignore any additional states from |
| 1366 | // WebRtcSession (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1367 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1368 | return; |
| 1369 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1370 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1371 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | void PeerConnection::OnIceGatheringChange( |
| 1375 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1376 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1377 | if (IsClosed()) { |
| 1378 | return; |
| 1379 | } |
| 1380 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1381 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1385 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1386 | observer_->OnIceCandidate(candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1389 | void PeerConnection::OnIceConnectionReceivingChange(bool receiving) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1390 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1391 | observer_->OnIceConnectionReceivingChange(receiving); |
| 1392 | } |
| 1393 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1394 | void PeerConnection::ChangeSignalingState( |
| 1395 | PeerConnectionInterface::SignalingState signaling_state) { |
| 1396 | signaling_state_ = signaling_state; |
| 1397 | if (signaling_state == kClosed) { |
| 1398 | ice_connection_state_ = kIceConnectionClosed; |
| 1399 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 1400 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 1401 | ice_gathering_state_ = kIceGatheringComplete; |
| 1402 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 1403 | } |
| 1404 | } |
| 1405 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1408 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 1409 | MediaStreamInterface* stream) { |
| 1410 | auto sender = FindSenderForTrack(track); |
| 1411 | if (sender != senders_.end()) { |
| 1412 | // We already have a sender for this track, so just change the stream_id |
| 1413 | // so that it's correct in the next call to CreateOffer. |
| 1414 | (*sender)->set_stream_id(stream->label()); |
| 1415 | return; |
| 1416 | } |
| 1417 | |
| 1418 | // Normal case; we've never seen this track before. |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1419 | rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create( |
| 1420 | signaling_thread(), |
| 1421 | new AudioRtpSender(track, stream->label(), session_.get(), stats_.get())); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1422 | senders_.push_back(new_sender); |
| 1423 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 1424 | // which will connect the sender to the underlying transport. This can |
| 1425 | // occur if a local session description that contains the ID of the sender |
| 1426 | // is set before AddStream is called. It can also occur if the local |
| 1427 | // session description is not changed and RemoveStream is called, and |
| 1428 | // later AddStream is called again with the same stream. |
| 1429 | const TrackInfo* track_info = |
| 1430 | FindTrackInfo(local_audio_tracks_, stream->label(), track->id()); |
| 1431 | if (track_info) { |
| 1432 | new_sender->SetSsrc(track_info->ssrc); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 1437 | // indefinitely, when we have unified plan SDP. |
| 1438 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 1439 | MediaStreamInterface* stream) { |
| 1440 | auto sender = FindSenderForTrack(track); |
| 1441 | if (sender == senders_.end()) { |
| 1442 | LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 1443 | << " doesn't exist."; |
| 1444 | return; |
| 1445 | } |
| 1446 | (*sender)->Stop(); |
| 1447 | senders_.erase(sender); |
| 1448 | } |
| 1449 | |
| 1450 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 1451 | MediaStreamInterface* stream) { |
| 1452 | auto sender = FindSenderForTrack(track); |
| 1453 | if (sender != senders_.end()) { |
| 1454 | // We already have a sender for this track, so just change the stream_id |
| 1455 | // so that it's correct in the next call to CreateOffer. |
| 1456 | (*sender)->set_stream_id(stream->label()); |
| 1457 | return; |
| 1458 | } |
| 1459 | |
| 1460 | // Normal case; we've never seen this track before. |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1461 | rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create( |
| 1462 | signaling_thread(), |
| 1463 | new VideoRtpSender(track, stream->label(), session_.get())); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1464 | senders_.push_back(new_sender); |
| 1465 | const TrackInfo* track_info = |
| 1466 | FindTrackInfo(local_video_tracks_, stream->label(), track->id()); |
| 1467 | if (track_info) { |
| 1468 | new_sender->SetSsrc(track_info->ssrc); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 1473 | MediaStreamInterface* stream) { |
| 1474 | auto sender = FindSenderForTrack(track); |
| 1475 | if (sender == senders_.end()) { |
| 1476 | LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 1477 | << " doesn't exist."; |
| 1478 | return; |
| 1479 | } |
| 1480 | (*sender)->Stop(); |
| 1481 | senders_.erase(sender); |
| 1482 | } |
| 1483 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1484 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 1485 | SetSessionDescriptionObserver* observer, |
| 1486 | const std::string& error) { |
| 1487 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 1488 | msg->error = error; |
| 1489 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
| 1490 | } |
| 1491 | |
| 1492 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 1493 | CreateSessionDescriptionObserver* observer, |
| 1494 | const std::string& error) { |
| 1495 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 1496 | msg->error = error; |
| 1497 | signaling_thread()->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
| 1498 | } |
| 1499 | |
| 1500 | bool PeerConnection::GetOptionsForOffer( |
| 1501 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 1502 | cricket::MediaSessionOptions* session_options) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1503 | if (!ConvertRtcOptionsForOffer(rtc_options, session_options)) { |
| 1504 | return false; |
| 1505 | } |
| 1506 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1507 | AddSendStreams(session_options, senders_, rtp_data_channels_); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1508 | // Offer to receive audio/video if the constraint is not set and there are |
| 1509 | // send streams, or we're currently receiving. |
| 1510 | if (rtc_options.offer_to_receive_audio == RTCOfferAnswerOptions::kUndefined) { |
| 1511 | session_options->recv_audio = |
| 1512 | session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO) || |
| 1513 | !remote_audio_tracks_.empty(); |
| 1514 | } |
| 1515 | if (rtc_options.offer_to_receive_video == RTCOfferAnswerOptions::kUndefined) { |
| 1516 | session_options->recv_video = |
| 1517 | session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO) || |
| 1518 | !remote_video_tracks_.empty(); |
| 1519 | } |
| 1520 | session_options->bundle_enabled = |
| 1521 | session_options->bundle_enabled && |
| 1522 | (session_options->has_audio() || session_options->has_video() || |
| 1523 | session_options->has_data()); |
| 1524 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1525 | if (session_->data_channel_type() == cricket::DCT_SCTP && HasDataChannels()) { |
| 1526 | session_options->data_channel_type = cricket::DCT_SCTP; |
| 1527 | } |
| 1528 | return true; |
| 1529 | } |
| 1530 | |
| 1531 | bool PeerConnection::GetOptionsForAnswer( |
| 1532 | const MediaConstraintsInterface* constraints, |
| 1533 | cricket::MediaSessionOptions* session_options) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1534 | session_options->recv_audio = false; |
| 1535 | session_options->recv_video = false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1536 | if (!ParseConstraintsForAnswer(constraints, session_options)) { |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1540 | AddSendStreams(session_options, senders_, rtp_data_channels_); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1541 | session_options->bundle_enabled = |
| 1542 | session_options->bundle_enabled && |
| 1543 | (session_options->has_audio() || session_options->has_video() || |
| 1544 | session_options->has_data()); |
| 1545 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1546 | // RTP data channel is handled in MediaSessionOptions::AddStream. SCTP streams |
| 1547 | // are not signaled in the SDP so does not go through that path and must be |
| 1548 | // handled here. |
| 1549 | if (session_->data_channel_type() == cricket::DCT_SCTP) { |
| 1550 | session_options->data_channel_type = cricket::DCT_SCTP; |
| 1551 | } |
| 1552 | return true; |
| 1553 | } |
| 1554 | |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1555 | void PeerConnection::RemoveTracks(cricket::MediaType media_type) { |
| 1556 | UpdateLocalTracks(std::vector<cricket::StreamParams>(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1557 | UpdateRemoteStreamsList(std::vector<cricket::StreamParams>(), false, |
| 1558 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1561 | void PeerConnection::UpdateRemoteStreamsList( |
| 1562 | const cricket::StreamParamsVec& streams, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1563 | bool default_track_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1564 | cricket::MediaType media_type, |
| 1565 | StreamCollection* new_streams) { |
| 1566 | TrackInfos* current_tracks = GetRemoteTracks(media_type); |
| 1567 | |
| 1568 | // 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] | 1569 | // the new StreamParam. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1570 | auto track_it = current_tracks->begin(); |
| 1571 | while (track_it != current_tracks->end()) { |
| 1572 | const TrackInfo& info = *track_it; |
| 1573 | const cricket::StreamParams* params = |
| 1574 | cricket::GetStreamBySsrc(streams, info.ssrc); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1575 | bool track_exists = params && params->id == info.track_id; |
| 1576 | // If this is a default track, and we still need it, don't remove it. |
| 1577 | if ((info.stream_label == kDefaultStreamLabel && default_track_needed) || |
| 1578 | track_exists) { |
| 1579 | ++track_it; |
| 1580 | } else { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1581 | OnRemoteTrackRemoved(info.stream_label, info.track_id, media_type); |
| 1582 | track_it = current_tracks->erase(track_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | // Find new and active tracks. |
| 1587 | for (const cricket::StreamParams& params : streams) { |
| 1588 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 1589 | // track id. |
| 1590 | const std::string& stream_label = params.sync_label; |
| 1591 | const std::string& track_id = params.id; |
| 1592 | uint32_t ssrc = params.first_ssrc(); |
| 1593 | |
| 1594 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 1595 | remote_streams_->find(stream_label); |
| 1596 | if (!stream) { |
| 1597 | // This is a new MediaStream. Create a new remote MediaStream. |
| 1598 | stream = remote_stream_factory_->CreateMediaStream(stream_label); |
| 1599 | remote_streams_->AddStream(stream); |
| 1600 | new_streams->AddStream(stream); |
| 1601 | } |
| 1602 | |
| 1603 | const TrackInfo* track_info = |
| 1604 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 1605 | if (!track_info) { |
| 1606 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 1607 | OnRemoteTrackSeen(stream_label, track_id, ssrc, media_type); |
| 1608 | } |
| 1609 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1610 | |
| 1611 | // Add default track if necessary. |
| 1612 | if (default_track_needed) { |
| 1613 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
| 1614 | remote_streams_->find(kDefaultStreamLabel); |
| 1615 | if (!default_stream) { |
| 1616 | // Create the new default MediaStream. |
| 1617 | default_stream = |
| 1618 | remote_stream_factory_->CreateMediaStream(kDefaultStreamLabel); |
| 1619 | remote_streams_->AddStream(default_stream); |
| 1620 | new_streams->AddStream(default_stream); |
| 1621 | } |
| 1622 | std::string default_track_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 1623 | ? kDefaultAudioTrackLabel |
| 1624 | : kDefaultVideoTrackLabel; |
| 1625 | const TrackInfo* default_track_info = |
| 1626 | FindTrackInfo(*current_tracks, kDefaultStreamLabel, default_track_id); |
| 1627 | if (!default_track_info) { |
| 1628 | current_tracks->push_back( |
| 1629 | TrackInfo(kDefaultStreamLabel, default_track_id, 0)); |
| 1630 | OnRemoteTrackSeen(kDefaultStreamLabel, default_track_id, 0, media_type); |
| 1631 | } |
| 1632 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | void PeerConnection::OnRemoteTrackSeen(const std::string& stream_label, |
| 1636 | const std::string& track_id, |
| 1637 | uint32_t ssrc, |
| 1638 | cricket::MediaType media_type) { |
| 1639 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 1640 | |
| 1641 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1642 | AudioTrackInterface* audio_track = remote_stream_factory_->AddAudioTrack( |
| 1643 | ssrc, session_.get(), stream, track_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1644 | CreateAudioReceiver(stream, audio_track, ssrc); |
| 1645 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1646 | VideoTrackInterface* video_track = |
| 1647 | remote_stream_factory_->AddVideoTrack(stream, track_id); |
| 1648 | CreateVideoReceiver(stream, video_track, ssrc); |
| 1649 | } else { |
| 1650 | RTC_DCHECK(false && "Invalid media type"); |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | void PeerConnection::OnRemoteTrackRemoved(const std::string& stream_label, |
| 1655 | const std::string& track_id, |
| 1656 | cricket::MediaType media_type) { |
| 1657 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 1658 | |
| 1659 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1660 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 1661 | stream->FindAudioTrack(track_id); |
| 1662 | if (audio_track) { |
| 1663 | audio_track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1664 | stream->RemoveTrack(audio_track); |
| 1665 | DestroyAudioReceiver(stream, audio_track); |
| 1666 | } |
| 1667 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1668 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1669 | stream->FindVideoTrack(track_id); |
| 1670 | if (video_track) { |
| 1671 | video_track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1672 | stream->RemoveTrack(video_track); |
| 1673 | DestroyVideoReceiver(stream, video_track); |
| 1674 | } |
| 1675 | } else { |
| 1676 | ASSERT(false && "Invalid media type"); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 1681 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 1682 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 1683 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 1684 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 1685 | streams_to_remove.push_back(stream); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | for (const auto& stream : streams_to_remove) { |
| 1690 | remote_streams_->RemoveStream(stream); |
| 1691 | observer_->OnRemoveStream(stream); |
| 1692 | } |
| 1693 | } |
| 1694 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1695 | void PeerConnection::EndRemoteTracks(cricket::MediaType media_type) { |
| 1696 | TrackInfos* current_tracks = GetRemoteTracks(media_type); |
| 1697 | for (TrackInfos::iterator track_it = current_tracks->begin(); |
| 1698 | track_it != current_tracks->end(); ++track_it) { |
| 1699 | const TrackInfo& info = *track_it; |
| 1700 | MediaStreamInterface* stream = remote_streams_->find(info.stream_label); |
| 1701 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1702 | AudioTrackInterface* track = stream->FindAudioTrack(info.track_id); |
| 1703 | // There's no guarantee the track is still available, e.g. the track may |
| 1704 | // have been removed from the stream by javascript. |
| 1705 | if (track) { |
| 1706 | track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1707 | } |
| 1708 | } |
| 1709 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1710 | VideoTrackInterface* track = stream->FindVideoTrack(info.track_id); |
| 1711 | // There's no guarantee the track is still available, e.g. the track may |
| 1712 | // have been removed from the stream by javascript. |
| 1713 | if (track) { |
| 1714 | track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | void PeerConnection::UpdateLocalTracks( |
| 1721 | const std::vector<cricket::StreamParams>& streams, |
| 1722 | cricket::MediaType media_type) { |
| 1723 | TrackInfos* current_tracks = GetLocalTracks(media_type); |
| 1724 | |
| 1725 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 1726 | // don't match the new StreamParam. |
| 1727 | TrackInfos::iterator track_it = current_tracks->begin(); |
| 1728 | while (track_it != current_tracks->end()) { |
| 1729 | const TrackInfo& info = *track_it; |
| 1730 | const cricket::StreamParams* params = |
| 1731 | cricket::GetStreamBySsrc(streams, info.ssrc); |
| 1732 | if (!params || params->id != info.track_id || |
| 1733 | params->sync_label != info.stream_label) { |
| 1734 | OnLocalTrackRemoved(info.stream_label, info.track_id, info.ssrc, |
| 1735 | media_type); |
| 1736 | track_it = current_tracks->erase(track_it); |
| 1737 | } else { |
| 1738 | ++track_it; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | // Find new and active tracks. |
| 1743 | for (const cricket::StreamParams& params : streams) { |
| 1744 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 1745 | // track id. |
| 1746 | const std::string& stream_label = params.sync_label; |
| 1747 | const std::string& track_id = params.id; |
| 1748 | uint32_t ssrc = params.first_ssrc(); |
| 1749 | const TrackInfo* track_info = |
| 1750 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 1751 | if (!track_info) { |
| 1752 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 1753 | OnLocalTrackSeen(stream_label, track_id, params.first_ssrc(), media_type); |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | void PeerConnection::OnLocalTrackSeen(const std::string& stream_label, |
| 1759 | const std::string& track_id, |
| 1760 | uint32_t ssrc, |
| 1761 | cricket::MediaType media_type) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1762 | RtpSenderInterface* sender = FindSenderById(track_id); |
| 1763 | if (!sender) { |
| 1764 | LOG(LS_WARNING) << "An unknown RtpSender with id " << track_id |
| 1765 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1766 | return; |
| 1767 | } |
| 1768 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1769 | if (sender->media_type() != media_type) { |
| 1770 | LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 1771 | << " description with an unexpected media type."; |
| 1772 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1773 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1774 | |
| 1775 | sender->set_stream_id(stream_label); |
| 1776 | sender->SetSsrc(ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | void PeerConnection::OnLocalTrackRemoved(const std::string& stream_label, |
| 1780 | const std::string& track_id, |
| 1781 | uint32_t ssrc, |
| 1782 | cricket::MediaType media_type) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1783 | RtpSenderInterface* sender = FindSenderById(track_id); |
| 1784 | if (!sender) { |
| 1785 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1786 | // SessionDescriptions has been renegotiated. |
| 1787 | return; |
| 1788 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1789 | |
| 1790 | // A sender has been removed from the SessionDescription but it's still |
| 1791 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 1792 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 1793 | if (sender->media_type() != media_type) { |
| 1794 | LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 1795 | << " description with an unexpected media type."; |
| 1796 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1797 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1798 | |
| 1799 | sender->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 1803 | const cricket::StreamParamsVec& streams) { |
| 1804 | std::vector<std::string> existing_channels; |
| 1805 | |
| 1806 | // Find new and active data channels. |
| 1807 | for (const cricket::StreamParams& params : streams) { |
| 1808 | // |it->sync_label| is actually the data channel label. The reason is that |
| 1809 | // we use the same naming of data channels as we do for |
| 1810 | // MediaStreams and Tracks. |
| 1811 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 1812 | // track label is the same as |streamid|. |
| 1813 | const std::string& channel_label = params.sync_label; |
| 1814 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
| 1815 | if (!VERIFY(data_channel_it != rtp_data_channels_.end())) { |
| 1816 | continue; |
| 1817 | } |
| 1818 | // Set the SSRC the data channel should use for sending. |
| 1819 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 1820 | existing_channels.push_back(data_channel_it->first); |
| 1821 | } |
| 1822 | |
| 1823 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 1824 | } |
| 1825 | |
| 1826 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 1827 | const cricket::StreamParamsVec& streams) { |
| 1828 | std::vector<std::string> existing_channels; |
| 1829 | |
| 1830 | // Find new and active data channels. |
| 1831 | for (const cricket::StreamParams& params : streams) { |
| 1832 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 1833 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 1834 | std::string label = params.sync_label.empty() |
| 1835 | ? rtc::ToString(params.first_ssrc()) |
| 1836 | : params.sync_label; |
| 1837 | auto data_channel_it = rtp_data_channels_.find(label); |
| 1838 | if (data_channel_it == rtp_data_channels_.end()) { |
| 1839 | // This is a new data channel. |
| 1840 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 1841 | } else { |
| 1842 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 1843 | } |
| 1844 | existing_channels.push_back(label); |
| 1845 | } |
| 1846 | |
| 1847 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 1848 | } |
| 1849 | |
| 1850 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 1851 | const std::vector<std::string>& active_channels, |
| 1852 | bool is_local_update) { |
| 1853 | auto it = rtp_data_channels_.begin(); |
| 1854 | while (it != rtp_data_channels_.end()) { |
| 1855 | DataChannel* data_channel = it->second; |
| 1856 | if (std::find(active_channels.begin(), active_channels.end(), |
| 1857 | data_channel->label()) != active_channels.end()) { |
| 1858 | ++it; |
| 1859 | continue; |
| 1860 | } |
| 1861 | |
| 1862 | if (is_local_update) { |
| 1863 | data_channel->SetSendSsrc(0); |
| 1864 | } else { |
| 1865 | data_channel->RemotePeerRequestClose(); |
| 1866 | } |
| 1867 | |
| 1868 | if (data_channel->state() == DataChannel::kClosed) { |
| 1869 | rtp_data_channels_.erase(it); |
| 1870 | it = rtp_data_channels_.begin(); |
| 1871 | } else { |
| 1872 | ++it; |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 1878 | uint32_t remote_ssrc) { |
| 1879 | rtc::scoped_refptr<DataChannel> channel( |
| 1880 | InternalCreateDataChannel(label, nullptr)); |
| 1881 | if (!channel.get()) { |
| 1882 | LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 1883 | << "CreateDataChannel failed."; |
| 1884 | return; |
| 1885 | } |
| 1886 | channel->SetReceiveSsrc(remote_ssrc); |
| 1887 | observer_->OnDataChannel( |
| 1888 | DataChannelProxy::Create(signaling_thread(), channel)); |
| 1889 | } |
| 1890 | |
| 1891 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 1892 | const std::string& label, |
| 1893 | const InternalDataChannelInit* config) { |
| 1894 | if (IsClosed()) { |
| 1895 | return nullptr; |
| 1896 | } |
| 1897 | if (session_->data_channel_type() == cricket::DCT_NONE) { |
| 1898 | LOG(LS_ERROR) |
| 1899 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 1900 | return nullptr; |
| 1901 | } |
| 1902 | InternalDataChannelInit new_config = |
| 1903 | config ? (*config) : InternalDataChannelInit(); |
| 1904 | if (session_->data_channel_type() == cricket::DCT_SCTP) { |
| 1905 | if (new_config.id < 0) { |
| 1906 | rtc::SSLRole role; |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 1907 | if ((session_->GetSslRole(session_->data_channel(), &role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1908 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
| 1909 | LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel."; |
| 1910 | return nullptr; |
| 1911 | } |
| 1912 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
| 1913 | LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 1914 | << "because the id is already in use or out of range."; |
| 1915 | return nullptr; |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | rtc::scoped_refptr<DataChannel> channel(DataChannel::Create( |
| 1920 | session_.get(), session_->data_channel_type(), label, new_config)); |
| 1921 | if (!channel) { |
| 1922 | sid_allocator_.ReleaseSid(new_config.id); |
| 1923 | return nullptr; |
| 1924 | } |
| 1925 | |
| 1926 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 1927 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
| 1928 | LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 1929 | << " already exists."; |
| 1930 | return nullptr; |
| 1931 | } |
| 1932 | rtp_data_channels_[channel->label()] = channel; |
| 1933 | } else { |
| 1934 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 1935 | sctp_data_channels_.push_back(channel); |
| 1936 | channel->SignalClosed.connect(this, |
| 1937 | &PeerConnection::OnSctpDataChannelClosed); |
| 1938 | } |
| 1939 | |
| 1940 | return channel; |
| 1941 | } |
| 1942 | |
| 1943 | bool PeerConnection::HasDataChannels() const { |
| 1944 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 1945 | } |
| 1946 | |
| 1947 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 1948 | for (const auto& channel : sctp_data_channels_) { |
| 1949 | if (channel->id() < 0) { |
| 1950 | int sid; |
| 1951 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
| 1952 | LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
| 1953 | continue; |
| 1954 | } |
| 1955 | channel->SetSctpSid(sid); |
| 1956 | } |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1961 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1962 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 1963 | ++it) { |
| 1964 | if (it->get() == channel) { |
| 1965 | if (channel->id() >= 0) { |
| 1966 | sid_allocator_.ReleaseSid(channel->id()); |
| 1967 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1968 | // Since this method is triggered by a signal from the DataChannel, |
| 1969 | // we can't free it directly here; we need to free it asynchronously. |
| 1970 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1971 | sctp_data_channels_.erase(it); |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1972 | signaling_thread()->Post(this, MSG_FREE_DATACHANNELS, nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1973 | return; |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | void PeerConnection::OnVoiceChannelDestroyed() { |
| 1979 | EndRemoteTracks(cricket::MEDIA_TYPE_AUDIO); |
| 1980 | } |
| 1981 | |
| 1982 | void PeerConnection::OnVideoChannelDestroyed() { |
| 1983 | EndRemoteTracks(cricket::MEDIA_TYPE_VIDEO); |
| 1984 | } |
| 1985 | |
| 1986 | void PeerConnection::OnDataChannelCreated() { |
| 1987 | for (const auto& channel : sctp_data_channels_) { |
| 1988 | channel->OnTransportChannelCreated(); |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | void PeerConnection::OnDataChannelDestroyed() { |
| 1993 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 1994 | // DataChannel may callback to us and try to modify the list. |
| 1995 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 1996 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 1997 | for (const auto& kv : temp_rtp_dcs) { |
| 1998 | kv.second->OnTransportChannelDestroyed(); |
| 1999 | } |
| 2000 | |
| 2001 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 2002 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 2003 | for (const auto& channel : temp_sctp_dcs) { |
| 2004 | channel->OnTransportChannelDestroyed(); |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | void PeerConnection::OnDataChannelOpenMessage( |
| 2009 | const std::string& label, |
| 2010 | const InternalDataChannelInit& config) { |
| 2011 | rtc::scoped_refptr<DataChannel> channel( |
| 2012 | InternalCreateDataChannel(label, &config)); |
| 2013 | if (!channel.get()) { |
| 2014 | LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
| 2015 | return; |
| 2016 | } |
| 2017 | |
| 2018 | observer_->OnDataChannel( |
| 2019 | DataChannelProxy::Create(signaling_thread(), channel)); |
| 2020 | } |
| 2021 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2022 | RtpSenderInterface* PeerConnection::FindSenderById(const std::string& id) { |
| 2023 | auto it = |
| 2024 | std::find_if(senders_.begin(), senders_.end(), |
| 2025 | [id](const rtc::scoped_refptr<RtpSenderInterface>& sender) { |
| 2026 | return sender->id() == id; |
| 2027 | }); |
| 2028 | return it != senders_.end() ? it->get() : nullptr; |
| 2029 | } |
| 2030 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2031 | std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator |
| 2032 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) { |
| 2033 | return std::find_if( |
| 2034 | senders_.begin(), senders_.end(), |
| 2035 | [track](const rtc::scoped_refptr<RtpSenderInterface>& sender) { |
| 2036 | return sender->track() == track; |
| 2037 | }); |
| 2038 | } |
| 2039 | |
| 2040 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator |
| 2041 | PeerConnection::FindReceiverForTrack(MediaStreamTrackInterface* track) { |
| 2042 | return std::find_if( |
| 2043 | receivers_.begin(), receivers_.end(), |
| 2044 | [track](const rtc::scoped_refptr<RtpReceiverInterface>& receiver) { |
| 2045 | return receiver->track() == track; |
| 2046 | }); |
| 2047 | } |
| 2048 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2049 | PeerConnection::TrackInfos* PeerConnection::GetRemoteTracks( |
| 2050 | cricket::MediaType media_type) { |
| 2051 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2052 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 2053 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &remote_audio_tracks_ |
| 2054 | : &remote_video_tracks_; |
| 2055 | } |
| 2056 | |
| 2057 | PeerConnection::TrackInfos* PeerConnection::GetLocalTracks( |
| 2058 | cricket::MediaType media_type) { |
| 2059 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2060 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 2061 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_tracks_ |
| 2062 | : &local_video_tracks_; |
| 2063 | } |
| 2064 | |
| 2065 | const PeerConnection::TrackInfo* PeerConnection::FindTrackInfo( |
| 2066 | const PeerConnection::TrackInfos& infos, |
| 2067 | const std::string& stream_label, |
| 2068 | const std::string track_id) const { |
| 2069 | for (const TrackInfo& track_info : infos) { |
| 2070 | if (track_info.stream_label == stream_label && |
| 2071 | track_info.track_id == track_id) { |
| 2072 | return &track_info; |
| 2073 | } |
| 2074 | } |
| 2075 | return nullptr; |
| 2076 | } |
| 2077 | |
| 2078 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 2079 | for (const auto& channel : sctp_data_channels_) { |
| 2080 | if (channel->id() == sid) { |
| 2081 | return channel; |
| 2082 | } |
| 2083 | } |
| 2084 | return nullptr; |
| 2085 | } |
| 2086 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2087 | } // namespace webrtc |