xref: /aosp_15_r20/external/webrtc/pc/connection_context.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2020 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef PC_CONNECTION_CONTEXT_H_
12*d9f75844SAndroid Build Coastguard Worker #define PC_CONNECTION_CONTEXT_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <string>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "api/call/call_factory_interface.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/field_trials_view.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/media_stream_interface.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/peer_connection_interface.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/ref_counted_base.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/transport/sctp_transport_factory_interface.h"
25*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_engine.h"
26*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/basic_packet_socket_factory.h"
27*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
28*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/network.h"
29*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/network_monitor_factory.h"
30*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/rtc_certificate_generator.h"
31*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/socket_factory.h"
32*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread.h"
33*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
34*d9f75844SAndroid Build Coastguard Worker 
35*d9f75844SAndroid Build Coastguard Worker namespace rtc {
36*d9f75844SAndroid Build Coastguard Worker class BasicPacketSocketFactory;
37*d9f75844SAndroid Build Coastguard Worker class UniqueRandomIdGenerator;
38*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
39*d9f75844SAndroid Build Coastguard Worker 
40*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker class RtcEventLog;
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker // This class contains resources needed by PeerConnection and associated
45*d9f75844SAndroid Build Coastguard Worker // objects. A reference to this object is passed to each PeerConnection. The
46*d9f75844SAndroid Build Coastguard Worker // methods on this object are assumed not to change the state in any way that
47*d9f75844SAndroid Build Coastguard Worker // interferes with the operation of other PeerConnections.
48*d9f75844SAndroid Build Coastguard Worker //
49*d9f75844SAndroid Build Coastguard Worker // This class must be created and destroyed on the signaling thread.
50*d9f75844SAndroid Build Coastguard Worker class ConnectionContext final
51*d9f75844SAndroid Build Coastguard Worker     : public rtc::RefCountedNonVirtual<ConnectionContext> {
52*d9f75844SAndroid Build Coastguard Worker  public:
53*d9f75844SAndroid Build Coastguard Worker   // Creates a ConnectionContext. May return null if initialization fails.
54*d9f75844SAndroid Build Coastguard Worker   // The Dependencies class allows simple management of all new dependencies
55*d9f75844SAndroid Build Coastguard Worker   // being added to the ConnectionContext.
56*d9f75844SAndroid Build Coastguard Worker   static rtc::scoped_refptr<ConnectionContext> Create(
57*d9f75844SAndroid Build Coastguard Worker       PeerConnectionFactoryDependencies* dependencies);
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   // This class is not copyable or movable.
60*d9f75844SAndroid Build Coastguard Worker   ConnectionContext(const ConnectionContext&) = delete;
61*d9f75844SAndroid Build Coastguard Worker   ConnectionContext& operator=(const ConnectionContext&) = delete;
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker   // Functions called from PeerConnection and friends
sctp_transport_factory()64*d9f75844SAndroid Build Coastguard Worker   SctpTransportFactoryInterface* sctp_transport_factory() const {
65*d9f75844SAndroid Build Coastguard Worker     return sctp_factory_.get();
66*d9f75844SAndroid Build Coastguard Worker   }
67*d9f75844SAndroid Build Coastguard Worker 
media_engine()68*d9f75844SAndroid Build Coastguard Worker   cricket::MediaEngineInterface* media_engine() const {
69*d9f75844SAndroid Build Coastguard Worker     return media_engine_.get();
70*d9f75844SAndroid Build Coastguard Worker   }
71*d9f75844SAndroid Build Coastguard Worker 
signaling_thread()72*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* signaling_thread() { return signaling_thread_; }
signaling_thread()73*d9f75844SAndroid Build Coastguard Worker   const rtc::Thread* signaling_thread() const { return signaling_thread_; }
worker_thread()74*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* worker_thread() { return worker_thread_.get(); }
worker_thread()75*d9f75844SAndroid Build Coastguard Worker   const rtc::Thread* worker_thread() const { return worker_thread_.get(); }
network_thread()76*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* network_thread() { return network_thread_; }
network_thread()77*d9f75844SAndroid Build Coastguard Worker   const rtc::Thread* network_thread() const { return network_thread_; }
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker   // Field trials associated with the PeerConnectionFactory.
80*d9f75844SAndroid Build Coastguard Worker   // Note: that there can be different field trials for different
81*d9f75844SAndroid Build Coastguard Worker   // PeerConnections (but they are not supposed change after creating the
82*d9f75844SAndroid Build Coastguard Worker   // PeerConnection).
field_trials()83*d9f75844SAndroid Build Coastguard Worker   const FieldTrialsView& field_trials() const { return *trials_.get(); }
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker   // Accessors only used from the PeerConnectionFactory class
default_network_manager()86*d9f75844SAndroid Build Coastguard Worker   rtc::NetworkManager* default_network_manager() {
87*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(signaling_thread_);
88*d9f75844SAndroid Build Coastguard Worker     return default_network_manager_.get();
89*d9f75844SAndroid Build Coastguard Worker   }
default_socket_factory()90*d9f75844SAndroid Build Coastguard Worker   rtc::PacketSocketFactory* default_socket_factory() {
91*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(signaling_thread_);
92*d9f75844SAndroid Build Coastguard Worker     return default_socket_factory_.get();
93*d9f75844SAndroid Build Coastguard Worker   }
call_factory()94*d9f75844SAndroid Build Coastguard Worker   CallFactoryInterface* call_factory() {
95*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(worker_thread());
96*d9f75844SAndroid Build Coastguard Worker     return call_factory_.get();
97*d9f75844SAndroid Build Coastguard Worker   }
ssrc_generator()98*d9f75844SAndroid Build Coastguard Worker   rtc::UniqueRandomIdGenerator* ssrc_generator() { return &ssrc_generator_; }
99*d9f75844SAndroid Build Coastguard Worker   // Note: There is lots of code that wants to know whether or not we
100*d9f75844SAndroid Build Coastguard Worker   // use RTX, but so far, no code has been found that sets it to false.
101*d9f75844SAndroid Build Coastguard Worker   // Kept in the API in order to ease introduction if we want to resurrect
102*d9f75844SAndroid Build Coastguard Worker   // the functionality.
use_rtx()103*d9f75844SAndroid Build Coastguard Worker   bool use_rtx() { return true; }
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker  protected:
106*d9f75844SAndroid Build Coastguard Worker   explicit ConnectionContext(PeerConnectionFactoryDependencies* dependencies);
107*d9f75844SAndroid Build Coastguard Worker 
108*d9f75844SAndroid Build Coastguard Worker   friend class rtc::RefCountedNonVirtual<ConnectionContext>;
109*d9f75844SAndroid Build Coastguard Worker   ~ConnectionContext();
110*d9f75844SAndroid Build Coastguard Worker 
111*d9f75844SAndroid Build Coastguard Worker  private:
112*d9f75844SAndroid Build Coastguard Worker   // The following three variables are used to communicate between the
113*d9f75844SAndroid Build Coastguard Worker   // constructor and the destructor, and are never exposed externally.
114*d9f75844SAndroid Build Coastguard Worker   bool wraps_current_thread_;
115*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::SocketFactory> owned_socket_factory_;
116*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::Thread> owned_network_thread_
117*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(signaling_thread_);
118*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const network_thread_;
119*d9f75844SAndroid Build Coastguard Worker   AlwaysValidPointer<rtc::Thread> const worker_thread_;
120*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const signaling_thread_;
121*d9f75844SAndroid Build Coastguard Worker 
122*d9f75844SAndroid Build Coastguard Worker   // Accessed both on signaling thread and worker thread.
123*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<FieldTrialsView> const trials_;
124*d9f75844SAndroid Build Coastguard Worker 
125*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<cricket::MediaEngineInterface> media_engine_;
126*d9f75844SAndroid Build Coastguard Worker 
127*d9f75844SAndroid Build Coastguard Worker   // This object should be used to generate any SSRC that is not explicitly
128*d9f75844SAndroid Build Coastguard Worker   // specified by the user (or by the remote party).
129*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/12666): This variable is used from both the signaling
130*d9f75844SAndroid Build Coastguard Worker   // and worker threads. See if we can't restrict usage to a single thread.
131*d9f75844SAndroid Build Coastguard Worker   rtc::UniqueRandomIdGenerator ssrc_generator_;
132*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_
133*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(signaling_thread_);
134*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::NetworkManager> default_network_manager_
135*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(signaling_thread_);
136*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<webrtc::CallFactoryInterface> const call_factory_
137*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(worker_thread());
138*d9f75844SAndroid Build Coastguard Worker 
139*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::PacketSocketFactory> default_socket_factory_
140*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(signaling_thread_);
141*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_;
142*d9f75844SAndroid Build Coastguard Worker };
143*d9f75844SAndroid Build Coastguard Worker 
144*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
145*d9f75844SAndroid Build Coastguard Worker 
146*d9f75844SAndroid Build Coastguard Worker #endif  // PC_CONNECTION_CONTEXT_H_
147