Skip to content

Instantly share code, notes, and snippets.

@yiding
Created June 30, 2014 19:03
Show Gist options
  • Save yiding/1bfe9bbf5187a6ff6f34 to your computer and use it in GitHub Desktop.
Save yiding/1bfe9bbf5187a6ff6f34 to your computer and use it in GitHub Desktop.
WebRTC native, forcing ports to be allocated in a certain range.
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);
}
@pvnick
Copy link

pvnick commented Jun 5, 2015

Nice!

@jcleneveu-geny
Copy link

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