henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/p2p/base/portallocator.h" |
| 12 | |
| 13 | #include "webrtc/p2p/base/portallocatorsessionproxy.h" |
| 14 | |
| 15 | namespace cricket { |
| 16 | |
| 17 | PortAllocatorSession::PortAllocatorSession(const std::string& content_name, |
| 18 | int component, |
| 19 | const std::string& ice_ufrag, |
| 20 | const std::string& ice_pwd, |
| 21 | uint32 flags) |
| 22 | : content_name_(content_name), |
| 23 | component_(component), |
| 24 | flags_(flags), |
| 25 | generation_(0), |
| 26 | // If PORTALLOCATOR_ENABLE_SHARED_UFRAG flag is not enabled, ignore the |
| 27 | // incoming ufrag and pwd, which will cause each Port to generate one |
| 28 | // by itself. |
| 29 | username_(flags_ & PORTALLOCATOR_ENABLE_SHARED_UFRAG ? ice_ufrag : ""), |
| 30 | password_(flags_ & PORTALLOCATOR_ENABLE_SHARED_UFRAG ? ice_pwd : "") { |
| 31 | } |
| 32 | |
| 33 | PortAllocator::~PortAllocator() { |
| 34 | for (SessionMuxerMap::iterator iter = muxers_.begin(); |
| 35 | iter != muxers_.end(); ++iter) { |
| 36 | delete iter->second; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | PortAllocatorSession* PortAllocator::CreateSession( |
| 41 | const std::string& sid, |
| 42 | const std::string& content_name, |
| 43 | int component, |
| 44 | const std::string& ice_ufrag, |
| 45 | const std::string& ice_pwd) { |
| 46 | if (flags_ & PORTALLOCATOR_ENABLE_BUNDLE) { |
| 47 | // If we just use |sid| as key in identifying PortAllocatorSessionMuxer, |
| 48 | // ICE restart will not result in different candidates, as |sid| will |
| 49 | // be same. To yield different candiates we are using combination of |
| 50 | // |ice_ufrag| and |ice_pwd|. |
| 51 | // Ideally |ice_ufrag| and |ice_pwd| should change together, but |
| 52 | // there can be instances where only ice_pwd will be changed. |
| 53 | std::string key_str = ice_ufrag + ":" + ice_pwd; |
| 54 | PortAllocatorSessionMuxer* muxer = GetSessionMuxer(key_str); |
| 55 | if (!muxer) { |
| 56 | PortAllocatorSession* session_impl = CreateSessionInternal( |
| 57 | content_name, component, ice_ufrag, ice_pwd); |
| 58 | // Create PortAllocatorSessionMuxer object for |session_impl|. |
| 59 | muxer = new PortAllocatorSessionMuxer(session_impl); |
| 60 | muxer->SignalDestroyed.connect( |
| 61 | this, &PortAllocator::OnSessionMuxerDestroyed); |
| 62 | // Add PortAllocatorSession to the map. |
| 63 | muxers_[key_str] = muxer; |
| 64 | } |
| 65 | PortAllocatorSessionProxy* proxy = |
| 66 | new PortAllocatorSessionProxy(content_name, component, flags_); |
| 67 | muxer->RegisterSessionProxy(proxy); |
| 68 | return proxy; |
| 69 | } |
| 70 | return CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd); |
| 71 | } |
| 72 | |
| 73 | PortAllocatorSessionMuxer* PortAllocator::GetSessionMuxer( |
| 74 | const std::string& key) const { |
| 75 | SessionMuxerMap::const_iterator iter = muxers_.find(key); |
| 76 | if (iter != muxers_.end()) |
| 77 | return iter->second; |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | void PortAllocator::OnSessionMuxerDestroyed( |
| 82 | PortAllocatorSessionMuxer* session) { |
| 83 | SessionMuxerMap::iterator iter; |
| 84 | for (iter = muxers_.begin(); iter != muxers_.end(); ++iter) { |
| 85 | if (iter->second == session) |
| 86 | break; |
| 87 | } |
| 88 | if (iter != muxers_.end()) |
| 89 | muxers_.erase(iter); |
| 90 | } |
| 91 | |
| 92 | } // namespace cricket |