Skip to content

Move threads code to rtc_peerconnection_factory #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 2 additions & 47 deletions src/libwebrtc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<rtc::Thread> worker_thread;
std::unique_ptr<rtc::Thread> signaling_thread;
std::unique_ptr<rtc::Thread> 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;
}
Expand All @@ -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;
}
Expand All @@ -77,9 +34,7 @@ scoped_refptr<RTCPeerConnectionFactory>
LibWebRTC::CreateRTCPeerConnectionFactory() {
scoped_refptr<RTCPeerConnectionFactory> rtc_peerconnection_factory =
scoped_refptr<RTCPeerConnectionFactory>(
new RefCountedObject<RTCPeerConnectionFactoryImpl>(
worker_thread.get(), signaling_thread.get(),
network_thread.get()));
new RefCountedObject<RTCPeerConnectionFactoryImpl>());
rtc_peerconnection_factory->Initialize();
return rtc_peerconnection_factory;
}
Expand Down
37 changes: 21 additions & 16 deletions src/rtc_peerconnection_factory_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,22 @@ std::unique_ptr<webrtc::VideoDecoderFactory> 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<void>(RTC_FROM_HERE,
Expand All @@ -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)
Expand Down Expand Up @@ -138,16 +143,16 @@ scoped_refptr<RTCAudioDevice> RTCPeerConnectionFactoryImpl::GetAudioDevice() {
if (!audio_device_impl_)
audio_device_impl_ =
scoped_refptr<AudioDeviceImpl>(new RefCountedObject<AudioDeviceImpl>(
audio_device_module_, worker_thread_));
audio_device_module_, worker_thread_.get()));

return audio_device_impl_;
}

scoped_refptr<RTCVideoDevice> RTCPeerConnectionFactoryImpl::GetVideoDevice() {
if (!video_device_impl_)
video_device_impl_ = scoped_refptr<RTCVideoDeviceImpl>(
new RefCountedObject<RTCVideoDeviceImpl>(signaling_thread_,
worker_thread_));
new RefCountedObject<RTCVideoDeviceImpl>(signaling_thread_.get(),
worker_thread_.get()));

return video_device_impl_;
}
Expand All @@ -167,7 +172,7 @@ scoped_refptr<RTCDesktopDevice>
RTCPeerConnectionFactoryImpl::GetDesktopDevice() {
if (!desktop_device_impl_) {
desktop_device_impl_ = scoped_refptr<RTCDesktopDeviceImpl>(
new RefCountedObject<RTCDesktopDeviceImpl>(signaling_thread_));
new RefCountedObject<RTCDesktopDeviceImpl>(signaling_thread_.get()));
}
return desktop_device_impl_;
}
Expand All @@ -177,7 +182,7 @@ scoped_refptr<RTCVideoSource> RTCPeerConnectionFactoryImpl::CreateVideoSource(
scoped_refptr<RTCVideoCapturer> capturer,
const string video_source_label,
scoped_refptr<RTCMediaConstraints> constraints) {
if (rtc::Thread::Current() != signaling_thread_) {
if (rtc::Thread::Current() != signaling_thread_.get()) {
scoped_refptr<RTCVideoSource> source =
signaling_thread_->Invoke<scoped_refptr<RTCVideoSource>>(
RTC_FROM_HERE, [this, capturer, video_source_label, constraints] {
Expand Down Expand Up @@ -214,7 +219,7 @@ scoped_refptr<RTCVideoSource> RTCPeerConnectionFactoryImpl::CreateDesktopSource(
scoped_refptr<RTCDesktopCapturer> capturer,
const string video_source_label,
scoped_refptr<RTCMediaConstraints> constraints) {
if (rtc::Thread::Current() != signaling_thread_) {
if (rtc::Thread::Current() != signaling_thread_.get()) {
scoped_refptr<RTCVideoSource> source =
signaling_thread_->Invoke<scoped_refptr<RTCVideoSource>>(
RTC_FROM_HERE, [this, capturer, video_source_label, constraints] {
Expand Down Expand Up @@ -300,7 +305,7 @@ scoped_refptr<RTCAudioTrack> RTCPeerConnectionFactoryImpl::CreateAudioTrack(
scoped_refptr<RTCRtpCapabilities>
RTCPeerConnectionFactoryImpl::GetRtpSenderCapabilities(
RTCMediaType media_type) {
if (rtc::Thread::Current() != signaling_thread_) {
if (rtc::Thread::Current() != signaling_thread_.get()) {
scoped_refptr<RTCRtpCapabilities> capabilities =
signaling_thread_->Invoke<scoped_refptr<RTCRtpCapabilities>>(
RTC_FROM_HERE, [this, media_type] {
Expand Down Expand Up @@ -329,7 +334,7 @@ RTCPeerConnectionFactoryImpl::GetRtpSenderCapabilities(
scoped_refptr<RTCRtpCapabilities>
RTCPeerConnectionFactoryImpl::GetRtpReceiverCapabilities(
RTCMediaType media_type) {
if (rtc::Thread::Current() != signaling_thread_) {
if (rtc::Thread::Current() != signaling_thread_.get()) {
scoped_refptr<RTCRtpCapabilities> capabilities =
signaling_thread_->Invoke<scoped_refptr<RTCRtpCapabilities>>(
RTC_FROM_HERE, [this, media_type] {
Expand Down
10 changes: 4 additions & 6 deletions src/rtc_peerconnection_factory_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -94,9 +92,9 @@ class RTCPeerConnectionFactoryImpl : public RTCPeerConnectionFactory {
scoped_refptr<RTCMediaConstraints> constraints);
#endif
private:
rtc::Thread* worker_thread_ = nullptr;
rtc::Thread* signaling_thread_ = nullptr;
rtc::Thread* network_thread_ = nullptr;
std::unique_ptr<rtc::Thread> worker_thread_;
std::unique_ptr<rtc::Thread> signaling_thread_;
std::unique_ptr<rtc::Thread> network_thread_;
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
rtc_peerconnection_factory_;
rtc::scoped_refptr<webrtc::AudioDeviceModule> audio_device_module_;
Expand Down