Skip to content

Commit f5d5b27

Browse files
tyler-albertcopybara-github
authored andcommitted
feat: Create worker thread for PeerConnection and make it owned by the MediaApiClient
PiperOrigin-RevId: 725871175
1 parent 3fc0535 commit f5d5b27

File tree

5 files changed

+80
-50
lines changed

5 files changed

+80
-50
lines changed

native_with_state/internal/media_api_client.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ class MediaApiClient : public MediaApiClientInterface {
5353
};
5454

5555
MediaApiClient(std::unique_ptr<rtc::Thread> client_thread,
56+
std::unique_ptr<rtc::Thread> worker_thread,
5657
rtc::scoped_refptr<MediaApiClientObserverInterface> observer,
5758
std::unique_ptr<ConferencePeerConnectionInterface>
5859
conference_peer_connection,
5960
ConferenceDataChannels data_channels)
6061
: client_thread_(std::move(client_thread)),
62+
worker_thread_(std::move(worker_thread)),
6163
observer_(std::move(observer)),
6264
conference_peer_connection_(std::move(conference_peer_connection)),
6365
data_channels_(std::move(data_channels)) {
@@ -159,6 +161,11 @@ class MediaApiClient : public MediaApiClientInterface {
159161

160162
// Internal thread for client initiated asynchronous behavior.
161163
std::unique_ptr<rtc::Thread> client_thread_;
164+
// The worker thread used by WebRTC objects and the MediaApiAudioDeviceModule.
165+
//
166+
// Since the thread must outlive all of these objects, the client owns the
167+
// thread.
168+
std::unique_ptr<rtc::Thread> worker_thread_;
162169
// Safety flag for ensuring that tasks posted to the client thread are
163170
// cancelled when the client is destroyed.
164171
rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> alive_flag_;

native_with_state/internal/media_api_client_factory.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,11 @@ absl::StatusOr<MediaApiClient::ConferenceDataChannels> CreateDataChannels(
200200
} // namespace
201201

202202
MediaApiClientFactory::MediaApiClientFactory() {
203-
peer_connection_factory_provider_ = [](rtc::Thread* signaling_thread)
203+
peer_connection_factory_provider_ = [](rtc::Thread* signaling_thread,
204+
rtc::Thread* worker_thread)
204205
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
205206
return webrtc::CreatePeerConnectionFactory(
206-
/*network_thread=*/nullptr, /*worker_thread=*/nullptr, signaling_thread,
207+
/*network_thread=*/nullptr, worker_thread, signaling_thread,
207208
rtc::make_ref_counted<MediaApiAudioDeviceModule>(),
208209
webrtc::CreateBuiltinAudioEncoderFactory(),
209210
webrtc::CreateOpusAudioDecoderFactory(),
@@ -237,10 +238,15 @@ MediaApiClientFactory::CreateMediaApiClient(
237238
if (!signaling_thread->Start()) {
238239
return absl::InternalError("Failed to start signaling thread");
239240
}
241+
std::unique_ptr<rtc::Thread> worker_thread = rtc::Thread::Create();
242+
worker_thread->SetName("media_api_client_worker_thread", nullptr);
243+
if (!worker_thread->Start()) {
244+
return absl::InternalError("Failed to start worker thread");
245+
}
240246

241247
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
242-
peer_connection_factory =
243-
peer_connection_factory_provider_(signaling_thread.get());
248+
peer_connection_factory = peer_connection_factory_provider_(
249+
signaling_thread.get(), worker_thread.get());
244250

245251
auto curl_connector =
246252
std::make_unique<CurlConnector>(std::make_unique<CurlApiWrapper>());
@@ -275,7 +281,7 @@ MediaApiClientFactory::CreateMediaApiClient(
275281
conference_peer_connection->SetPeerConnection(std::move(peer_connection));
276282

277283
return std::make_unique<MediaApiClient>(
278-
std::move(client_thread), std::move(observer),
284+
std::move(client_thread), std::move(worker_thread), std::move(observer),
279285
std::move(conference_peer_connection),
280286
std::move(conference_data_channels).value());
281287
}

native_with_state/internal/media_api_client_factory.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ namespace meet {
3131

3232
class MediaApiClientFactory : public MediaApiClientFactoryInterface {
3333
public:
34-
using PeerConnectionFactoryProvider = absl::AnyInvocable<rtc::scoped_refptr<
35-
webrtc::PeerConnectionFactoryInterface>(rtc::Thread* signaling_thread)>;
34+
using PeerConnectionFactoryProvider = absl::AnyInvocable<
35+
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>(
36+
rtc::Thread* signaling_thread, rtc::Thread* worker_thread)>;
3637

3738
// Default constructor that builds clients with real dependencies.
3839
MediaApiClientFactory();

native_with_state/internal/media_api_client_factory_test.cc

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ TEST(MediaApiClientFactoryTest,
9393
Return(static_cast<rtc::scoped_refptr<webrtc::DataChannelInterface>>(
9494
webrtc::MockDataChannelInterface::Create())));
9595
MediaApiClientFactory::PeerConnectionFactoryProvider
96-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
96+
peer_connection_factory_provider =
97+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
9798
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
9899
return peer_connection_factory;
99100
};
@@ -135,7 +136,8 @@ TEST(MediaApiClientFactoryTest, FailsIfPeerConnectionFactoryFailsToCreate) {
135136
.WillOnce(Return(webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR,
136137
"test error")));
137138
MediaApiClientFactory::PeerConnectionFactoryProvider
138-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
139+
peer_connection_factory_provider =
140+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
139141
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
140142
return peer_connection_factory;
141143
};
@@ -172,7 +174,8 @@ TEST(MediaApiClientFactoryTest, FailsIfAudioTransceiverFailsToBeCreated) {
172174
"test error");
173175
});
174176
MediaApiClientFactory::PeerConnectionFactoryProvider
175-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
177+
peer_connection_factory_provider =
178+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
176179
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
177180
return peer_connection_factory;
178181
};
@@ -222,7 +225,8 @@ TEST(MediaApiClientFactoryTest, FailsIfVideoTransceiverFailsToBeCreated) {
222225
webrtc::MockDataChannelInterface::Create());
223226
});
224227
MediaApiClientFactory::PeerConnectionFactoryProvider
225-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
228+
peer_connection_factory_provider =
229+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
226230
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
227231
return peer_connection_factory;
228232
};
@@ -272,7 +276,8 @@ TEST(MediaApiClientFactoryTest,
272276
.WillOnce(Return(webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR,
273277
"test error")));
274278
MediaApiClientFactory::PeerConnectionFactoryProvider
275-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
279+
peer_connection_factory_provider =
280+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
276281
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
277282
return peer_connection_factory;
278283
};
@@ -326,7 +331,8 @@ TEST(MediaApiClientFactoryTest, FailsIfMediaStatsDataChannelFailsToBeCreated) {
326331
.WillOnce(Return(webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR,
327332
"test error")));
328333
MediaApiClientFactory::PeerConnectionFactoryProvider
329-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
334+
peer_connection_factory_provider =
335+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
330336
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
331337
return peer_connection_factory;
332338
};
@@ -385,7 +391,8 @@ TEST(MediaApiClientFactoryTest,
385391
.WillOnce(Return(webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR,
386392
"test error")));
387393
MediaApiClientFactory::PeerConnectionFactoryProvider
388-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
394+
peer_connection_factory_provider =
395+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
389396
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
390397
return peer_connection_factory;
391398
};
@@ -448,7 +455,8 @@ TEST(MediaApiClientFactoryTest,
448455
.WillOnce(Return(webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR,
449456
"test error")));
450457
MediaApiClientFactory::PeerConnectionFactoryProvider
451-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
458+
peer_connection_factory_provider =
459+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
452460
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
453461
return peer_connection_factory;
454462
};
@@ -515,7 +523,8 @@ TEST(MediaApiClientFactoryTest,
515523
.WillOnce(Return(webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR,
516524
"test error")));
517525
MediaApiClientFactory::PeerConnectionFactoryProvider
518-
peer_connection_factory_provider = [&](rtc::Thread* signaling_thread)
526+
peer_connection_factory_provider =
527+
[&](rtc::Thread* signaling_thread, rtc::Thread* worker_thread)
519528
-> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> {
520529
return peer_connection_factory;
521530
};

0 commit comments

Comments
 (0)