Skip to content

Commit d38d698

Browse files
authored
Move threads code from libwebrtc.cc to rtc_peerconnection_factory (#71)
1 parent e1d090c commit d38d698

File tree

3 files changed

+27
-69
lines changed

3 files changed

+27
-69
lines changed

src/libwebrtc.cc

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,12 @@ namespace libwebrtc {
1010

1111
// Initialize static variable g_is_initialized to false.
1212
static bool g_is_initialized = false;
13-
// Thread pointers for different thread types.
14-
std::unique_ptr<rtc::Thread> worker_thread;
15-
std::unique_ptr<rtc::Thread> signaling_thread;
16-
std::unique_ptr<rtc::Thread> network_thread;
1713

18-
// Initializes SSL and the required threads, if not initialized.
14+
// Initializes SSL, if not initialized.
1915
bool LibWebRTC::Initialize() {
2016
if (!g_is_initialized) {
2117
rtc::InitializeSSL();
2218
g_is_initialized = true;
23-
24-
// Creates and starts the worker thread.
25-
if (worker_thread.get() == nullptr) {
26-
worker_thread = rtc::Thread::Create();
27-
worker_thread->SetName("worker_thread", nullptr);
28-
RTC_CHECK(worker_thread->Start()) << "Failed to start thread";
29-
}
30-
31-
// Creates and starts the signaling thread.
32-
if (signaling_thread.get() == nullptr) {
33-
signaling_thread = rtc::Thread::Create();
34-
signaling_thread->SetName("signaling_thread", NULL);
35-
RTC_CHECK(signaling_thread->Start()) << "Failed to start thread";
36-
}
37-
38-
// Creates and starts the network thread.
39-
if (network_thread.get() == nullptr) {
40-
network_thread = rtc::Thread::CreateWithSocketServer();
41-
network_thread->SetName("network_thread", nullptr);
42-
RTC_CHECK(network_thread->Start()) << "Failed to start thread";
43-
}
4419
}
4520
return g_is_initialized;
4621
}
@@ -50,24 +25,6 @@ void LibWebRTC::Terminate() {
5025
rtc::ThreadManager::Instance()->SetCurrentThread(NULL);
5126
rtc::CleanupSSL();
5227

53-
// Stops and resets the worker thread.
54-
if (worker_thread.get() != nullptr) {
55-
worker_thread->Stop();
56-
worker_thread.reset(nullptr);
57-
}
58-
59-
// Stops and resets the signaling thread.
60-
if (signaling_thread.get() != nullptr) {
61-
signaling_thread->Stop();
62-
signaling_thread.reset(nullptr);
63-
}
64-
65-
// Stops and resets the network thread.
66-
if (network_thread.get() != nullptr) {
67-
network_thread->Stop();
68-
network_thread.reset(nullptr);
69-
}
70-
7128
// Resets the static variable g_is_initialized to false.
7229
g_is_initialized = false;
7330
}
@@ -77,9 +34,7 @@ scoped_refptr<RTCPeerConnectionFactory>
7734
LibWebRTC::CreateRTCPeerConnectionFactory() {
7835
scoped_refptr<RTCPeerConnectionFactory> rtc_peerconnection_factory =
7936
scoped_refptr<RTCPeerConnectionFactory>(
80-
new RefCountedObject<RTCPeerConnectionFactoryImpl>(
81-
worker_thread.get(), signaling_thread.get(),
82-
network_thread.get()));
37+
new RefCountedObject<RTCPeerConnectionFactoryImpl>());
8338
rtc_peerconnection_factory->Initialize();
8439
return rtc_peerconnection_factory;
8540
}

src/rtc_peerconnection_factory_impl.cc

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,22 @@ std::unique_ptr<webrtc::VideoDecoderFactory> CreateIntelVideoDecoderFactory() {
4242
}
4343
#endif
4444

45-
RTCPeerConnectionFactoryImpl::RTCPeerConnectionFactoryImpl(
46-
rtc::Thread* worker_thread,
47-
rtc::Thread* signaling_thread,
48-
rtc::Thread* network_thread)
49-
: worker_thread_(worker_thread),
50-
signaling_thread_(signaling_thread),
51-
network_thread_(network_thread) {}
45+
RTCPeerConnectionFactoryImpl::RTCPeerConnectionFactoryImpl() {}
5246

5347
RTCPeerConnectionFactoryImpl::~RTCPeerConnectionFactoryImpl() {}
5448

5549
bool RTCPeerConnectionFactoryImpl::Initialize() {
50+
worker_thread_ = rtc::Thread::Create();
51+
worker_thread_->SetName("worker_thread", nullptr);
52+
RTC_CHECK(worker_thread_->Start()) << "Failed to start thread";
53+
54+
signaling_thread_ = rtc::Thread::Create();
55+
signaling_thread_->SetName("signaling_thread", nullptr);
56+
RTC_CHECK(signaling_thread_->Start()) << "Failed to start thread";
57+
58+
network_thread_ = rtc::Thread::CreateWithSocketServer();
59+
network_thread_->SetName("network_thread", nullptr);
60+
RTC_CHECK(network_thread_->Start()) << "Failed to start thread";
5661
if (!audio_device_module_) {
5762
task_queue_factory_ = webrtc::CreateDefaultTaskQueueFactory();
5863
worker_thread_->Invoke<void>(RTC_FROM_HERE,
@@ -61,7 +66,7 @@ bool RTCPeerConnectionFactoryImpl::Initialize() {
6166

6267
if (!rtc_peerconnection_factory_) {
6368
rtc_peerconnection_factory_ = webrtc::CreatePeerConnectionFactory(
64-
network_thread_, worker_thread_, signaling_thread_,
69+
network_thread_.get(), worker_thread_.get(), signaling_thread_.get(),
6570
audio_device_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
6671
webrtc::CreateBuiltinAudioDecoderFactory(),
6772
#if defined(USE_INTEL_MEDIA_SDK)
@@ -138,16 +143,16 @@ scoped_refptr<RTCAudioDevice> RTCPeerConnectionFactoryImpl::GetAudioDevice() {
138143
if (!audio_device_impl_)
139144
audio_device_impl_ =
140145
scoped_refptr<AudioDeviceImpl>(new RefCountedObject<AudioDeviceImpl>(
141-
audio_device_module_, worker_thread_));
146+
audio_device_module_, worker_thread_.get()));
142147

143148
return audio_device_impl_;
144149
}
145150

146151
scoped_refptr<RTCVideoDevice> RTCPeerConnectionFactoryImpl::GetVideoDevice() {
147152
if (!video_device_impl_)
148153
video_device_impl_ = scoped_refptr<RTCVideoDeviceImpl>(
149-
new RefCountedObject<RTCVideoDeviceImpl>(signaling_thread_,
150-
worker_thread_));
154+
new RefCountedObject<RTCVideoDeviceImpl>(signaling_thread_.get(),
155+
worker_thread_.get()));
151156

152157
return video_device_impl_;
153158
}
@@ -167,7 +172,7 @@ scoped_refptr<RTCDesktopDevice>
167172
RTCPeerConnectionFactoryImpl::GetDesktopDevice() {
168173
if (!desktop_device_impl_) {
169174
desktop_device_impl_ = scoped_refptr<RTCDesktopDeviceImpl>(
170-
new RefCountedObject<RTCDesktopDeviceImpl>(signaling_thread_));
175+
new RefCountedObject<RTCDesktopDeviceImpl>(signaling_thread_.get()));
171176
}
172177
return desktop_device_impl_;
173178
}
@@ -177,7 +182,7 @@ scoped_refptr<RTCVideoSource> RTCPeerConnectionFactoryImpl::CreateVideoSource(
177182
scoped_refptr<RTCVideoCapturer> capturer,
178183
const string video_source_label,
179184
scoped_refptr<RTCMediaConstraints> constraints) {
180-
if (rtc::Thread::Current() != signaling_thread_) {
185+
if (rtc::Thread::Current() != signaling_thread_.get()) {
181186
scoped_refptr<RTCVideoSource> source =
182187
signaling_thread_->Invoke<scoped_refptr<RTCVideoSource>>(
183188
RTC_FROM_HERE, [this, capturer, video_source_label, constraints] {
@@ -214,7 +219,7 @@ scoped_refptr<RTCVideoSource> RTCPeerConnectionFactoryImpl::CreateDesktopSource(
214219
scoped_refptr<RTCDesktopCapturer> capturer,
215220
const string video_source_label,
216221
scoped_refptr<RTCMediaConstraints> constraints) {
217-
if (rtc::Thread::Current() != signaling_thread_) {
222+
if (rtc::Thread::Current() != signaling_thread_.get()) {
218223
scoped_refptr<RTCVideoSource> source =
219224
signaling_thread_->Invoke<scoped_refptr<RTCVideoSource>>(
220225
RTC_FROM_HERE, [this, capturer, video_source_label, constraints] {
@@ -300,7 +305,7 @@ scoped_refptr<RTCAudioTrack> RTCPeerConnectionFactoryImpl::CreateAudioTrack(
300305
scoped_refptr<RTCRtpCapabilities>
301306
RTCPeerConnectionFactoryImpl::GetRtpSenderCapabilities(
302307
RTCMediaType media_type) {
303-
if (rtc::Thread::Current() != signaling_thread_) {
308+
if (rtc::Thread::Current() != signaling_thread_.get()) {
304309
scoped_refptr<RTCRtpCapabilities> capabilities =
305310
signaling_thread_->Invoke<scoped_refptr<RTCRtpCapabilities>>(
306311
RTC_FROM_HERE, [this, media_type] {
@@ -329,7 +334,7 @@ RTCPeerConnectionFactoryImpl::GetRtpSenderCapabilities(
329334
scoped_refptr<RTCRtpCapabilities>
330335
RTCPeerConnectionFactoryImpl::GetRtpReceiverCapabilities(
331336
RTCMediaType media_type) {
332-
if (rtc::Thread::Current() != signaling_thread_) {
337+
if (rtc::Thread::Current() != signaling_thread_.get()) {
333338
scoped_refptr<RTCRtpCapabilities> capabilities =
334339
signaling_thread_->Invoke<scoped_refptr<RTCRtpCapabilities>>(
335340
RTC_FROM_HERE, [this, media_type] {

src/rtc_peerconnection_factory_impl.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ namespace libwebrtc {
2222

2323
class RTCPeerConnectionFactoryImpl : public RTCPeerConnectionFactory {
2424
public:
25-
RTCPeerConnectionFactoryImpl(rtc::Thread* worker_thread,
26-
rtc::Thread* signaling_thread,
27-
rtc::Thread* network_thread);
25+
RTCPeerConnectionFactoryImpl();
2826

2927
virtual ~RTCPeerConnectionFactoryImpl();
3028

@@ -94,9 +92,9 @@ class RTCPeerConnectionFactoryImpl : public RTCPeerConnectionFactory {
9492
scoped_refptr<RTCMediaConstraints> constraints);
9593
#endif
9694
private:
97-
rtc::Thread* worker_thread_ = nullptr;
98-
rtc::Thread* signaling_thread_ = nullptr;
99-
rtc::Thread* network_thread_ = nullptr;
95+
std::unique_ptr<rtc::Thread> worker_thread_;
96+
std::unique_ptr<rtc::Thread> signaling_thread_;
97+
std::unique_ptr<rtc::Thread> network_thread_;
10098
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
10199
rtc_peerconnection_factory_;
102100
rtc::scoped_refptr<webrtc::AudioDeviceModule> audio_device_module_;

0 commit comments

Comments
 (0)