Skip to content

Commit

Permalink
(#4): Move PeerConnectionFactory initialization inside RTCPeerConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
aisouard committed Mar 29, 2017
1 parent 73a2eaf commit f32c851
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
25 changes: 6 additions & 19 deletions src/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ EventQueue *Globals::_eventQueue = NULL;
rtc::Thread *Globals::_signalingThread = NULL;
rtc::Thread *Globals::_workerThread = NULL;
rtc::RTCCertificateGenerator *Globals::_certificateGenerator = NULL;
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
Globals::_peerConnectionFactory = NULL;

bool Globals::Init() {
_eventQueue = new EventQueue();
Expand All @@ -54,29 +52,15 @@ bool Globals::Init() {
return false;
}

rtc::ThreadManager::Instance()->SetCurrentThread(_signalingThread);
if (rtc::ThreadManager::Instance()->CurrentThread() != _signalingThread) {
Nan::ThrowError("Failed to set the current thread.");
}

_certificateGenerator =
new rtc::RTCCertificateGenerator(_signalingThread, _workerThread);

_peerConnectionFactory =
webrtc::CreatePeerConnectionFactory(_workerThread, _signalingThread,
NULL, NULL, NULL);
return true;
}

void Globals::Cleanup(void* args) {
_peerConnectionFactory = NULL;

delete _certificateGenerator;

if (rtc::ThreadManager::Instance()->CurrentThread() == _signalingThread) {
rtc::ThreadManager::Instance()->SetCurrentThread(NULL);
}

_signalingThread->Stop();
_workerThread->Stop();

Expand All @@ -89,7 +73,6 @@ void Globals::Cleanup(void* args) {
rtc::CleanupSSL();

delete _eventQueue;

_eventQueue = NULL;
}

Expand All @@ -101,6 +84,10 @@ rtc::RTCCertificateGenerator *Globals::GetCertificateGenerator() {
return _certificateGenerator;
}

webrtc::PeerConnectionFactoryInterface *Globals::GetPeerConnectionFactory() {
return _peerConnectionFactory.get();
rtc::Thread *Globals::GetSignalingThread() {
return _signalingThread;
}

rtc::Thread *Globals::GetWorkerThread() {
return _workerThread;
}
6 changes: 3 additions & 3 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class Globals {

static EventQueue *GetEventQueue();
static rtc::RTCCertificateGenerator *GetCertificateGenerator();
static webrtc::PeerConnectionFactoryInterface *GetPeerConnectionFactory();
static rtc::Thread *GetSignalingThread();
static rtc::Thread *GetWorkerThread();

private:
static EventQueue *_eventQueue;
static rtc::Thread *_signalingThread;
static rtc::Thread *_workerThread;
static rtc::RTCCertificateGenerator *_certificateGenerator;
static rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
_peerConnectionFactory;
static webrtc::PeerConnectionFactoryInterface *_peerConnectionFactory;
};

#endif // GLOBALS_H_
7 changes: 6 additions & 1 deletion src/rtcpeerconnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,20 @@ RTCPeerConnection::RTCPeerConnection(
const webrtc::PeerConnectionInterface::RTCConfiguration& config,
const webrtc::MediaConstraintsInterface& constraints) {

_peerConnectionFactory = webrtc::CreatePeerConnectionFactory(
Globals::GetSignalingThread(), Globals::GetWorkerThread(),
NULL, NULL, NULL);

_peerConnectionObserver = PeerConnectionObserver::Create();
_peerConnection = Globals::GetPeerConnectionFactory()->CreatePeerConnection(
_peerConnection = _peerConnectionFactory->CreatePeerConnection(
config, &constraints, NULL, NULL, _peerConnectionObserver);
_peerConnectionObserver->SetPeerConnection(_peerConnection);
}

RTCPeerConnection::~RTCPeerConnection() {
_peerConnection = NULL;
_peerConnectionObserver = NULL;
_peerConnectionFactory = NULL;
}

NAN_METHOD(RTCPeerConnection::New) {
Expand Down
2 changes: 2 additions & 0 deletions src/rtcpeerconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class RTCPeerConnection : public Nan::ObjectWrap {
static Nan::Persistent<FunctionTemplate> constructor;

protected:
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
_peerConnectionFactory;
rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection;
rtc::scoped_refptr<PeerConnectionObserver> _peerConnectionObserver;
};
Expand Down

0 comments on commit f32c851

Please sign in to comment.