From f32c8517c54ff7c16c8ec10f2a3743f09bb8011f Mon Sep 17 00:00:00 2001 From: Axel Isouard Date: Wed, 29 Mar 2017 20:14:57 +0200 Subject: [PATCH] (#4): Move PeerConnectionFactory initialization inside RTCPeerConnection --- src/globals.cc | 25 ++++++------------------- src/globals.h | 6 +++--- src/rtcpeerconnection.cc | 7 ++++++- src/rtcpeerconnection.h | 2 ++ 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/globals.cc b/src/globals.cc index b0fde03..1896f2b 100644 --- a/src/globals.cc +++ b/src/globals.cc @@ -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 - Globals::_peerConnectionFactory = NULL; bool Globals::Init() { _eventQueue = new EventQueue(); @@ -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(); @@ -89,7 +73,6 @@ void Globals::Cleanup(void* args) { rtc::CleanupSSL(); delete _eventQueue; - _eventQueue = NULL; } @@ -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; } diff --git a/src/globals.h b/src/globals.h index fc8f15d..460cf69 100644 --- a/src/globals.h +++ b/src/globals.h @@ -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 - _peerConnectionFactory; + static webrtc::PeerConnectionFactoryInterface *_peerConnectionFactory; }; #endif // GLOBALS_H_ diff --git a/src/rtcpeerconnection.cc b/src/rtcpeerconnection.cc index af52b55..2209cf4 100644 --- a/src/rtcpeerconnection.cc +++ b/src/rtcpeerconnection.cc @@ -113,8 +113,12 @@ 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); } @@ -122,6 +126,7 @@ RTCPeerConnection::RTCPeerConnection( RTCPeerConnection::~RTCPeerConnection() { _peerConnection = NULL; _peerConnectionObserver = NULL; + _peerConnectionFactory = NULL; } NAN_METHOD(RTCPeerConnection::New) { diff --git a/src/rtcpeerconnection.h b/src/rtcpeerconnection.h index 3157a00..9bb302e 100644 --- a/src/rtcpeerconnection.h +++ b/src/rtcpeerconnection.h @@ -65,6 +65,8 @@ class RTCPeerConnection : public Nan::ObjectWrap { static Nan::Persistent constructor; protected: + rtc::scoped_refptr + _peerConnectionFactory; rtc::scoped_refptr _peerConnection; rtc::scoped_refptr _peerConnectionObserver; };