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