Created
June 30, 2014 19:03
-
-
Save yiding/1bfe9bbf5187a6ff6f34 to your computer and use it in GitHub Desktop.
WebRTC native, forcing ports to be allocated in a certain range.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PortAllocatorFactoryWrapper : public PortAllocatorFactory { | |
public: | |
PortAllocatorFactoryWrapper(talk_base::Thread* worker_thread, int minPort, int maxPort) | |
: PortAllocatorFactory(worker_thread), minPort_(minPort), maxPort_(maxPort) {} | |
virtual cricket::PortAllocator *CreatePortAllocator( | |
const std::vector<StunConfiguration> &stun, | |
const std::vector<TurnConfiguration> &turn) override { | |
auto allocator = PortAllocatorFactory::CreatePortAllocator(stun, turn); | |
allocator->SetPortRange(minPort_, maxPort_); | |
return allocator; | |
} | |
private: | |
int minPort_; | |
int maxPort_; | |
}; | |
// Example use: Pass an instance of this factory into CreatePeerConnection. | |
PeerConnectionInterface *createPeerConnection( | |
PeerConnectionFactoryInterface *factory, | |
MediaConstraintsInterface *mediaConstraints, | |
PeerConnectionObserver *observer) | |
{ | |
return factory->CreatePeerConnection( | |
PeerConnectionInterface::IceServers(), | |
mediaConstraints, | |
new talk_base::RefCountedObject<PortAllocatorFactoryWrapper>( | |
static_cast<PeerConnectionFactory *>(factory.get())->worker_thread(), | |
portRange.first, | |
portRange.second), | |
nullptr, | |
observer); | |
} |
Nice ! Please note that static_cast<PeerConnectionFactory *>(factory.get())->worker_thread()
can be replaced with rtc::Thread::Current()
=> cleaner, shorter, safer 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!