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, |
Per | 0f13ec1 | 2016-03-03 09:22:28 +0100 | [diff] [blame] | 385 | rtc::Thread* worker_thread) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 386 | : signaling_thread_(signaling_thread), |
Per | 0f13ec1 | 2016-03-03 09:22:28 +0100 | [diff] [blame] | 387 | worker_thread_(worker_thread) {} |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 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, |
Per | 0f13ec1 | 2016-03-03 09:22:28 +0100 | [diff] [blame] | 407 | VideoSource::Create(worker_thread_, 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_; |
Per | 0f13ec1 | 2016-03-03 09:22:28 +0100 | [diff] [blame] | 427 | rtc::Thread* worker_thread_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 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; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 447 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 448 | for (auto& kv : session_options->transport_options) { |
| 449 | kv.second.ice_restart = rtc_options.ice_restart; |
| 450 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 451 | |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints, |
| 456 | cricket::MediaSessionOptions* session_options) { |
| 457 | bool value = false; |
| 458 | size_t mandatory_constraints_satisfied = 0; |
| 459 | |
| 460 | // kOfferToReceiveAudio defaults to true according to spec. |
| 461 | if (!FindConstraint(constraints, |
| 462 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 463 | &mandatory_constraints_satisfied) || |
| 464 | value) { |
| 465 | session_options->recv_audio = true; |
| 466 | } |
| 467 | |
| 468 | // kOfferToReceiveVideo defaults to false according to spec. But |
| 469 | // if it is an answer and video is offered, we should still accept video |
| 470 | // per default. |
| 471 | value = false; |
| 472 | if (!FindConstraint(constraints, |
| 473 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 474 | &mandatory_constraints_satisfied) || |
| 475 | value) { |
| 476 | session_options->recv_video = true; |
| 477 | } |
| 478 | |
| 479 | if (FindConstraint(constraints, |
| 480 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 481 | &mandatory_constraints_satisfied)) { |
| 482 | session_options->vad_enabled = value; |
| 483 | } |
| 484 | |
| 485 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 486 | &mandatory_constraints_satisfied)) { |
| 487 | session_options->bundle_enabled = value; |
| 488 | } else { |
| 489 | // kUseRtpMux defaults to true according to spec. |
| 490 | session_options->bundle_enabled = true; |
| 491 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 492 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 493 | bool ice_restart = false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 494 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 495 | &value, &mandatory_constraints_satisfied)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 496 | // kIceRestart defaults to false according to spec. |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 497 | ice_restart = true; |
| 498 | } |
| 499 | for (auto& kv : session_options->transport_options) { |
| 500 | kv.second.ice_restart = ice_restart; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (!constraints) { |
| 504 | return true; |
| 505 | } |
| 506 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 507 | } |
| 508 | |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 509 | bool ParseIceServers(const PeerConnectionInterface::IceServers& servers, |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 510 | cricket::ServerAddresses* stun_servers, |
| 511 | std::vector<cricket::RelayServerConfig>* turn_servers) { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 512 | for (const webrtc::PeerConnectionInterface::IceServer& server : servers) { |
| 513 | if (!server.urls.empty()) { |
| 514 | for (const std::string& url : server.urls) { |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 515 | if (url.empty()) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 516 | LOG(LS_ERROR) << "Empty uri."; |
| 517 | return false; |
Joachim Bauch | d935f91 | 2015-05-29 22:14:21 +0200 | [diff] [blame] | 518 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 519 | if (!ParseIceServerUrl(server, url, stun_servers, turn_servers)) { |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | } else if (!server.uri.empty()) { |
| 524 | // Fallback to old .uri if new .urls isn't present. |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 525 | if (!ParseIceServerUrl(server, server.uri, stun_servers, turn_servers)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 526 | return false; |
Joachim Bauch | 7c4e745 | 2015-05-28 23:06:30 +0200 | [diff] [blame] | 527 | } |
| 528 | } else { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 529 | LOG(LS_ERROR) << "Empty uri."; |
| 530 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 533 | // Candidates must have unique priorities, so that connectivity checks |
| 534 | // are performed in a well-defined order. |
| 535 | int priority = static_cast<int>(turn_servers->size() - 1); |
| 536 | for (cricket::RelayServerConfig& turn_server : *turn_servers) { |
| 537 | // First in the list gets highest priority. |
| 538 | turn_server.priority = priority--; |
| 539 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 543 | PeerConnection::PeerConnection(PeerConnectionFactory* factory) |
| 544 | : factory_(factory), |
| 545 | observer_(NULL), |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 546 | uma_observer_(NULL), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 547 | signaling_state_(kStable), |
| 548 | ice_state_(kIceNew), |
| 549 | ice_connection_state_(kIceConnectionNew), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 550 | ice_gathering_state_(kIceGatheringNew), |
| 551 | local_streams_(StreamCollection::Create()), |
| 552 | remote_streams_(StreamCollection::Create()) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 553 | |
| 554 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 555 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 556 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 557 | // Need to detach RTP senders/receivers from WebRtcSession, |
| 558 | // since it's about to be destroyed. |
| 559 | for (const auto& sender : senders_) { |
| 560 | sender->Stop(); |
| 561 | } |
| 562 | for (const auto& receiver : receivers_) { |
| 563 | receiver->Stop(); |
| 564 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 568 | const PeerConnectionInterface::RTCConfiguration& configuration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 569 | const MediaConstraintsInterface* constraints, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 570 | rtc::scoped_ptr<cricket::PortAllocator> allocator, |
| 571 | rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
| 572 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 573 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 574 | RTC_DCHECK(observer != nullptr); |
| 575 | if (!observer) { |
| 576 | return false; |
| 577 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 578 | observer_ = observer; |
| 579 | |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 580 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 581 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 582 | cricket::ServerAddresses stun_servers; |
| 583 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 584 | if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 585 | return false; |
| 586 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 587 | port_allocator_->SetIceServers(stun_servers, turn_servers); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 588 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 589 | // To handle both internal and externally created port allocator, we will |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 590 | // enable BUNDLE here. |
braveyao@webrtc.org | 1732df6 | 2014-10-27 03:01:37 +0000 | [diff] [blame] | 591 | int portallocator_flags = port_allocator_->flags(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 592 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 593 | cricket::PORTALLOCATOR_ENABLE_IPV6; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 594 | bool value; |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 595 | // If IPv6 flag was specified, we'll not override it by experiment. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 596 | if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, |
| 597 | &value, nullptr)) { |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 598 | if (!value) { |
| 599 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
guoweis@webrtc.org | 97ed393 | 2014-09-19 21:06:12 +0000 | [diff] [blame] | 600 | } |
guoweis@webrtc.org | 2c1bcea | 2014-09-23 16:23:02 +0000 | [diff] [blame] | 601 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") == |
guoweis@webrtc.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 602 | "Disabled") { |
| 603 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 606 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 607 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
| 608 | LOG(LS_INFO) << "TCP candidates are disabled."; |
| 609 | } |
| 610 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 611 | port_allocator_->set_flags(portallocator_flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 612 | // No step delay is used while allocating ports. |
| 613 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 614 | |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 615 | // We rely on default values when constraints aren't found. |
| 616 | cricket::MediaConfig media_config; |
| 617 | |
nisse | 0db023a | 2016-03-01 04:29:59 -0800 | [diff] [blame] | 618 | media_config.video.disable_prerenderer_smoothing = |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 619 | configuration.disable_prerenderer_smoothing; |
| 620 | |
| 621 | // Find DSCP constraint. |
| 622 | FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, |
| 623 | &media_config.enable_dscp, NULL); |
nisse | 0db023a | 2016-03-01 04:29:59 -0800 | [diff] [blame] | 624 | // Find constraint for cpu overuse detection. |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 625 | FindConstraint(constraints, MediaConstraintsInterface::kCpuOveruseDetection, |
nisse | 0db023a | 2016-03-01 04:29:59 -0800 | [diff] [blame] | 626 | &media_config.video.enable_cpu_overuse_detection, NULL); |
| 627 | |
| 628 | // Find Suspend Below Min Bitrate constraint. |
| 629 | FindConstraint(constraints, |
| 630 | MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate, |
| 631 | &media_config.video.suspend_below_min_bitrate, NULL); |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 632 | |
| 633 | media_controller_.reset(factory_->CreateMediaController(media_config)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 634 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 635 | remote_stream_factory_.reset(new RemoteMediaStreamFactory( |
Per | 0f13ec1 | 2016-03-03 09:22:28 +0100 | [diff] [blame] | 636 | factory_->signaling_thread(), factory_->worker_thread())); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 637 | |
| 638 | session_.reset( |
| 639 | new WebRtcSession(media_controller_.get(), factory_->signaling_thread(), |
| 640 | factory_->worker_thread(), port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 641 | stats_.reset(new StatsCollector(this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 642 | |
| 643 | // Initialize the WebRtcSession. It creates transport channels etc. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 644 | if (!session_->Initialize(factory_->options(), constraints, |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 645 | std::move(dtls_identity_store), configuration)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 646 | return false; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 647 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 648 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 649 | // Register PeerConnection as receiver of local ice candidates. |
| 650 | // All the callbacks will be posted to the application from PeerConnection. |
| 651 | session_->RegisterIceObserver(this); |
| 652 | session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 653 | session_->SignalVoiceChannelDestroyed.connect( |
| 654 | this, &PeerConnection::OnVoiceChannelDestroyed); |
| 655 | session_->SignalVideoChannelDestroyed.connect( |
| 656 | this, &PeerConnection::OnVideoChannelDestroyed); |
| 657 | session_->SignalDataChannelCreated.connect( |
| 658 | this, &PeerConnection::OnDataChannelCreated); |
| 659 | session_->SignalDataChannelDestroyed.connect( |
| 660 | this, &PeerConnection::OnDataChannelDestroyed); |
| 661 | session_->SignalDataChannelOpenMessage.connect( |
| 662 | this, &PeerConnection::OnDataChannelOpenMessage); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 663 | return true; |
| 664 | } |
| 665 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 666 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 667 | PeerConnection::local_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 668 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 669 | } |
| 670 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 671 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 672 | PeerConnection::remote_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 673 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 674 | } |
| 675 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 676 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 677 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 678 | if (IsClosed()) { |
| 679 | return false; |
| 680 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 681 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 682 | return false; |
| 683 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 684 | |
| 685 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 686 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 687 | observer->SignalAudioTrackAdded.connect(this, |
| 688 | &PeerConnection::OnAudioTrackAdded); |
| 689 | observer->SignalAudioTrackRemoved.connect( |
| 690 | this, &PeerConnection::OnAudioTrackRemoved); |
| 691 | observer->SignalVideoTrackAdded.connect(this, |
| 692 | &PeerConnection::OnVideoTrackAdded); |
| 693 | observer->SignalVideoTrackRemoved.connect( |
| 694 | this, &PeerConnection::OnVideoTrackRemoved); |
| 695 | stream_observers_.push_back(rtc::scoped_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 696 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 697 | for (const auto& track : local_stream->GetAudioTracks()) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 698 | OnAudioTrackAdded(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 699 | } |
| 700 | for (const auto& track : local_stream->GetVideoTracks()) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 701 | OnVideoTrackAdded(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 702 | } |
| 703 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 704 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 705 | observer_->OnRenegotiationNeeded(); |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 710 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 711 | for (const auto& track : local_stream->GetAudioTracks()) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 712 | OnAudioTrackRemoved(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 713 | } |
| 714 | for (const auto& track : local_stream->GetVideoTracks()) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 715 | OnVideoTrackRemoved(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 719 | stream_observers_.erase( |
| 720 | std::remove_if( |
| 721 | stream_observers_.begin(), stream_observers_.end(), |
| 722 | [local_stream](const rtc::scoped_ptr<MediaStreamObserver>& observer) { |
| 723 | return observer->stream()->label().compare(local_stream->label()) == |
| 724 | 0; |
| 725 | }), |
| 726 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 727 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 728 | if (IsClosed()) { |
| 729 | return; |
| 730 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 731 | observer_->OnRenegotiationNeeded(); |
| 732 | } |
| 733 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 734 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 735 | MediaStreamTrackInterface* track, |
| 736 | std::vector<MediaStreamInterface*> streams) { |
| 737 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
| 738 | if (IsClosed()) { |
| 739 | return nullptr; |
| 740 | } |
| 741 | if (streams.size() >= 2) { |
| 742 | LOG(LS_ERROR) |
| 743 | << "Adding a track with two streams is not currently supported."; |
| 744 | return nullptr; |
| 745 | } |
| 746 | // TODO(deadbeef): Support adding a track to two different senders. |
| 747 | if (FindSenderForTrack(track) != senders_.end()) { |
| 748 | LOG(LS_ERROR) << "Sender for track " << track->id() << " already exists."; |
| 749 | return nullptr; |
| 750 | } |
| 751 | |
| 752 | // TODO(deadbeef): Support adding a track to multiple streams. |
| 753 | rtc::scoped_refptr<RtpSenderInterface> new_sender; |
| 754 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 755 | new_sender = RtpSenderProxy::Create( |
| 756 | signaling_thread(), |
| 757 | new AudioRtpSender(static_cast<AudioTrackInterface*>(track), |
| 758 | session_.get(), stats_.get())); |
| 759 | if (!streams.empty()) { |
| 760 | new_sender->set_stream_id(streams[0]->label()); |
| 761 | } |
| 762 | const TrackInfo* track_info = FindTrackInfo( |
| 763 | local_audio_tracks_, new_sender->stream_id(), track->id()); |
| 764 | if (track_info) { |
| 765 | new_sender->SetSsrc(track_info->ssrc); |
| 766 | } |
| 767 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 768 | new_sender = RtpSenderProxy::Create( |
| 769 | signaling_thread(), |
| 770 | new VideoRtpSender(static_cast<VideoTrackInterface*>(track), |
| 771 | session_.get())); |
| 772 | if (!streams.empty()) { |
| 773 | new_sender->set_stream_id(streams[0]->label()); |
| 774 | } |
| 775 | const TrackInfo* track_info = FindTrackInfo( |
| 776 | local_video_tracks_, new_sender->stream_id(), track->id()); |
| 777 | if (track_info) { |
| 778 | new_sender->SetSsrc(track_info->ssrc); |
| 779 | } |
| 780 | } else { |
| 781 | LOG(LS_ERROR) << "CreateSender called with invalid kind: " << track->kind(); |
| 782 | return rtc::scoped_refptr<RtpSenderInterface>(); |
| 783 | } |
| 784 | |
| 785 | senders_.push_back(new_sender); |
| 786 | observer_->OnRenegotiationNeeded(); |
| 787 | return new_sender; |
| 788 | } |
| 789 | |
| 790 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 791 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
| 792 | if (IsClosed()) { |
| 793 | return false; |
| 794 | } |
| 795 | |
| 796 | auto it = std::find(senders_.begin(), senders_.end(), sender); |
| 797 | if (it == senders_.end()) { |
| 798 | LOG(LS_ERROR) << "Couldn't find sender " << sender->id() << " to remove."; |
| 799 | return false; |
| 800 | } |
| 801 | (*it)->Stop(); |
| 802 | senders_.erase(it); |
| 803 | |
| 804 | observer_->OnRenegotiationNeeded(); |
| 805 | return true; |
| 806 | } |
| 807 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 808 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 809 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 810 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 811 | if (!track) { |
| 812 | LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
| 813 | return NULL; |
| 814 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 815 | if (!local_streams_->FindAudioTrack(track->id())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 816 | LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track."; |
| 817 | return NULL; |
| 818 | } |
| 819 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 820 | rtc::scoped_refptr<DtmfSenderInterface> sender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | DtmfSender::Create(track, signaling_thread(), session_.get())); |
| 822 | if (!sender.get()) { |
| 823 | LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create."; |
| 824 | return NULL; |
| 825 | } |
| 826 | return DtmfSenderProxy::Create(signaling_thread(), sender.get()); |
| 827 | } |
| 828 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 829 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 830 | const std::string& kind, |
| 831 | const std::string& stream_id) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 832 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 833 | rtc::scoped_refptr<RtpSenderInterface> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 834 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 835 | new_sender = RtpSenderProxy::Create( |
| 836 | signaling_thread(), new AudioRtpSender(session_.get(), stats_.get())); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 837 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 838 | new_sender = RtpSenderProxy::Create(signaling_thread(), |
| 839 | new VideoRtpSender(session_.get())); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 840 | } else { |
| 841 | LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 842 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 843 | } |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 844 | if (!stream_id.empty()) { |
| 845 | new_sender->set_stream_id(stream_id); |
| 846 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 847 | senders_.push_back(new_sender); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 848 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 849 | } |
| 850 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 851 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 852 | const { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 853 | return senders_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 857 | PeerConnection::GetReceivers() const { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 858 | return receivers_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 859 | } |
| 860 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 861 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 862 | MediaStreamTrackInterface* track, |
| 863 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 864 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 865 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 866 | if (!VERIFY(observer != NULL)) { |
| 867 | LOG(LS_ERROR) << "GetStats - observer is NULL."; |
| 868 | return false; |
| 869 | } |
| 870 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 871 | stats_->UpdateStats(level); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 872 | signaling_thread()->Post(this, MSG_GETSTATS, |
| 873 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 874 | return true; |
| 875 | } |
| 876 | |
| 877 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 878 | return signaling_state_; |
| 879 | } |
| 880 | |
| 881 | PeerConnectionInterface::IceState PeerConnection::ice_state() { |
| 882 | return ice_state_; |
| 883 | } |
| 884 | |
| 885 | PeerConnectionInterface::IceConnectionState |
| 886 | PeerConnection::ice_connection_state() { |
| 887 | return ice_connection_state_; |
| 888 | } |
| 889 | |
| 890 | PeerConnectionInterface::IceGatheringState |
| 891 | PeerConnection::ice_gathering_state() { |
| 892 | return ice_gathering_state_; |
| 893 | } |
| 894 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 895 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 896 | PeerConnection::CreateDataChannel( |
| 897 | const std::string& label, |
| 898 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 899 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 900 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 901 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 902 | rtc::scoped_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 903 | if (config) { |
| 904 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 905 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 906 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 907 | InternalCreateDataChannel(label, internal_config.get())); |
| 908 | if (!channel.get()) { |
| 909 | return nullptr; |
| 910 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 911 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 912 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 913 | // the first SCTP DataChannel. |
| 914 | if (session_->data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
| 915 | observer_->OnRenegotiationNeeded(); |
| 916 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 917 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 918 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 919 | } |
| 920 | |
| 921 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 922 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 923 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 924 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 925 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 926 | return; |
| 927 | } |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 928 | RTCOfferAnswerOptions options; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 929 | |
| 930 | bool value; |
| 931 | size_t mandatory_constraints = 0; |
| 932 | |
| 933 | if (FindConstraint(constraints, |
| 934 | MediaConstraintsInterface::kOfferToReceiveAudio, |
| 935 | &value, |
| 936 | &mandatory_constraints)) { |
| 937 | options.offer_to_receive_audio = |
| 938 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 939 | } |
| 940 | |
| 941 | if (FindConstraint(constraints, |
| 942 | MediaConstraintsInterface::kOfferToReceiveVideo, |
| 943 | &value, |
| 944 | &mandatory_constraints)) { |
| 945 | options.offer_to_receive_video = |
| 946 | value ? RTCOfferAnswerOptions::kOfferToReceiveMediaTrue : 0; |
| 947 | } |
| 948 | |
| 949 | if (FindConstraint(constraints, |
| 950 | MediaConstraintsInterface::kVoiceActivityDetection, |
| 951 | &value, |
| 952 | &mandatory_constraints)) { |
| 953 | options.voice_activity_detection = value; |
| 954 | } |
| 955 | |
| 956 | if (FindConstraint(constraints, |
| 957 | MediaConstraintsInterface::kIceRestart, |
| 958 | &value, |
| 959 | &mandatory_constraints)) { |
| 960 | options.ice_restart = value; |
| 961 | } |
| 962 | |
| 963 | if (FindConstraint(constraints, |
| 964 | MediaConstraintsInterface::kUseRtpMux, |
| 965 | &value, |
| 966 | &mandatory_constraints)) { |
| 967 | options.use_rtp_mux = value; |
| 968 | } |
| 969 | |
| 970 | CreateOffer(observer, options); |
| 971 | } |
| 972 | |
| 973 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 974 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 975 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 976 | if (!VERIFY(observer != nullptr)) { |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 977 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 978 | return; |
| 979 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 980 | |
| 981 | cricket::MediaSessionOptions session_options; |
| 982 | if (!GetOptionsForOffer(options, &session_options)) { |
| 983 | std::string error = "CreateOffer called with invalid options."; |
| 984 | LOG(LS_ERROR) << error; |
| 985 | PostCreateSessionDescriptionFailure(observer, error); |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | session_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | void PeerConnection::CreateAnswer( |
| 993 | CreateSessionDescriptionObserver* observer, |
| 994 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 995 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 996 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 997 | LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 998 | return; |
| 999 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1000 | |
| 1001 | cricket::MediaSessionOptions session_options; |
| 1002 | if (!GetOptionsForAnswer(constraints, &session_options)) { |
| 1003 | std::string error = "CreateAnswer called with invalid constraints."; |
| 1004 | LOG(LS_ERROR) << error; |
| 1005 | PostCreateSessionDescriptionFailure(observer, error); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | session_->CreateAnswer(observer, constraints, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | void PeerConnection::SetLocalDescription( |
| 1013 | SetSessionDescriptionObserver* observer, |
| 1014 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1015 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1016 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1017 | LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
| 1018 | return; |
| 1019 | } |
| 1020 | if (!desc) { |
| 1021 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1022 | return; |
| 1023 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1024 | // Update stats here so that we have the most recent stats for tracks and |
| 1025 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1026 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1027 | std::string error; |
| 1028 | if (!session_->SetLocalDescription(desc, &error)) { |
| 1029 | PostSetSessionDescriptionFailure(observer, error); |
| 1030 | return; |
| 1031 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1032 | |
| 1033 | // If setting the description decided our SSL role, allocate any necessary |
| 1034 | // SCTP sids. |
| 1035 | rtc::SSLRole role; |
| 1036 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 1037 | session_->GetSslRole(session_->data_channel(), &role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1038 | AllocateSctpSids(role); |
| 1039 | } |
| 1040 | |
| 1041 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 1042 | // local session description. |
| 1043 | const cricket::ContentInfo* audio_content = |
| 1044 | GetFirstAudioContent(desc->description()); |
| 1045 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1046 | if (audio_content->rejected) { |
| 1047 | RemoveTracks(cricket::MEDIA_TYPE_AUDIO); |
| 1048 | } else { |
| 1049 | const cricket::AudioContentDescription* audio_desc = |
| 1050 | static_cast<const cricket::AudioContentDescription*>( |
| 1051 | audio_content->description); |
| 1052 | UpdateLocalTracks(audio_desc->streams(), audio_desc->type()); |
| 1053 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | const cricket::ContentInfo* video_content = |
| 1057 | GetFirstVideoContent(desc->description()); |
| 1058 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1059 | if (video_content->rejected) { |
| 1060 | RemoveTracks(cricket::MEDIA_TYPE_VIDEO); |
| 1061 | } else { |
| 1062 | const cricket::VideoContentDescription* video_desc = |
| 1063 | static_cast<const cricket::VideoContentDescription*>( |
| 1064 | video_content->description); |
| 1065 | UpdateLocalTracks(video_desc->streams(), video_desc->type()); |
| 1066 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | const cricket::ContentInfo* data_content = |
| 1070 | GetFirstDataContent(desc->description()); |
| 1071 | if (data_content) { |
| 1072 | const cricket::DataContentDescription* data_desc = |
| 1073 | static_cast<const cricket::DataContentDescription*>( |
| 1074 | data_content->description); |
| 1075 | if (rtc::starts_with(data_desc->protocol().data(), |
| 1076 | cricket::kMediaProtocolRtpPrefix)) { |
| 1077 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1082 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1083 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1084 | // MaybeStartGathering needs to be called after posting |
| 1085 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1086 | // before signaling that SetLocalDescription completed. |
| 1087 | session_->MaybeStartGathering(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | void PeerConnection::SetRemoteDescription( |
| 1091 | SetSessionDescriptionObserver* observer, |
| 1092 | SessionDescriptionInterface* desc) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1093 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1094 | if (!VERIFY(observer != nullptr)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1095 | LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
| 1096 | return; |
| 1097 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1098 | if (!desc) { |
| 1099 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1100 | return; |
| 1101 | } |
| 1102 | // Update stats here so that we have the most recent stats for tracks and |
| 1103 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1104 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1105 | std::string error; |
| 1106 | if (!session_->SetRemoteDescription(desc, &error)) { |
| 1107 | PostSetSessionDescriptionFailure(observer, error); |
| 1108 | return; |
| 1109 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1110 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1111 | // If setting the description decided our SSL role, allocate any necessary |
| 1112 | // SCTP sids. |
| 1113 | rtc::SSLRole role; |
| 1114 | if (session_->data_channel_type() == cricket::DCT_SCTP && |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 1115 | session_->GetSslRole(session_->data_channel(), &role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1116 | AllocateSctpSids(role); |
| 1117 | } |
| 1118 | |
| 1119 | const cricket::SessionDescription* remote_desc = desc->description(); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1120 | const cricket::ContentInfo* audio_content = GetFirstAudioContent(remote_desc); |
| 1121 | const cricket::ContentInfo* video_content = GetFirstVideoContent(remote_desc); |
| 1122 | const cricket::AudioContentDescription* audio_desc = |
| 1123 | GetFirstAudioContentDescription(remote_desc); |
| 1124 | const cricket::VideoContentDescription* video_desc = |
| 1125 | GetFirstVideoContentDescription(remote_desc); |
| 1126 | const cricket::DataContentDescription* data_desc = |
| 1127 | GetFirstDataContentDescription(remote_desc); |
| 1128 | |
| 1129 | // Check if the descriptions include streams, just in case the peer supports |
| 1130 | // MSID, but doesn't indicate so with "a=msid-semantic". |
| 1131 | if (remote_desc->msid_supported() || |
| 1132 | (audio_desc && !audio_desc->streams().empty()) || |
| 1133 | (video_desc && !video_desc->streams().empty())) { |
| 1134 | remote_peer_supports_msid_ = true; |
| 1135 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1136 | |
| 1137 | // We wait to signal new streams until we finish processing the description, |
| 1138 | // since only at that point will new streams have all their tracks. |
| 1139 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 1140 | |
| 1141 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 1142 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1143 | if (audio_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1144 | if (audio_content->rejected) { |
| 1145 | RemoveTracks(cricket::MEDIA_TYPE_AUDIO); |
| 1146 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1147 | bool default_audio_track_needed = |
| 1148 | !remote_peer_supports_msid_ && |
| 1149 | MediaContentDirectionHasSend(audio_desc->direction()); |
| 1150 | UpdateRemoteStreamsList(GetActiveStreams(audio_desc), |
| 1151 | default_audio_track_needed, audio_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1152 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1153 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 1157 | // and MediaStreams. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1158 | if (video_content) { |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1159 | if (video_content->rejected) { |
| 1160 | RemoveTracks(cricket::MEDIA_TYPE_VIDEO); |
| 1161 | } else { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1162 | bool default_video_track_needed = |
| 1163 | !remote_peer_supports_msid_ && |
| 1164 | MediaContentDirectionHasSend(video_desc->direction()); |
| 1165 | UpdateRemoteStreamsList(GetActiveStreams(video_desc), |
| 1166 | default_video_track_needed, video_desc->type(), |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1167 | new_streams); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1168 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | // Update the DataChannels with the information from the remote peer. |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1172 | if (data_desc) { |
| 1173 | if (rtc::starts_with(data_desc->protocol().data(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1174 | cricket::kMediaProtocolRtpPrefix)) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1175 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 1180 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 1181 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 1182 | stats_->AddStream(new_stream); |
| 1183 | observer_->OnAddStream(new_stream); |
| 1184 | } |
| 1185 | |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1186 | UpdateEndedRemoteMediaStreams(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1187 | |
| 1188 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 1189 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame] | 1192 | bool PeerConnection::SetConfiguration(const RTCConfiguration& config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1193 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1194 | if (port_allocator_) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 1195 | cricket::ServerAddresses stun_servers; |
| 1196 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1197 | if (!ParseIceServers(config.servers, &stun_servers, &turn_servers)) { |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1198 | return false; |
| 1199 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 1200 | port_allocator_->SetIceServers(stun_servers, turn_servers); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1201 | } |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 1202 | session_->SetIceConfig(session_->ParseIceConfig(config)); |
mallinath@webrtc.org | 3d81b1b | 2014-09-09 14:38:10 +0000 | [diff] [blame] | 1203 | return session_->SetIceTransports(config.type); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1206 | bool PeerConnection::AddIceCandidate( |
| 1207 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1208 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1209 | return session_->ProcessIceMessage(ice_candidate); |
| 1210 | } |
| 1211 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1212 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1213 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1214 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1215 | |
| 1216 | if (session_) { |
| 1217 | session_->set_metrics_observer(uma_observer_); |
| 1218 | } |
| 1219 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1220 | // Send information about IPv4/IPv6 status. |
| 1221 | if (uma_observer_ && port_allocator_) { |
| 1222 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1223 | uma_observer_->IncrementEnumCounter( |
| 1224 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 1225 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 1226 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 1227 | uma_observer_->IncrementEnumCounter( |
| 1228 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 1229 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 1230 | } |
| 1231 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1234 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
| 1235 | return session_->local_description(); |
| 1236 | } |
| 1237 | |
| 1238 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
| 1239 | return session_->remote_description(); |
| 1240 | } |
| 1241 | |
| 1242 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1243 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1244 | // Update stats here so that we have the most recent stats for tracks and |
| 1245 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1246 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1247 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1248 | session_->Close(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1251 | void PeerConnection::OnSessionStateChange(WebRtcSession* /*session*/, |
| 1252 | WebRtcSession::State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1253 | switch (state) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1254 | case WebRtcSession::STATE_INIT: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1255 | ChangeSignalingState(PeerConnectionInterface::kStable); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1256 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1257 | case WebRtcSession::STATE_SENTOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1258 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer); |
| 1259 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1260 | case WebRtcSession::STATE_SENTPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1261 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer); |
| 1262 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1263 | case WebRtcSession::STATE_RECEIVEDOFFER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1264 | ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer); |
| 1265 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1266 | case WebRtcSession::STATE_RECEIVEDPRANSWER: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1267 | ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer); |
| 1268 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1269 | case WebRtcSession::STATE_INPROGRESS: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1270 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 1271 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1272 | case WebRtcSession::STATE_CLOSED: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1273 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 1274 | break; |
| 1275 | default: |
| 1276 | break; |
| 1277 | } |
| 1278 | } |
| 1279 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1280 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1281 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1282 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 1283 | SetSessionDescriptionMsg* param = |
| 1284 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1285 | param->observer->OnSuccess(); |
| 1286 | delete param; |
| 1287 | break; |
| 1288 | } |
| 1289 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 1290 | SetSessionDescriptionMsg* param = |
| 1291 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 1292 | param->observer->OnFailure(param->error); |
| 1293 | delete param; |
| 1294 | break; |
| 1295 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1296 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 1297 | CreateSessionDescriptionMsg* param = |
| 1298 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 1299 | param->observer->OnFailure(param->error); |
| 1300 | delete param; |
| 1301 | break; |
| 1302 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1303 | case MSG_GETSTATS: { |
| 1304 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1305 | StatsReports reports; |
| 1306 | stats_->GetStats(param->track, &reports); |
| 1307 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1308 | delete param; |
| 1309 | break; |
| 1310 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1311 | case MSG_FREE_DATACHANNELS: { |
| 1312 | sctp_data_channels_to_free_.clear(); |
| 1313 | break; |
| 1314 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1315 | default: |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1316 | RTC_DCHECK(false && "Not implemented"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1317 | break; |
| 1318 | } |
| 1319 | } |
| 1320 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1321 | void PeerConnection::CreateAudioReceiver(MediaStreamInterface* stream, |
| 1322 | AudioTrackInterface* audio_track, |
| 1323 | uint32_t ssrc) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1324 | receivers_.push_back(RtpReceiverProxy::Create( |
| 1325 | signaling_thread(), |
| 1326 | new AudioRtpReceiver(audio_track, ssrc, session_.get()))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1329 | void PeerConnection::CreateVideoReceiver(MediaStreamInterface* stream, |
| 1330 | VideoTrackInterface* video_track, |
| 1331 | uint32_t ssrc) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1332 | receivers_.push_back(RtpReceiverProxy::Create( |
| 1333 | signaling_thread(), |
| 1334 | new VideoRtpReceiver(video_track, ssrc, session_.get()))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1337 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 1338 | // description. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1339 | void PeerConnection::DestroyAudioReceiver(MediaStreamInterface* stream, |
| 1340 | AudioTrackInterface* audio_track) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1341 | auto it = FindReceiverForTrack(audio_track); |
| 1342 | if (it == receivers_.end()) { |
| 1343 | LOG(LS_WARNING) << "RtpReceiver for track with id " << audio_track->id() |
| 1344 | << " doesn't exist."; |
| 1345 | } else { |
| 1346 | (*it)->Stop(); |
| 1347 | receivers_.erase(it); |
| 1348 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1351 | void PeerConnection::DestroyVideoReceiver(MediaStreamInterface* stream, |
| 1352 | VideoTrackInterface* video_track) { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1353 | auto it = FindReceiverForTrack(video_track); |
| 1354 | if (it == receivers_.end()) { |
| 1355 | LOG(LS_WARNING) << "RtpReceiver for track with id " << video_track->id() |
| 1356 | << " doesn't exist."; |
| 1357 | } else { |
| 1358 | (*it)->Stop(); |
| 1359 | receivers_.erase(it); |
| 1360 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1361 | } |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1362 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1363 | void PeerConnection::OnIceConnectionChange( |
| 1364 | PeerConnectionInterface::IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1365 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1366 | // After transitioning to "closed", ignore any additional states from |
| 1367 | // WebRtcSession (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1368 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1369 | return; |
| 1370 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1371 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1372 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | void PeerConnection::OnIceGatheringChange( |
| 1376 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1377 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1378 | if (IsClosed()) { |
| 1379 | return; |
| 1380 | } |
| 1381 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1382 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1386 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 1387 | observer_->OnIceCandidate(candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1390 | void PeerConnection::OnIceConnectionReceivingChange(bool receiving) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1391 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1392 | observer_->OnIceConnectionReceivingChange(receiving); |
| 1393 | } |
| 1394 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1395 | void PeerConnection::ChangeSignalingState( |
| 1396 | PeerConnectionInterface::SignalingState signaling_state) { |
| 1397 | signaling_state_ = signaling_state; |
| 1398 | if (signaling_state == kClosed) { |
| 1399 | ice_connection_state_ = kIceConnectionClosed; |
| 1400 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 1401 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 1402 | ice_gathering_state_ = kIceGatheringComplete; |
| 1403 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 1404 | } |
| 1405 | } |
| 1406 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1409 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 1410 | MediaStreamInterface* stream) { |
| 1411 | auto sender = FindSenderForTrack(track); |
| 1412 | if (sender != senders_.end()) { |
| 1413 | // We already have a sender for this track, so just change the stream_id |
| 1414 | // so that it's correct in the next call to CreateOffer. |
| 1415 | (*sender)->set_stream_id(stream->label()); |
| 1416 | return; |
| 1417 | } |
| 1418 | |
| 1419 | // Normal case; we've never seen this track before. |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1420 | rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create( |
| 1421 | signaling_thread(), |
| 1422 | new AudioRtpSender(track, stream->label(), session_.get(), stats_.get())); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1423 | senders_.push_back(new_sender); |
| 1424 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 1425 | // which will connect the sender to the underlying transport. This can |
| 1426 | // occur if a local session description that contains the ID of the sender |
| 1427 | // is set before AddStream is called. It can also occur if the local |
| 1428 | // session description is not changed and RemoveStream is called, and |
| 1429 | // later AddStream is called again with the same stream. |
| 1430 | const TrackInfo* track_info = |
| 1431 | FindTrackInfo(local_audio_tracks_, stream->label(), track->id()); |
| 1432 | if (track_info) { |
| 1433 | new_sender->SetSsrc(track_info->ssrc); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 1438 | // indefinitely, when we have unified plan SDP. |
| 1439 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 1440 | MediaStreamInterface* stream) { |
| 1441 | auto sender = FindSenderForTrack(track); |
| 1442 | if (sender == senders_.end()) { |
| 1443 | LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 1444 | << " doesn't exist."; |
| 1445 | return; |
| 1446 | } |
| 1447 | (*sender)->Stop(); |
| 1448 | senders_.erase(sender); |
| 1449 | } |
| 1450 | |
| 1451 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 1452 | MediaStreamInterface* stream) { |
| 1453 | auto sender = FindSenderForTrack(track); |
| 1454 | if (sender != senders_.end()) { |
| 1455 | // We already have a sender for this track, so just change the stream_id |
| 1456 | // so that it's correct in the next call to CreateOffer. |
| 1457 | (*sender)->set_stream_id(stream->label()); |
| 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | // Normal case; we've never seen this track before. |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1462 | rtc::scoped_refptr<RtpSenderInterface> new_sender = RtpSenderProxy::Create( |
| 1463 | signaling_thread(), |
| 1464 | new VideoRtpSender(track, stream->label(), session_.get())); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1465 | senders_.push_back(new_sender); |
| 1466 | const TrackInfo* track_info = |
| 1467 | FindTrackInfo(local_video_tracks_, stream->label(), track->id()); |
| 1468 | if (track_info) { |
| 1469 | new_sender->SetSsrc(track_info->ssrc); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 1474 | MediaStreamInterface* stream) { |
| 1475 | auto sender = FindSenderForTrack(track); |
| 1476 | if (sender == senders_.end()) { |
| 1477 | LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 1478 | << " doesn't exist."; |
| 1479 | return; |
| 1480 | } |
| 1481 | (*sender)->Stop(); |
| 1482 | senders_.erase(sender); |
| 1483 | } |
| 1484 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1485 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 1486 | SetSessionDescriptionObserver* observer, |
| 1487 | const std::string& error) { |
| 1488 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 1489 | msg->error = error; |
| 1490 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
| 1491 | } |
| 1492 | |
| 1493 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 1494 | CreateSessionDescriptionObserver* observer, |
| 1495 | const std::string& error) { |
| 1496 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 1497 | msg->error = error; |
| 1498 | signaling_thread()->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
| 1499 | } |
| 1500 | |
| 1501 | bool PeerConnection::GetOptionsForOffer( |
| 1502 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 1503 | cricket::MediaSessionOptions* session_options) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 1504 | // TODO(deadbeef): Once we have transceivers, enumerate them here instead of |
| 1505 | // ContentInfos. |
| 1506 | if (session_->local_description()) { |
| 1507 | for (const cricket::ContentInfo& content : |
| 1508 | session_->local_description()->description()->contents()) { |
| 1509 | session_options->transport_options[content.name] = |
| 1510 | cricket::TransportOptions(); |
| 1511 | } |
| 1512 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1513 | if (!ConvertRtcOptionsForOffer(rtc_options, session_options)) { |
| 1514 | return false; |
| 1515 | } |
| 1516 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1517 | AddSendStreams(session_options, senders_, rtp_data_channels_); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1518 | // Offer to receive audio/video if the constraint is not set and there are |
| 1519 | // send streams, or we're currently receiving. |
| 1520 | if (rtc_options.offer_to_receive_audio == RTCOfferAnswerOptions::kUndefined) { |
| 1521 | session_options->recv_audio = |
| 1522 | session_options->HasSendMediaStream(cricket::MEDIA_TYPE_AUDIO) || |
| 1523 | !remote_audio_tracks_.empty(); |
| 1524 | } |
| 1525 | if (rtc_options.offer_to_receive_video == RTCOfferAnswerOptions::kUndefined) { |
| 1526 | session_options->recv_video = |
| 1527 | session_options->HasSendMediaStream(cricket::MEDIA_TYPE_VIDEO) || |
| 1528 | !remote_video_tracks_.empty(); |
| 1529 | } |
| 1530 | session_options->bundle_enabled = |
| 1531 | session_options->bundle_enabled && |
| 1532 | (session_options->has_audio() || session_options->has_video() || |
| 1533 | session_options->has_data()); |
| 1534 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1535 | if (session_->data_channel_type() == cricket::DCT_SCTP && HasDataChannels()) { |
| 1536 | session_options->data_channel_type = cricket::DCT_SCTP; |
| 1537 | } |
| 1538 | return true; |
| 1539 | } |
| 1540 | |
| 1541 | bool PeerConnection::GetOptionsForAnswer( |
| 1542 | const MediaConstraintsInterface* constraints, |
| 1543 | cricket::MediaSessionOptions* session_options) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1544 | session_options->recv_audio = false; |
| 1545 | session_options->recv_video = false; |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 1546 | // TODO(deadbeef): Once we have transceivers, enumerate them here instead of |
| 1547 | // ContentInfos. |
| 1548 | if (session_->remote_description()) { |
| 1549 | // Initialize the transport_options map. |
| 1550 | for (const cricket::ContentInfo& content : |
| 1551 | session_->remote_description()->description()->contents()) { |
| 1552 | session_options->transport_options[content.name] = |
| 1553 | cricket::TransportOptions(); |
| 1554 | } |
| 1555 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1556 | if (!ParseConstraintsForAnswer(constraints, session_options)) { |
| 1557 | return false; |
| 1558 | } |
| 1559 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1560 | AddSendStreams(session_options, senders_, rtp_data_channels_); |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1561 | session_options->bundle_enabled = |
| 1562 | session_options->bundle_enabled && |
| 1563 | (session_options->has_audio() || session_options->has_video() || |
| 1564 | session_options->has_data()); |
| 1565 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1566 | // RTP data channel is handled in MediaSessionOptions::AddStream. SCTP streams |
| 1567 | // are not signaled in the SDP so does not go through that path and must be |
| 1568 | // handled here. |
| 1569 | if (session_->data_channel_type() == cricket::DCT_SCTP) { |
| 1570 | session_options->data_channel_type = cricket::DCT_SCTP; |
| 1571 | } |
| 1572 | return true; |
| 1573 | } |
| 1574 | |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1575 | void PeerConnection::RemoveTracks(cricket::MediaType media_type) { |
| 1576 | UpdateLocalTracks(std::vector<cricket::StreamParams>(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1577 | UpdateRemoteStreamsList(std::vector<cricket::StreamParams>(), false, |
| 1578 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1581 | void PeerConnection::UpdateRemoteStreamsList( |
| 1582 | const cricket::StreamParamsVec& streams, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1583 | bool default_track_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1584 | cricket::MediaType media_type, |
| 1585 | StreamCollection* new_streams) { |
| 1586 | TrackInfos* current_tracks = GetRemoteTracks(media_type); |
| 1587 | |
| 1588 | // 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] | 1589 | // the new StreamParam. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1590 | auto track_it = current_tracks->begin(); |
| 1591 | while (track_it != current_tracks->end()) { |
| 1592 | const TrackInfo& info = *track_it; |
| 1593 | const cricket::StreamParams* params = |
| 1594 | cricket::GetStreamBySsrc(streams, info.ssrc); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1595 | bool track_exists = params && params->id == info.track_id; |
| 1596 | // If this is a default track, and we still need it, don't remove it. |
| 1597 | if ((info.stream_label == kDefaultStreamLabel && default_track_needed) || |
| 1598 | track_exists) { |
| 1599 | ++track_it; |
| 1600 | } else { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1601 | OnRemoteTrackRemoved(info.stream_label, info.track_id, media_type); |
| 1602 | track_it = current_tracks->erase(track_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | // Find new and active tracks. |
| 1607 | for (const cricket::StreamParams& params : streams) { |
| 1608 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 1609 | // track id. |
| 1610 | const std::string& stream_label = params.sync_label; |
| 1611 | const std::string& track_id = params.id; |
| 1612 | uint32_t ssrc = params.first_ssrc(); |
| 1613 | |
| 1614 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 1615 | remote_streams_->find(stream_label); |
| 1616 | if (!stream) { |
| 1617 | // This is a new MediaStream. Create a new remote MediaStream. |
| 1618 | stream = remote_stream_factory_->CreateMediaStream(stream_label); |
| 1619 | remote_streams_->AddStream(stream); |
| 1620 | new_streams->AddStream(stream); |
| 1621 | } |
| 1622 | |
| 1623 | const TrackInfo* track_info = |
| 1624 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 1625 | if (!track_info) { |
| 1626 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 1627 | OnRemoteTrackSeen(stream_label, track_id, ssrc, media_type); |
| 1628 | } |
| 1629 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 1630 | |
| 1631 | // Add default track if necessary. |
| 1632 | if (default_track_needed) { |
| 1633 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
| 1634 | remote_streams_->find(kDefaultStreamLabel); |
| 1635 | if (!default_stream) { |
| 1636 | // Create the new default MediaStream. |
| 1637 | default_stream = |
| 1638 | remote_stream_factory_->CreateMediaStream(kDefaultStreamLabel); |
| 1639 | remote_streams_->AddStream(default_stream); |
| 1640 | new_streams->AddStream(default_stream); |
| 1641 | } |
| 1642 | std::string default_track_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 1643 | ? kDefaultAudioTrackLabel |
| 1644 | : kDefaultVideoTrackLabel; |
| 1645 | const TrackInfo* default_track_info = |
| 1646 | FindTrackInfo(*current_tracks, kDefaultStreamLabel, default_track_id); |
| 1647 | if (!default_track_info) { |
| 1648 | current_tracks->push_back( |
| 1649 | TrackInfo(kDefaultStreamLabel, default_track_id, 0)); |
| 1650 | OnRemoteTrackSeen(kDefaultStreamLabel, default_track_id, 0, media_type); |
| 1651 | } |
| 1652 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | void PeerConnection::OnRemoteTrackSeen(const std::string& stream_label, |
| 1656 | const std::string& track_id, |
| 1657 | uint32_t ssrc, |
| 1658 | cricket::MediaType media_type) { |
| 1659 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 1660 | |
| 1661 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1662 | AudioTrackInterface* audio_track = remote_stream_factory_->AddAudioTrack( |
| 1663 | ssrc, session_.get(), stream, track_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1664 | CreateAudioReceiver(stream, audio_track, ssrc); |
| 1665 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1666 | VideoTrackInterface* video_track = |
| 1667 | remote_stream_factory_->AddVideoTrack(stream, track_id); |
| 1668 | CreateVideoReceiver(stream, video_track, ssrc); |
| 1669 | } else { |
| 1670 | RTC_DCHECK(false && "Invalid media type"); |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | void PeerConnection::OnRemoteTrackRemoved(const std::string& stream_label, |
| 1675 | const std::string& track_id, |
| 1676 | cricket::MediaType media_type) { |
| 1677 | MediaStreamInterface* stream = remote_streams_->find(stream_label); |
| 1678 | |
| 1679 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1680 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
| 1681 | stream->FindAudioTrack(track_id); |
| 1682 | if (audio_track) { |
| 1683 | audio_track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1684 | stream->RemoveTrack(audio_track); |
| 1685 | DestroyAudioReceiver(stream, audio_track); |
| 1686 | } |
| 1687 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1688 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
| 1689 | stream->FindVideoTrack(track_id); |
| 1690 | if (video_track) { |
| 1691 | video_track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1692 | stream->RemoveTrack(video_track); |
| 1693 | DestroyVideoReceiver(stream, video_track); |
| 1694 | } |
| 1695 | } else { |
| 1696 | ASSERT(false && "Invalid media type"); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 1701 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 1702 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 1703 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 1704 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 1705 | streams_to_remove.push_back(stream); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | for (const auto& stream : streams_to_remove) { |
| 1710 | remote_streams_->RemoveStream(stream); |
| 1711 | observer_->OnRemoveStream(stream); |
| 1712 | } |
| 1713 | } |
| 1714 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1715 | void PeerConnection::EndRemoteTracks(cricket::MediaType media_type) { |
| 1716 | TrackInfos* current_tracks = GetRemoteTracks(media_type); |
| 1717 | for (TrackInfos::iterator track_it = current_tracks->begin(); |
| 1718 | track_it != current_tracks->end(); ++track_it) { |
| 1719 | const TrackInfo& info = *track_it; |
| 1720 | MediaStreamInterface* stream = remote_streams_->find(info.stream_label); |
| 1721 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1722 | AudioTrackInterface* track = stream->FindAudioTrack(info.track_id); |
| 1723 | // There's no guarantee the track is still available, e.g. the track may |
| 1724 | // have been removed from the stream by javascript. |
| 1725 | if (track) { |
| 1726 | track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1727 | } |
| 1728 | } |
| 1729 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1730 | VideoTrackInterface* track = stream->FindVideoTrack(info.track_id); |
| 1731 | // There's no guarantee the track is still available, e.g. the track may |
| 1732 | // have been removed from the stream by javascript. |
| 1733 | if (track) { |
| 1734 | track->set_state(webrtc::MediaStreamTrackInterface::kEnded); |
| 1735 | } |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | void PeerConnection::UpdateLocalTracks( |
| 1741 | const std::vector<cricket::StreamParams>& streams, |
| 1742 | cricket::MediaType media_type) { |
| 1743 | TrackInfos* current_tracks = GetLocalTracks(media_type); |
| 1744 | |
| 1745 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 1746 | // don't match the new StreamParam. |
| 1747 | TrackInfos::iterator track_it = current_tracks->begin(); |
| 1748 | while (track_it != current_tracks->end()) { |
| 1749 | const TrackInfo& info = *track_it; |
| 1750 | const cricket::StreamParams* params = |
| 1751 | cricket::GetStreamBySsrc(streams, info.ssrc); |
| 1752 | if (!params || params->id != info.track_id || |
| 1753 | params->sync_label != info.stream_label) { |
| 1754 | OnLocalTrackRemoved(info.stream_label, info.track_id, info.ssrc, |
| 1755 | media_type); |
| 1756 | track_it = current_tracks->erase(track_it); |
| 1757 | } else { |
| 1758 | ++track_it; |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | // Find new and active tracks. |
| 1763 | for (const cricket::StreamParams& params : streams) { |
| 1764 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 1765 | // track id. |
| 1766 | const std::string& stream_label = params.sync_label; |
| 1767 | const std::string& track_id = params.id; |
| 1768 | uint32_t ssrc = params.first_ssrc(); |
| 1769 | const TrackInfo* track_info = |
| 1770 | FindTrackInfo(*current_tracks, stream_label, track_id); |
| 1771 | if (!track_info) { |
| 1772 | current_tracks->push_back(TrackInfo(stream_label, track_id, ssrc)); |
| 1773 | OnLocalTrackSeen(stream_label, track_id, params.first_ssrc(), media_type); |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | void PeerConnection::OnLocalTrackSeen(const std::string& stream_label, |
| 1779 | const std::string& track_id, |
| 1780 | uint32_t ssrc, |
| 1781 | cricket::MediaType media_type) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1782 | RtpSenderInterface* sender = FindSenderById(track_id); |
| 1783 | if (!sender) { |
| 1784 | LOG(LS_WARNING) << "An unknown RtpSender with id " << track_id |
| 1785 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1786 | return; |
| 1787 | } |
| 1788 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1789 | if (sender->media_type() != media_type) { |
| 1790 | LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 1791 | << " description with an unexpected media type."; |
| 1792 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1793 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1794 | |
| 1795 | sender->set_stream_id(stream_label); |
| 1796 | sender->SetSsrc(ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | void PeerConnection::OnLocalTrackRemoved(const std::string& stream_label, |
| 1800 | const std::string& track_id, |
| 1801 | uint32_t ssrc, |
| 1802 | cricket::MediaType media_type) { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1803 | RtpSenderInterface* sender = FindSenderById(track_id); |
| 1804 | if (!sender) { |
| 1805 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1806 | // SessionDescriptions has been renegotiated. |
| 1807 | return; |
| 1808 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1809 | |
| 1810 | // A sender has been removed from the SessionDescription but it's still |
| 1811 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 1812 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 1813 | if (sender->media_type() != media_type) { |
| 1814 | LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
| 1815 | << " description with an unexpected media type."; |
| 1816 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1817 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1818 | |
| 1819 | sender->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 1823 | const cricket::StreamParamsVec& streams) { |
| 1824 | std::vector<std::string> existing_channels; |
| 1825 | |
| 1826 | // Find new and active data channels. |
| 1827 | for (const cricket::StreamParams& params : streams) { |
| 1828 | // |it->sync_label| is actually the data channel label. The reason is that |
| 1829 | // we use the same naming of data channels as we do for |
| 1830 | // MediaStreams and Tracks. |
| 1831 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 1832 | // track label is the same as |streamid|. |
| 1833 | const std::string& channel_label = params.sync_label; |
| 1834 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
| 1835 | if (!VERIFY(data_channel_it != rtp_data_channels_.end())) { |
| 1836 | continue; |
| 1837 | } |
| 1838 | // Set the SSRC the data channel should use for sending. |
| 1839 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 1840 | existing_channels.push_back(data_channel_it->first); |
| 1841 | } |
| 1842 | |
| 1843 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 1844 | } |
| 1845 | |
| 1846 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 1847 | const cricket::StreamParamsVec& streams) { |
| 1848 | std::vector<std::string> existing_channels; |
| 1849 | |
| 1850 | // Find new and active data channels. |
| 1851 | for (const cricket::StreamParams& params : streams) { |
| 1852 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 1853 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 1854 | std::string label = params.sync_label.empty() |
| 1855 | ? rtc::ToString(params.first_ssrc()) |
| 1856 | : params.sync_label; |
| 1857 | auto data_channel_it = rtp_data_channels_.find(label); |
| 1858 | if (data_channel_it == rtp_data_channels_.end()) { |
| 1859 | // This is a new data channel. |
| 1860 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 1861 | } else { |
| 1862 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 1863 | } |
| 1864 | existing_channels.push_back(label); |
| 1865 | } |
| 1866 | |
| 1867 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 1868 | } |
| 1869 | |
| 1870 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 1871 | const std::vector<std::string>& active_channels, |
| 1872 | bool is_local_update) { |
| 1873 | auto it = rtp_data_channels_.begin(); |
| 1874 | while (it != rtp_data_channels_.end()) { |
| 1875 | DataChannel* data_channel = it->second; |
| 1876 | if (std::find(active_channels.begin(), active_channels.end(), |
| 1877 | data_channel->label()) != active_channels.end()) { |
| 1878 | ++it; |
| 1879 | continue; |
| 1880 | } |
| 1881 | |
| 1882 | if (is_local_update) { |
| 1883 | data_channel->SetSendSsrc(0); |
| 1884 | } else { |
| 1885 | data_channel->RemotePeerRequestClose(); |
| 1886 | } |
| 1887 | |
| 1888 | if (data_channel->state() == DataChannel::kClosed) { |
| 1889 | rtp_data_channels_.erase(it); |
| 1890 | it = rtp_data_channels_.begin(); |
| 1891 | } else { |
| 1892 | ++it; |
| 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 1898 | uint32_t remote_ssrc) { |
| 1899 | rtc::scoped_refptr<DataChannel> channel( |
| 1900 | InternalCreateDataChannel(label, nullptr)); |
| 1901 | if (!channel.get()) { |
| 1902 | LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
| 1903 | << "CreateDataChannel failed."; |
| 1904 | return; |
| 1905 | } |
| 1906 | channel->SetReceiveSsrc(remote_ssrc); |
| 1907 | observer_->OnDataChannel( |
| 1908 | DataChannelProxy::Create(signaling_thread(), channel)); |
| 1909 | } |
| 1910 | |
| 1911 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 1912 | const std::string& label, |
| 1913 | const InternalDataChannelInit* config) { |
| 1914 | if (IsClosed()) { |
| 1915 | return nullptr; |
| 1916 | } |
| 1917 | if (session_->data_channel_type() == cricket::DCT_NONE) { |
| 1918 | LOG(LS_ERROR) |
| 1919 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 1920 | return nullptr; |
| 1921 | } |
| 1922 | InternalDataChannelInit new_config = |
| 1923 | config ? (*config) : InternalDataChannelInit(); |
| 1924 | if (session_->data_channel_type() == cricket::DCT_SCTP) { |
| 1925 | if (new_config.id < 0) { |
| 1926 | rtc::SSLRole role; |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 1927 | if ((session_->GetSslRole(session_->data_channel(), &role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1928 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
| 1929 | LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel."; |
| 1930 | return nullptr; |
| 1931 | } |
| 1932 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
| 1933 | LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 1934 | << "because the id is already in use or out of range."; |
| 1935 | return nullptr; |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | rtc::scoped_refptr<DataChannel> channel(DataChannel::Create( |
| 1940 | session_.get(), session_->data_channel_type(), label, new_config)); |
| 1941 | if (!channel) { |
| 1942 | sid_allocator_.ReleaseSid(new_config.id); |
| 1943 | return nullptr; |
| 1944 | } |
| 1945 | |
| 1946 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 1947 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
| 1948 | LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 1949 | << " already exists."; |
| 1950 | return nullptr; |
| 1951 | } |
| 1952 | rtp_data_channels_[channel->label()] = channel; |
| 1953 | } else { |
| 1954 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 1955 | sctp_data_channels_.push_back(channel); |
| 1956 | channel->SignalClosed.connect(this, |
| 1957 | &PeerConnection::OnSctpDataChannelClosed); |
| 1958 | } |
| 1959 | |
| 1960 | return channel; |
| 1961 | } |
| 1962 | |
| 1963 | bool PeerConnection::HasDataChannels() const { |
| 1964 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 1965 | } |
| 1966 | |
| 1967 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 1968 | for (const auto& channel : sctp_data_channels_) { |
| 1969 | if (channel->id() < 0) { |
| 1970 | int sid; |
| 1971 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
| 1972 | LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
| 1973 | continue; |
| 1974 | } |
| 1975 | channel->SetSctpSid(sid); |
| 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1981 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1982 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 1983 | ++it) { |
| 1984 | if (it->get() == channel) { |
| 1985 | if (channel->id() >= 0) { |
| 1986 | sid_allocator_.ReleaseSid(channel->id()); |
| 1987 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1988 | // Since this method is triggered by a signal from the DataChannel, |
| 1989 | // we can't free it directly here; we need to free it asynchronously. |
| 1990 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1991 | sctp_data_channels_.erase(it); |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 1992 | signaling_thread()->Post(this, MSG_FREE_DATACHANNELS, nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1993 | return; |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | void PeerConnection::OnVoiceChannelDestroyed() { |
| 1999 | EndRemoteTracks(cricket::MEDIA_TYPE_AUDIO); |
| 2000 | } |
| 2001 | |
| 2002 | void PeerConnection::OnVideoChannelDestroyed() { |
| 2003 | EndRemoteTracks(cricket::MEDIA_TYPE_VIDEO); |
| 2004 | } |
| 2005 | |
| 2006 | void PeerConnection::OnDataChannelCreated() { |
| 2007 | for (const auto& channel : sctp_data_channels_) { |
| 2008 | channel->OnTransportChannelCreated(); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | void PeerConnection::OnDataChannelDestroyed() { |
| 2013 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 2014 | // DataChannel may callback to us and try to modify the list. |
| 2015 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 2016 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 2017 | for (const auto& kv : temp_rtp_dcs) { |
| 2018 | kv.second->OnTransportChannelDestroyed(); |
| 2019 | } |
| 2020 | |
| 2021 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 2022 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 2023 | for (const auto& channel : temp_sctp_dcs) { |
| 2024 | channel->OnTransportChannelDestroyed(); |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | void PeerConnection::OnDataChannelOpenMessage( |
| 2029 | const std::string& label, |
| 2030 | const InternalDataChannelInit& config) { |
| 2031 | rtc::scoped_refptr<DataChannel> channel( |
| 2032 | InternalCreateDataChannel(label, &config)); |
| 2033 | if (!channel.get()) { |
| 2034 | LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
| 2035 | return; |
| 2036 | } |
| 2037 | |
| 2038 | observer_->OnDataChannel( |
| 2039 | DataChannelProxy::Create(signaling_thread(), channel)); |
| 2040 | } |
| 2041 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 2042 | RtpSenderInterface* PeerConnection::FindSenderById(const std::string& id) { |
| 2043 | auto it = |
| 2044 | std::find_if(senders_.begin(), senders_.end(), |
| 2045 | [id](const rtc::scoped_refptr<RtpSenderInterface>& sender) { |
| 2046 | return sender->id() == id; |
| 2047 | }); |
| 2048 | return it != senders_.end() ? it->get() : nullptr; |
| 2049 | } |
| 2050 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 2051 | std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator |
| 2052 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) { |
| 2053 | return std::find_if( |
| 2054 | senders_.begin(), senders_.end(), |
| 2055 | [track](const rtc::scoped_refptr<RtpSenderInterface>& sender) { |
| 2056 | return sender->track() == track; |
| 2057 | }); |
| 2058 | } |
| 2059 | |
| 2060 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator |
| 2061 | PeerConnection::FindReceiverForTrack(MediaStreamTrackInterface* track) { |
| 2062 | return std::find_if( |
| 2063 | receivers_.begin(), receivers_.end(), |
| 2064 | [track](const rtc::scoped_refptr<RtpReceiverInterface>& receiver) { |
| 2065 | return receiver->track() == track; |
| 2066 | }); |
| 2067 | } |
| 2068 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2069 | PeerConnection::TrackInfos* PeerConnection::GetRemoteTracks( |
| 2070 | cricket::MediaType media_type) { |
| 2071 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2072 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 2073 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &remote_audio_tracks_ |
| 2074 | : &remote_video_tracks_; |
| 2075 | } |
| 2076 | |
| 2077 | PeerConnection::TrackInfos* PeerConnection::GetLocalTracks( |
| 2078 | cricket::MediaType media_type) { |
| 2079 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2080 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 2081 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_tracks_ |
| 2082 | : &local_video_tracks_; |
| 2083 | } |
| 2084 | |
| 2085 | const PeerConnection::TrackInfo* PeerConnection::FindTrackInfo( |
| 2086 | const PeerConnection::TrackInfos& infos, |
| 2087 | const std::string& stream_label, |
| 2088 | const std::string track_id) const { |
| 2089 | for (const TrackInfo& track_info : infos) { |
| 2090 | if (track_info.stream_label == stream_label && |
| 2091 | track_info.track_id == track_id) { |
| 2092 | return &track_info; |
| 2093 | } |
| 2094 | } |
| 2095 | return nullptr; |
| 2096 | } |
| 2097 | |
| 2098 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 2099 | for (const auto& channel : sctp_data_channels_) { |
| 2100 | if (channel->id() == sid) { |
| 2101 | return channel; |
| 2102 | } |
| 2103 | } |
| 2104 | return nullptr; |
| 2105 | } |
| 2106 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2107 | } // namespace webrtc |