henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012, Google Inc. |
| 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 | |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "talk/app/webrtc/dtmfsender.h" |
| 33 | #include "talk/app/webrtc/jsepicecandidate.h" |
| 34 | #include "talk/app/webrtc/jsepsessiondescription.h" |
| 35 | #include "talk/app/webrtc/mediastreamhandler.h" |
| 36 | #include "talk/app/webrtc/streamcollection.h" |
| 37 | #include "talk/base/logging.h" |
| 38 | #include "talk/base/stringencode.h" |
| 39 | #include "talk/session/media/channelmanager.h" |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | using webrtc::PeerConnectionInterface; |
| 44 | |
| 45 | // The min number of tokens in the ice uri. |
| 46 | static const size_t kMinIceUriTokens = 2; |
| 47 | // The min number of tokens must present in Turn host uri. |
| 48 | // e.g. user@turn.example.org |
| 49 | static const size_t kTurnHostTokensNum = 2; |
| 50 | // Number of tokens must be preset when TURN uri has transport param. |
| 51 | static const size_t kTurnTransportTokensNum = 2; |
| 52 | // The default stun port. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 53 | static const int kDefaultStunPort = 3478; |
| 54 | static const int kDefaultStunTlsPort = 5349; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | static const char kTransport[] = "transport"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 56 | static const char kUdpTransportType[] = "udp"; |
| 57 | static const char kTcpTransportType[] = "tcp"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | |
| 59 | // NOTE: Must be in the same order as the ServiceType enum. |
| 60 | static const char* kValidIceServiceTypes[] = { |
| 61 | "stun", "stuns", "turn", "turns", "invalid" }; |
| 62 | |
| 63 | enum ServiceType { |
| 64 | STUN, // Indicates a STUN server. |
| 65 | STUNS, // Indicates a STUN server used with a TLS session. |
| 66 | TURN, // Indicates a TURN server |
| 67 | TURNS, // Indicates a TURN server used with a TLS session. |
| 68 | INVALID, // Unknown. |
| 69 | }; |
| 70 | |
| 71 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 72 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 74 | MSG_GETSTATS, |
| 75 | MSG_ICECONNECTIONCHANGE, |
| 76 | MSG_ICEGATHERINGCHANGE, |
| 77 | MSG_ICECANDIDATE, |
| 78 | MSG_ICECOMPLETE, |
| 79 | }; |
| 80 | |
| 81 | struct CandidateMsg : public talk_base::MessageData { |
| 82 | explicit CandidateMsg(const webrtc::JsepIceCandidate* candidate) |
| 83 | : candidate(candidate) { |
| 84 | } |
| 85 | talk_base::scoped_ptr<const webrtc::JsepIceCandidate> candidate; |
| 86 | }; |
| 87 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | struct SetSessionDescriptionMsg : public talk_base::MessageData { |
| 89 | explicit SetSessionDescriptionMsg( |
| 90 | webrtc::SetSessionDescriptionObserver* observer) |
| 91 | : observer(observer) { |
| 92 | } |
| 93 | |
| 94 | talk_base::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 95 | std::string error; |
| 96 | }; |
| 97 | |
| 98 | struct GetStatsMsg : public talk_base::MessageData { |
| 99 | explicit GetStatsMsg(webrtc::StatsObserver* observer) |
| 100 | : observer(observer) { |
| 101 | } |
| 102 | webrtc::StatsReports reports; |
| 103 | talk_base::scoped_refptr<webrtc::StatsObserver> observer; |
| 104 | }; |
| 105 | |
| 106 | typedef webrtc::PortAllocatorFactoryInterface::StunConfiguration |
| 107 | StunConfiguration; |
| 108 | typedef webrtc::PortAllocatorFactoryInterface::TurnConfiguration |
| 109 | TurnConfiguration; |
| 110 | |
| 111 | bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration, |
| 112 | std::vector<StunConfiguration>* stun_config, |
| 113 | std::vector<TurnConfiguration>* turn_config) { |
| 114 | // draft-nandakumar-rtcweb-stun-uri-01 |
| 115 | // stunURI = scheme ":" stun-host [ ":" stun-port ] |
| 116 | // scheme = "stun" / "stuns" |
| 117 | // stun-host = IP-literal / IPv4address / reg-name |
| 118 | // stun-port = *DIGIT |
| 119 | |
| 120 | // draft-petithuguenin-behave-turn-uris-01 |
| 121 | // turnURI = scheme ":" turn-host [ ":" turn-port ] |
| 122 | // [ "?transport=" transport ] |
| 123 | // scheme = "turn" / "turns" |
| 124 | // transport = "udp" / "tcp" / transport-ext |
| 125 | // transport-ext = 1*unreserved |
| 126 | // turn-host = IP-literal / IPv4address / reg-name |
| 127 | // turn-port = *DIGIT |
| 128 | |
| 129 | // TODO(ronghuawu): Handle IPV6 address |
| 130 | for (size_t i = 0; i < configuration.size(); ++i) { |
| 131 | webrtc::PeerConnectionInterface::IceServer server = configuration[i]; |
| 132 | if (server.uri.empty()) { |
| 133 | LOG(WARNING) << "Empty uri."; |
| 134 | continue; |
| 135 | } |
| 136 | std::vector<std::string> tokens; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 137 | std::string turn_transport_type = kUdpTransportType; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | talk_base::tokenize(server.uri, '?', &tokens); |
| 139 | std::string uri_without_transport = tokens[0]; |
| 140 | // Let's look into transport= param, if it exists. |
| 141 | if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present. |
| 142 | std::string uri_transport_param = tokens[1]; |
| 143 | talk_base::tokenize(uri_transport_param, '=', &tokens); |
| 144 | if (tokens[0] == kTransport) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 145 | // As per above grammar transport param will be consist of lower case |
| 146 | // letters. |
| 147 | if (tokens[1] != kUdpTransportType && tokens[1] != kTcpTransportType) { |
| 148 | LOG(LS_WARNING) << "Transport param should always be udp or tcp."; |
| 149 | continue; |
| 150 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | turn_transport_type = tokens[1]; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | tokens.clear(); |
| 156 | talk_base::tokenize(uri_without_transport, ':', &tokens); |
| 157 | if (tokens.size() < kMinIceUriTokens) { |
| 158 | LOG(WARNING) << "Invalid uri: " << server.uri; |
| 159 | continue; |
| 160 | } |
| 161 | ServiceType service_type = INVALID; |
| 162 | const std::string& type = tokens[0]; |
| 163 | for (size_t i = 0; i < ARRAY_SIZE(kValidIceServiceTypes); ++i) { |
| 164 | if (type.compare(kValidIceServiceTypes[i]) == 0) { |
| 165 | service_type = static_cast<ServiceType>(i); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | if (service_type == INVALID) { |
| 170 | LOG(WARNING) << "Invalid service type: " << type; |
| 171 | continue; |
| 172 | } |
| 173 | std::string address = tokens[1]; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 174 | int port = kDefaultStunPort; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 175 | if (service_type == TURNS) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 176 | port = kDefaultStunTlsPort; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 177 | turn_transport_type = kTcpTransportType; |
| 178 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 179 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | if (tokens.size() > kMinIceUriTokens) { |
| 181 | if (!talk_base::FromString(tokens[2], &port)) { |
| 182 | LOG(LS_WARNING) << "Failed to parse port string: " << tokens[2]; |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | if (port <= 0 || port > 0xffff) { |
| 187 | LOG(WARNING) << "Invalid port: " << port; |
| 188 | continue; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | switch (service_type) { |
| 193 | case STUN: |
| 194 | case STUNS: |
| 195 | stun_config->push_back(StunConfiguration(address, port)); |
| 196 | break; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 197 | case TURN: |
| 198 | case TURNS: { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 199 | if (server.username.empty()) { |
| 200 | // Turn url example from the spec |url:"turn:user@turn.example.org"|. |
| 201 | std::vector<std::string> turn_tokens; |
| 202 | talk_base::tokenize(address, '@', &turn_tokens); |
| 203 | if (turn_tokens.size() == kTurnHostTokensNum) { |
| 204 | server.username = talk_base::s_url_decode(turn_tokens[0]); |
| 205 | address = turn_tokens[1]; |
| 206 | } |
| 207 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 208 | |
| 209 | bool secure = (service_type == TURNS); |
| 210 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | turn_config->push_back(TurnConfiguration(address, port, |
| 212 | server.username, |
| 213 | server.password, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 214 | turn_transport_type, |
| 215 | secure)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | // STUN functionality is part of TURN. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 217 | // Note: If there is only TURNS is supplied as part of configuration, |
| 218 | // we will have problem in fetching server reflexive candidate, as |
| 219 | // currently we don't have support of TCP/TLS in stunport.cc. |
| 220 | // In that case we should fetch server reflexive addess from |
| 221 | // TURN allocate response. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | stun_config->push_back(StunConfiguration(address, port)); |
| 223 | break; |
| 224 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 225 | case INVALID: |
| 226 | default: |
| 227 | LOG(WARNING) << "Configuration not supported: " << server.uri; |
| 228 | return false; |
| 229 | } |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | // Check if we can send |new_stream| on a PeerConnection. |
| 235 | // Currently only one audio but multiple video track is supported per |
| 236 | // PeerConnection. |
| 237 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 238 | webrtc::MediaStreamInterface* new_stream) { |
| 239 | if (!new_stream || !current_streams) |
| 240 | return false; |
| 241 | if (current_streams->find(new_stream->label()) != NULL) { |
| 242 | LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 243 | << " is already added."; |
| 244 | return false; |
| 245 | } |
| 246 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | } // namespace |
| 251 | |
| 252 | namespace webrtc { |
| 253 | |
| 254 | PeerConnection::PeerConnection(PeerConnectionFactory* factory) |
| 255 | : factory_(factory), |
| 256 | observer_(NULL), |
| 257 | signaling_state_(kStable), |
| 258 | ice_state_(kIceNew), |
| 259 | ice_connection_state_(kIceConnectionNew), |
| 260 | ice_gathering_state_(kIceGatheringNew) { |
| 261 | } |
| 262 | |
| 263 | PeerConnection::~PeerConnection() { |
| 264 | if (mediastream_signaling_) |
| 265 | mediastream_signaling_->TearDown(); |
| 266 | if (stream_handler_container_) |
| 267 | stream_handler_container_->TearDown(); |
| 268 | } |
| 269 | |
| 270 | bool PeerConnection::Initialize( |
| 271 | const PeerConnectionInterface::IceServers& configuration, |
| 272 | const MediaConstraintsInterface* constraints, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 273 | PortAllocatorFactoryInterface* allocator_factory, |
| 274 | DTLSIdentityServiceInterface* dtls_identity_service, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 275 | PeerConnectionObserver* observer) { |
| 276 | std::vector<PortAllocatorFactoryInterface::StunConfiguration> stun_config; |
| 277 | std::vector<PortAllocatorFactoryInterface::TurnConfiguration> turn_config; |
| 278 | if (!ParseIceServers(configuration, &stun_config, &turn_config)) { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | return DoInitialize(stun_config, turn_config, constraints, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 283 | allocator_factory, dtls_identity_service, observer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | bool PeerConnection::DoInitialize( |
| 287 | const StunConfigurations& stun_config, |
| 288 | const TurnConfigurations& turn_config, |
| 289 | const MediaConstraintsInterface* constraints, |
| 290 | webrtc::PortAllocatorFactoryInterface* allocator_factory, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 291 | DTLSIdentityServiceInterface* dtls_identity_service, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 292 | PeerConnectionObserver* observer) { |
| 293 | ASSERT(observer != NULL); |
| 294 | if (!observer) |
| 295 | return false; |
| 296 | observer_ = observer; |
| 297 | port_allocator_.reset( |
| 298 | allocator_factory->CreatePortAllocator(stun_config, turn_config)); |
| 299 | // To handle both internal and externally created port allocator, we will |
| 300 | // enable BUNDLE here. Also enabling TURN and disable legacy relay service. |
| 301 | port_allocator_->set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE | |
| 302 | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | |
| 303 | cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
| 304 | // No step delay is used while allocating ports. |
| 305 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 306 | |
| 307 | mediastream_signaling_.reset(new MediaStreamSignaling( |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 308 | factory_->signaling_thread(), this, factory_->channel_manager())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 309 | |
| 310 | session_.reset(new WebRtcSession(factory_->channel_manager(), |
| 311 | factory_->signaling_thread(), |
| 312 | factory_->worker_thread(), |
| 313 | port_allocator_.get(), |
| 314 | mediastream_signaling_.get())); |
| 315 | stream_handler_container_.reset(new MediaStreamHandlerContainer( |
| 316 | session_.get(), session_.get())); |
| 317 | stats_.set_session(session_.get()); |
| 318 | |
| 319 | // Initialize the WebRtcSession. It creates transport channels etc. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 320 | if (!session_->Initialize(factory_->options(), constraints, |
| 321 | dtls_identity_service)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 322 | return false; |
| 323 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 324 | // Register PeerConnection as receiver of local ice candidates. |
| 325 | // All the callbacks will be posted to the application from PeerConnection. |
| 326 | session_->RegisterIceObserver(this); |
| 327 | session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | talk_base::scoped_refptr<StreamCollectionInterface> |
| 332 | PeerConnection::local_streams() { |
| 333 | return mediastream_signaling_->local_streams(); |
| 334 | } |
| 335 | |
| 336 | talk_base::scoped_refptr<StreamCollectionInterface> |
| 337 | PeerConnection::remote_streams() { |
| 338 | return mediastream_signaling_->remote_streams(); |
| 339 | } |
| 340 | |
| 341 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream, |
| 342 | const MediaConstraintsInterface* constraints) { |
| 343 | if (IsClosed()) { |
| 344 | return false; |
| 345 | } |
| 346 | if (!CanAddLocalMediaStream(mediastream_signaling_->local_streams(), |
| 347 | local_stream)) |
| 348 | return false; |
| 349 | |
| 350 | // TODO(perkj): Implement support for MediaConstraints in AddStream. |
| 351 | if (!mediastream_signaling_->AddLocalStream(local_stream)) { |
| 352 | return false; |
| 353 | } |
| 354 | stats_.AddStream(local_stream); |
| 355 | observer_->OnRenegotiationNeeded(); |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
fischman@webrtc.org | 32001ef | 2013-08-12 23:26:21 +0000 | [diff] [blame] | 360 | mediastream_signaling_->RemoveLocalStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 361 | if (IsClosed()) { |
| 362 | return; |
| 363 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 364 | observer_->OnRenegotiationNeeded(); |
| 365 | } |
| 366 | |
| 367 | talk_base::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
| 368 | AudioTrackInterface* track) { |
| 369 | if (!track) { |
| 370 | LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
| 371 | return NULL; |
| 372 | } |
| 373 | if (!mediastream_signaling_->local_streams()->FindAudioTrack(track->id())) { |
| 374 | LOG(LS_ERROR) << "CreateDtmfSender is called with a non local audio track."; |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | talk_base::scoped_refptr<DtmfSenderInterface> sender( |
| 379 | DtmfSender::Create(track, signaling_thread(), session_.get())); |
| 380 | if (!sender.get()) { |
| 381 | LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create."; |
| 382 | return NULL; |
| 383 | } |
| 384 | return DtmfSenderProxy::Create(signaling_thread(), sender.get()); |
| 385 | } |
| 386 | |
| 387 | bool PeerConnection::GetStats(StatsObserver* observer, |
| 388 | MediaStreamTrackInterface* track) { |
| 389 | if (!VERIFY(observer != NULL)) { |
| 390 | LOG(LS_ERROR) << "GetStats - observer is NULL."; |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | stats_.UpdateStats(); |
| 395 | talk_base::scoped_ptr<GetStatsMsg> msg(new GetStatsMsg(observer)); |
| 396 | if (!stats_.GetStats(track, &(msg->reports))) { |
| 397 | return false; |
| 398 | } |
| 399 | signaling_thread()->Post(this, MSG_GETSTATS, msg.release()); |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 404 | return signaling_state_; |
| 405 | } |
| 406 | |
| 407 | PeerConnectionInterface::IceState PeerConnection::ice_state() { |
| 408 | return ice_state_; |
| 409 | } |
| 410 | |
| 411 | PeerConnectionInterface::IceConnectionState |
| 412 | PeerConnection::ice_connection_state() { |
| 413 | return ice_connection_state_; |
| 414 | } |
| 415 | |
| 416 | PeerConnectionInterface::IceGatheringState |
| 417 | PeerConnection::ice_gathering_state() { |
| 418 | return ice_gathering_state_; |
| 419 | } |
| 420 | |
| 421 | talk_base::scoped_refptr<DataChannelInterface> |
| 422 | PeerConnection::CreateDataChannel( |
| 423 | const std::string& label, |
| 424 | const DataChannelInit* config) { |
| 425 | talk_base::scoped_refptr<DataChannelInterface> channel( |
| 426 | session_->CreateDataChannel(label, config)); |
| 427 | if (!channel.get()) |
| 428 | return NULL; |
| 429 | |
| 430 | observer_->OnRenegotiationNeeded(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 431 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 432 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 433 | } |
| 434 | |
| 435 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 436 | const MediaConstraintsInterface* constraints) { |
| 437 | if (!VERIFY(observer != NULL)) { |
| 438 | LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 439 | return; |
| 440 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 441 | session_->CreateOffer(observer, constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void PeerConnection::CreateAnswer( |
| 445 | CreateSessionDescriptionObserver* observer, |
| 446 | const MediaConstraintsInterface* constraints) { |
| 447 | if (!VERIFY(observer != NULL)) { |
| 448 | LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 449 | return; |
| 450 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 451 | session_->CreateAnswer(observer, constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void PeerConnection::SetLocalDescription( |
| 455 | SetSessionDescriptionObserver* observer, |
| 456 | SessionDescriptionInterface* desc) { |
| 457 | if (!VERIFY(observer != NULL)) { |
| 458 | LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
| 459 | return; |
| 460 | } |
| 461 | if (!desc) { |
| 462 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 463 | return; |
| 464 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 465 | // Update stats here so that we have the most recent stats for tracks and |
| 466 | // streams that might be removed by updating the session description. |
| 467 | stats_.UpdateStats(); |
| 468 | std::string error; |
| 469 | if (!session_->SetLocalDescription(desc, &error)) { |
| 470 | PostSetSessionDescriptionFailure(observer, error); |
| 471 | return; |
| 472 | } |
| 473 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 474 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 475 | } |
| 476 | |
| 477 | void PeerConnection::SetRemoteDescription( |
| 478 | SetSessionDescriptionObserver* observer, |
| 479 | SessionDescriptionInterface* desc) { |
| 480 | if (!VERIFY(observer != NULL)) { |
| 481 | LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
| 482 | return; |
| 483 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | if (!desc) { |
| 485 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 486 | return; |
| 487 | } |
| 488 | // Update stats here so that we have the most recent stats for tracks and |
| 489 | // streams that might be removed by updating the session description. |
| 490 | stats_.UpdateStats(); |
| 491 | std::string error; |
| 492 | if (!session_->SetRemoteDescription(desc, &error)) { |
| 493 | PostSetSessionDescriptionFailure(observer, error); |
| 494 | return; |
| 495 | } |
| 496 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 497 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 498 | } |
| 499 | |
| 500 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 501 | SetSessionDescriptionObserver* observer, |
| 502 | const std::string& error) { |
| 503 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 504 | msg->error = error; |
| 505 | signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
| 506 | } |
| 507 | |
| 508 | bool PeerConnection::UpdateIce(const IceServers& configuration, |
| 509 | const MediaConstraintsInterface* constraints) { |
| 510 | // TODO(ronghuawu): Implement UpdateIce. |
| 511 | LOG(LS_ERROR) << "UpdateIce is not implemented."; |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | bool PeerConnection::AddIceCandidate( |
| 516 | const IceCandidateInterface* ice_candidate) { |
| 517 | return session_->ProcessIceMessage(ice_candidate); |
| 518 | } |
| 519 | |
| 520 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
| 521 | return session_->local_description(); |
| 522 | } |
| 523 | |
| 524 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
| 525 | return session_->remote_description(); |
| 526 | } |
| 527 | |
| 528 | void PeerConnection::Close() { |
| 529 | // Update stats here so that we have the most recent stats for tracks and |
| 530 | // streams before the channels are closed. |
| 531 | stats_.UpdateStats(); |
| 532 | |
| 533 | session_->Terminate(); |
| 534 | } |
| 535 | |
| 536 | void PeerConnection::OnSessionStateChange(cricket::BaseSession* /*session*/, |
| 537 | cricket::BaseSession::State state) { |
| 538 | switch (state) { |
| 539 | case cricket::BaseSession::STATE_INIT: |
| 540 | ChangeSignalingState(PeerConnectionInterface::kStable); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 541 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 542 | case cricket::BaseSession::STATE_SENTINITIATE: |
| 543 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalOffer); |
| 544 | break; |
| 545 | case cricket::BaseSession::STATE_SENTPRACCEPT: |
| 546 | ChangeSignalingState(PeerConnectionInterface::kHaveLocalPrAnswer); |
| 547 | break; |
| 548 | case cricket::BaseSession::STATE_RECEIVEDINITIATE: |
| 549 | ChangeSignalingState(PeerConnectionInterface::kHaveRemoteOffer); |
| 550 | break; |
| 551 | case cricket::BaseSession::STATE_RECEIVEDPRACCEPT: |
| 552 | ChangeSignalingState(PeerConnectionInterface::kHaveRemotePrAnswer); |
| 553 | break; |
| 554 | case cricket::BaseSession::STATE_SENTACCEPT: |
| 555 | case cricket::BaseSession::STATE_RECEIVEDACCEPT: |
| 556 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 557 | break; |
| 558 | case cricket::BaseSession::STATE_RECEIVEDTERMINATE: |
| 559 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 560 | break; |
| 561 | default: |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | void PeerConnection::OnMessage(talk_base::Message* msg) { |
| 567 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 568 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 569 | SetSessionDescriptionMsg* param = |
| 570 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 571 | param->observer->OnSuccess(); |
| 572 | delete param; |
| 573 | break; |
| 574 | } |
| 575 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 576 | SetSessionDescriptionMsg* param = |
| 577 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 578 | param->observer->OnFailure(param->error); |
| 579 | delete param; |
| 580 | break; |
| 581 | } |
| 582 | case MSG_GETSTATS: { |
| 583 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 584 | param->observer->OnComplete(param->reports); |
| 585 | delete param; |
| 586 | break; |
| 587 | } |
| 588 | case MSG_ICECONNECTIONCHANGE: { |
| 589 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 590 | break; |
| 591 | } |
| 592 | case MSG_ICEGATHERINGCHANGE: { |
| 593 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 594 | break; |
| 595 | } |
| 596 | case MSG_ICECANDIDATE: { |
| 597 | CandidateMsg* data = static_cast<CandidateMsg*>(msg->pdata); |
| 598 | observer_->OnIceCandidate(data->candidate.get()); |
| 599 | delete data; |
| 600 | break; |
| 601 | } |
| 602 | case MSG_ICECOMPLETE: { |
| 603 | observer_->OnIceComplete(); |
| 604 | break; |
| 605 | } |
| 606 | default: |
| 607 | ASSERT(false && "Not implemented"); |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | void PeerConnection::OnAddRemoteStream(MediaStreamInterface* stream) { |
| 613 | stats_.AddStream(stream); |
| 614 | observer_->OnAddStream(stream); |
| 615 | } |
| 616 | |
| 617 | void PeerConnection::OnRemoveRemoteStream(MediaStreamInterface* stream) { |
| 618 | stream_handler_container_->RemoveRemoteStream(stream); |
| 619 | observer_->OnRemoveStream(stream); |
| 620 | } |
| 621 | |
| 622 | void PeerConnection::OnAddDataChannel(DataChannelInterface* data_channel) { |
| 623 | observer_->OnDataChannel(DataChannelProxy::Create(signaling_thread(), |
| 624 | data_channel)); |
| 625 | } |
| 626 | |
| 627 | void PeerConnection::OnAddRemoteAudioTrack(MediaStreamInterface* stream, |
| 628 | AudioTrackInterface* audio_track, |
| 629 | uint32 ssrc) { |
| 630 | stream_handler_container_->AddRemoteAudioTrack(stream, audio_track, ssrc); |
| 631 | } |
| 632 | |
| 633 | void PeerConnection::OnAddRemoteVideoTrack(MediaStreamInterface* stream, |
| 634 | VideoTrackInterface* video_track, |
| 635 | uint32 ssrc) { |
| 636 | stream_handler_container_->AddRemoteVideoTrack(stream, video_track, ssrc); |
| 637 | } |
| 638 | |
| 639 | void PeerConnection::OnRemoveRemoteAudioTrack( |
| 640 | MediaStreamInterface* stream, |
| 641 | AudioTrackInterface* audio_track) { |
| 642 | stream_handler_container_->RemoveRemoteTrack(stream, audio_track); |
| 643 | } |
| 644 | |
| 645 | void PeerConnection::OnRemoveRemoteVideoTrack( |
| 646 | MediaStreamInterface* stream, |
| 647 | VideoTrackInterface* video_track) { |
| 648 | stream_handler_container_->RemoveRemoteTrack(stream, video_track); |
| 649 | } |
| 650 | void PeerConnection::OnAddLocalAudioTrack(MediaStreamInterface* stream, |
| 651 | AudioTrackInterface* audio_track, |
| 652 | uint32 ssrc) { |
| 653 | stream_handler_container_->AddLocalAudioTrack(stream, audio_track, ssrc); |
| 654 | } |
| 655 | void PeerConnection::OnAddLocalVideoTrack(MediaStreamInterface* stream, |
| 656 | VideoTrackInterface* video_track, |
| 657 | uint32 ssrc) { |
| 658 | stream_handler_container_->AddLocalVideoTrack(stream, video_track, ssrc); |
| 659 | } |
| 660 | |
| 661 | void PeerConnection::OnRemoveLocalAudioTrack(MediaStreamInterface* stream, |
| 662 | AudioTrackInterface* audio_track) { |
| 663 | stream_handler_container_->RemoveLocalTrack(stream, audio_track); |
| 664 | } |
| 665 | |
| 666 | void PeerConnection::OnRemoveLocalVideoTrack(MediaStreamInterface* stream, |
| 667 | VideoTrackInterface* video_track) { |
| 668 | stream_handler_container_->RemoveLocalTrack(stream, video_track); |
| 669 | } |
| 670 | |
| 671 | void PeerConnection::OnRemoveLocalStream(MediaStreamInterface* stream) { |
| 672 | stream_handler_container_->RemoveLocalStream(stream); |
| 673 | } |
| 674 | |
| 675 | void PeerConnection::OnIceConnectionChange( |
| 676 | PeerConnectionInterface::IceConnectionState new_state) { |
| 677 | ice_connection_state_ = new_state; |
| 678 | signaling_thread()->Post(this, MSG_ICECONNECTIONCHANGE); |
| 679 | } |
| 680 | |
| 681 | void PeerConnection::OnIceGatheringChange( |
| 682 | PeerConnectionInterface::IceGatheringState new_state) { |
| 683 | if (IsClosed()) { |
| 684 | return; |
| 685 | } |
| 686 | ice_gathering_state_ = new_state; |
| 687 | signaling_thread()->Post(this, MSG_ICEGATHERINGCHANGE); |
| 688 | } |
| 689 | |
| 690 | void PeerConnection::OnIceCandidate(const IceCandidateInterface* candidate) { |
| 691 | JsepIceCandidate* candidate_copy = NULL; |
| 692 | if (candidate) { |
| 693 | // TODO(ronghuawu): Make IceCandidateInterface reference counted instead |
| 694 | // of making a copy. |
| 695 | candidate_copy = new JsepIceCandidate(candidate->sdp_mid(), |
| 696 | candidate->sdp_mline_index(), |
| 697 | candidate->candidate()); |
| 698 | } |
| 699 | // The Post takes the ownership of the |candidate_copy|. |
| 700 | signaling_thread()->Post(this, MSG_ICECANDIDATE, |
| 701 | new CandidateMsg(candidate_copy)); |
| 702 | } |
| 703 | |
| 704 | void PeerConnection::OnIceComplete() { |
| 705 | signaling_thread()->Post(this, MSG_ICECOMPLETE); |
| 706 | } |
| 707 | |
| 708 | void PeerConnection::ChangeSignalingState( |
| 709 | PeerConnectionInterface::SignalingState signaling_state) { |
| 710 | signaling_state_ = signaling_state; |
| 711 | if (signaling_state == kClosed) { |
| 712 | ice_connection_state_ = kIceConnectionClosed; |
| 713 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 714 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 715 | ice_gathering_state_ = kIceGatheringComplete; |
| 716 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 717 | } |
| 718 | } |
| 719 | observer_->OnSignalingChange(signaling_state_); |
| 720 | observer_->OnStateChange(PeerConnectionObserver::kSignalingState); |
| 721 | } |
| 722 | |
| 723 | } // namespace webrtc |