diff --git a/src/libwebrtc.cc b/src/libwebrtc.cc index ebd21ceec2..8ecba33e82 100644 --- a/src/libwebrtc.cc +++ b/src/libwebrtc.cc @@ -10,37 +10,12 @@ namespace libwebrtc { // Initialize static variable g_is_initialized to false. static bool g_is_initialized = false; -// Thread pointers for different thread types. -std::unique_ptr worker_thread; -std::unique_ptr signaling_thread; -std::unique_ptr network_thread; -// Initializes SSL and the required threads, if not initialized. +// Initializes SSL, if not initialized. bool LibWebRTC::Initialize() { if (!g_is_initialized) { rtc::InitializeSSL(); g_is_initialized = true; - - // Creates and starts the worker thread. - if (worker_thread.get() == nullptr) { - worker_thread = rtc::Thread::Create(); - worker_thread->SetName("worker_thread", nullptr); - RTC_CHECK(worker_thread->Start()) << "Failed to start thread"; - } - - // Creates and starts the signaling thread. - if (signaling_thread.get() == nullptr) { - signaling_thread = rtc::Thread::Create(); - signaling_thread->SetName("signaling_thread", NULL); - RTC_CHECK(signaling_thread->Start()) << "Failed to start thread"; - } - - // Creates and starts the network thread. - if (network_thread.get() == nullptr) { - network_thread = rtc::Thread::CreateWithSocketServer(); - network_thread->SetName("network_thread", nullptr); - RTC_CHECK(network_thread->Start()) << "Failed to start thread"; - } } return g_is_initialized; } @@ -50,24 +25,6 @@ void LibWebRTC::Terminate() { rtc::ThreadManager::Instance()->SetCurrentThread(NULL); rtc::CleanupSSL(); - // Stops and resets the worker thread. - if (worker_thread.get() != nullptr) { - worker_thread->Stop(); - worker_thread.reset(nullptr); - } - - // Stops and resets the signaling thread. - if (signaling_thread.get() != nullptr) { - signaling_thread->Stop(); - signaling_thread.reset(nullptr); - } - - // Stops and resets the network thread. - if (network_thread.get() != nullptr) { - network_thread->Stop(); - network_thread.reset(nullptr); - } - // Resets the static variable g_is_initialized to false. g_is_initialized = false; } @@ -77,9 +34,7 @@ scoped_refptr LibWebRTC::CreateRTCPeerConnectionFactory() { scoped_refptr rtc_peerconnection_factory = scoped_refptr( - new RefCountedObject( - worker_thread.get(), signaling_thread.get(), - network_thread.get())); + new RefCountedObject()); rtc_peerconnection_factory->Initialize(); return rtc_peerconnection_factory; } diff --git a/src/rtc_peerconnection_factory_impl.cc b/src/rtc_peerconnection_factory_impl.cc index d2c01dce4e..8e3d75d768 100644 --- a/src/rtc_peerconnection_factory_impl.cc +++ b/src/rtc_peerconnection_factory_impl.cc @@ -42,17 +42,22 @@ std::unique_ptr CreateIntelVideoDecoderFactory() { } #endif -RTCPeerConnectionFactoryImpl::RTCPeerConnectionFactoryImpl( - rtc::Thread* worker_thread, - rtc::Thread* signaling_thread, - rtc::Thread* network_thread) - : worker_thread_(worker_thread), - signaling_thread_(signaling_thread), - network_thread_(network_thread) {} +RTCPeerConnectionFactoryImpl::RTCPeerConnectionFactoryImpl() {} RTCPeerConnectionFactoryImpl::~RTCPeerConnectionFactoryImpl() {} bool RTCPeerConnectionFactoryImpl::Initialize() { + worker_thread_ = rtc::Thread::Create(); + worker_thread_->SetName("worker_thread", nullptr); + RTC_CHECK(worker_thread_->Start()) << "Failed to start thread"; + + signaling_thread_ = rtc::Thread::Create(); + signaling_thread_->SetName("signaling_thread", nullptr); + RTC_CHECK(signaling_thread_->Start()) << "Failed to start thread"; + + network_thread_ = rtc::Thread::CreateWithSocketServer(); + network_thread_->SetName("network_thread", nullptr); + RTC_CHECK(network_thread_->Start()) << "Failed to start thread"; if (!audio_device_module_) { task_queue_factory_ = webrtc::CreateDefaultTaskQueueFactory(); worker_thread_->Invoke(RTC_FROM_HERE, @@ -61,7 +66,7 @@ bool RTCPeerConnectionFactoryImpl::Initialize() { if (!rtc_peerconnection_factory_) { rtc_peerconnection_factory_ = webrtc::CreatePeerConnectionFactory( - network_thread_, worker_thread_, signaling_thread_, + network_thread_.get(), worker_thread_.get(), signaling_thread_.get(), audio_device_module_, webrtc::CreateBuiltinAudioEncoderFactory(), webrtc::CreateBuiltinAudioDecoderFactory(), #if defined(USE_INTEL_MEDIA_SDK) @@ -138,7 +143,7 @@ scoped_refptr RTCPeerConnectionFactoryImpl::GetAudioDevice() { if (!audio_device_impl_) audio_device_impl_ = scoped_refptr(new RefCountedObject( - audio_device_module_, worker_thread_)); + audio_device_module_, worker_thread_.get())); return audio_device_impl_; } @@ -146,8 +151,8 @@ scoped_refptr RTCPeerConnectionFactoryImpl::GetAudioDevice() { scoped_refptr RTCPeerConnectionFactoryImpl::GetVideoDevice() { if (!video_device_impl_) video_device_impl_ = scoped_refptr( - new RefCountedObject(signaling_thread_, - worker_thread_)); + new RefCountedObject(signaling_thread_.get(), + worker_thread_.get())); return video_device_impl_; } @@ -167,7 +172,7 @@ scoped_refptr RTCPeerConnectionFactoryImpl::GetDesktopDevice() { if (!desktop_device_impl_) { desktop_device_impl_ = scoped_refptr( - new RefCountedObject(signaling_thread_)); + new RefCountedObject(signaling_thread_.get())); } return desktop_device_impl_; } @@ -177,7 +182,7 @@ scoped_refptr RTCPeerConnectionFactoryImpl::CreateVideoSource( scoped_refptr capturer, const string video_source_label, scoped_refptr constraints) { - if (rtc::Thread::Current() != signaling_thread_) { + if (rtc::Thread::Current() != signaling_thread_.get()) { scoped_refptr source = signaling_thread_->Invoke>( RTC_FROM_HERE, [this, capturer, video_source_label, constraints] { @@ -214,7 +219,7 @@ scoped_refptr RTCPeerConnectionFactoryImpl::CreateDesktopSource( scoped_refptr capturer, const string video_source_label, scoped_refptr constraints) { - if (rtc::Thread::Current() != signaling_thread_) { + if (rtc::Thread::Current() != signaling_thread_.get()) { scoped_refptr source = signaling_thread_->Invoke>( RTC_FROM_HERE, [this, capturer, video_source_label, constraints] { @@ -300,7 +305,7 @@ scoped_refptr RTCPeerConnectionFactoryImpl::CreateAudioTrack( scoped_refptr RTCPeerConnectionFactoryImpl::GetRtpSenderCapabilities( RTCMediaType media_type) { - if (rtc::Thread::Current() != signaling_thread_) { + if (rtc::Thread::Current() != signaling_thread_.get()) { scoped_refptr capabilities = signaling_thread_->Invoke>( RTC_FROM_HERE, [this, media_type] { @@ -329,7 +334,7 @@ RTCPeerConnectionFactoryImpl::GetRtpSenderCapabilities( scoped_refptr RTCPeerConnectionFactoryImpl::GetRtpReceiverCapabilities( RTCMediaType media_type) { - if (rtc::Thread::Current() != signaling_thread_) { + if (rtc::Thread::Current() != signaling_thread_.get()) { scoped_refptr capabilities = signaling_thread_->Invoke>( RTC_FROM_HERE, [this, media_type] { diff --git a/src/rtc_peerconnection_factory_impl.h b/src/rtc_peerconnection_factory_impl.h index 578cd768f5..3ac2519172 100644 --- a/src/rtc_peerconnection_factory_impl.h +++ b/src/rtc_peerconnection_factory_impl.h @@ -22,9 +22,7 @@ namespace libwebrtc { class RTCPeerConnectionFactoryImpl : public RTCPeerConnectionFactory { public: - RTCPeerConnectionFactoryImpl(rtc::Thread* worker_thread, - rtc::Thread* signaling_thread, - rtc::Thread* network_thread); + RTCPeerConnectionFactoryImpl(); virtual ~RTCPeerConnectionFactoryImpl(); @@ -94,9 +92,9 @@ class RTCPeerConnectionFactoryImpl : public RTCPeerConnectionFactory { scoped_refptr constraints); #endif private: - rtc::Thread* worker_thread_ = nullptr; - rtc::Thread* signaling_thread_ = nullptr; - rtc::Thread* network_thread_ = nullptr; + std::unique_ptr worker_thread_; + std::unique_ptr signaling_thread_; + std::unique_ptr network_thread_; rtc::scoped_refptr rtc_peerconnection_factory_; rtc::scoped_refptr audio_device_module_;