Create PeerConnectionFactoryDependencies to prevent new function overloads.
To address this, this CL introduces a PeerConnectionFactoryDependencies
structure to encapsulate all mandatory and optional dependencies (where a
dependency is defined as non trivial executable code that an API user may want
to provide to the native API). This allows adding a new injectable dependency
by simply adding a new field to the struct, avoiding the hassle described above.
Bug: webrtc:7913
Change-Id: Ice58fa72e8c578b250084a1629499fabda66dabf
Reviewed-on: https://webrtc-review.googlesource.com/79720
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Benjamin Wright <benwright@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23480}
diff --git a/api/peerconnectioninterface.h b/api/peerconnectioninterface.h
index fa94093..5397969 100644
--- a/api/peerconnectioninterface.h
+++ b/api/peerconnectioninterface.h
@@ -1205,6 +1205,36 @@
std::unique_ptr<rtc::SSLCertificateVerifier> tls_cert_verifier;
};
+// PeerConnectionFactoryDependencies holds all of the PeerConnectionFactory
+// dependencies. All new dependencies should be added here instead of
+// overloading the function. This simplifies dependency injection and makes it
+// clear which are mandatory and optional. If possible please allow the peer
+// connection factory to take ownership of the dependency by adding a unique_ptr
+// to this structure.
+struct PeerConnectionFactoryDependencies final {
+ PeerConnectionFactoryDependencies() = default;
+ // This object is not copyable or assignable.
+ PeerConnectionFactoryDependencies(const PeerConnectionFactoryDependencies&) =
+ delete;
+ PeerConnectionFactoryDependencies& operator=(
+ const PeerConnectionFactoryDependencies&) = delete;
+ // This object is only moveable.
+ PeerConnectionFactoryDependencies(PeerConnectionFactoryDependencies&&) =
+ default;
+ PeerConnectionFactoryDependencies& operator=(
+ PeerConnectionFactoryDependencies&&) = default;
+
+ // Optional dependencies
+ rtc::Thread* network_thread = nullptr;
+ rtc::Thread* worker_thread = nullptr;
+ rtc::Thread* signaling_thread = nullptr;
+ std::unique_ptr<cricket::MediaEngineInterface> media_engine;
+ std::unique_ptr<CallFactoryInterface> call_factory;
+ std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory;
+ std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory;
+ std::unique_ptr<NetworkControllerFactoryInterface> network_controller_factory;
+};
+
// PeerConnectionFactoryInterface is the factory interface used for creating
// PeerConnection, MediaStream and MediaStreamTrack objects.
//
@@ -1555,6 +1585,10 @@
std::unique_ptr<NetworkControllerFactoryInterface>
network_controller_factory = nullptr);
+rtc::scoped_refptr<PeerConnectionFactoryInterface>
+CreateModularPeerConnectionFactory(
+ PeerConnectionFactoryDependencies dependencies);
+
} // namespace webrtc
#endif // API_PEERCONNECTIONINTERFACE_H_
diff --git a/pc/peerconnectionfactory.cc b/pc/peerconnectionfactory.cc
index 0fc6af2..c9fa223 100644
--- a/pc/peerconnectionfactory.cc
+++ b/pc/peerconnectionfactory.cc
@@ -55,9 +55,14 @@
std::unique_ptr<cricket::MediaEngineInterface> media_engine,
std::unique_ptr<CallFactoryInterface> call_factory,
std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory) {
- return CreateModularPeerConnectionFactory(
- network_thread, worker_thread, signaling_thread, std::move(media_engine),
- std::move(call_factory), std::move(event_log_factory), nullptr, nullptr);
+ PeerConnectionFactoryDependencies dependencies;
+ dependencies.network_thread = network_thread;
+ dependencies.worker_thread = worker_thread;
+ dependencies.signaling_thread = signaling_thread;
+ dependencies.media_engine = std::move(media_engine);
+ dependencies.call_factory = std::move(call_factory);
+ dependencies.event_log_factory = std::move(event_log_factory);
+ return CreateModularPeerConnectionFactory(std::move(dependencies));
}
rtc::scoped_refptr<PeerConnectionFactoryInterface>
@@ -71,13 +76,25 @@
std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory,
std::unique_ptr<NetworkControllerFactoryInterface>
network_controller_factory) {
+ PeerConnectionFactoryDependencies dependencies;
+ dependencies.network_thread = network_thread;
+ dependencies.worker_thread = worker_thread;
+ dependencies.signaling_thread = signaling_thread;
+ dependencies.media_engine = std::move(media_engine);
+ dependencies.call_factory = std::move(call_factory);
+ dependencies.event_log_factory = std::move(event_log_factory);
+ dependencies.fec_controller_factory = std::move(fec_controller_factory);
+ dependencies.network_controller_factory =
+ std::move(network_controller_factory);
+ return CreateModularPeerConnectionFactory(std::move(dependencies));
+}
+
+rtc::scoped_refptr<PeerConnectionFactoryInterface>
+CreateModularPeerConnectionFactory(
+ PeerConnectionFactoryDependencies dependencies) {
rtc::scoped_refptr<PeerConnectionFactory> pc_factory(
new rtc::RefCountedObject<PeerConnectionFactory>(
- network_thread, worker_thread, signaling_thread,
- std::move(media_engine), std::move(call_factory),
- std::move(event_log_factory), std::move(fec_controller_factory),
- std::move(network_controller_factory)));
-
+ std::move(dependencies)));
// Call Initialize synchronously but make sure it is executed on
// |signaling_thread|.
MethodCall0<PeerConnectionFactory, bool> call(
@@ -158,6 +175,18 @@
// RTC_DCHECK(default_adm != NULL);
}
+PeerConnectionFactory::PeerConnectionFactory(
+ PeerConnectionFactoryDependencies dependencies)
+ : PeerConnectionFactory(
+ dependencies.network_thread,
+ dependencies.worker_thread,
+ dependencies.signaling_thread,
+ std::move(dependencies.media_engine),
+ std::move(dependencies.call_factory),
+ std::move(dependencies.event_log_factory),
+ std::move(dependencies.fec_controller_factory),
+ std::move(dependencies.network_controller_factory)) {}
+
PeerConnectionFactory::~PeerConnectionFactory() {
RTC_DCHECK(signaling_thread_->IsCurrent());
channel_manager_.reset(nullptr);
diff --git a/pc/peerconnectionfactory.h b/pc/peerconnectionfactory.h
index b566dd3..bce1e4c 100644
--- a/pc/peerconnectionfactory.h
+++ b/pc/peerconnectionfactory.h
@@ -119,6 +119,12 @@
std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory,
std::unique_ptr<NetworkControllerFactoryInterface>
network_controller_factory);
+ // Use this implementation for all future use. This structure allows simple
+ // management of all new dependencies being added to the
+ // PeerConnectionFactory.
+ explicit PeerConnectionFactory(
+ PeerConnectionFactoryDependencies dependencies);
+
virtual ~PeerConnectionFactory();
private: