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