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/media_stream_interface.h"
19 #include "api/peer_connection_interface.h"
20 #include "api/scoped_refptr.h"
21 #include "api/sequence_checker.h"
22 #include "api/transport/sctp_transport_factory_interface.h"
23 #include "api/transport/webrtc_key_value_config.h"
24 #include "media/base/media_engine.h"
25 #include "media/sctp/sctp_transport_internal.h"
26 #include "p2p/base/basic_packet_socket_factory.h"
27 #include "pc/channel_manager.h"
28 #include "rtc_base/checks.h"
29 #include "rtc_base/network.h"
30 #include "rtc_base/network_monitor_factory.h"
31 #include "rtc_base/ref_count.h"
32 #include "rtc_base/rtc_certificate_generator.h"
33 #include "rtc_base/thread.h"
34 #include "rtc_base/thread_annotations.h"
35 
36 namespace rtc {
37 class BasicNetworkManager;
38 class BasicPacketSocketFactory;
39 }  // namespace rtc
40 
41 namespace webrtc {
42 
43 class RtcEventLog;
44 
45 // This class contains resources needed by PeerConnection and associated
46 // objects. A reference to this object is passed to each PeerConnection. The
47 // methods on this object are assumed not to change the state in any way that
48 // interferes with the operation of other PeerConnections.
49 //
50 // This class must be created and destroyed on the signaling thread.
51 class ConnectionContext : public rtc::RefCountInterface {
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 
68   cricket::ChannelManager* channel_manager() const;
69 
signaling_thread()70   rtc::Thread* signaling_thread() { return signaling_thread_; }
signaling_thread()71   const rtc::Thread* signaling_thread() const { return signaling_thread_; }
worker_thread()72   rtc::Thread* worker_thread() { return worker_thread_; }
worker_thread()73   const rtc::Thread* worker_thread() const { return worker_thread_; }
network_thread()74   rtc::Thread* network_thread() { return network_thread_; }
network_thread()75   const rtc::Thread* network_thread() const { return network_thread_; }
76 
trials()77   const WebRtcKeyValueConfig& trials() const { return *trials_.get(); }
78 
79   // Accessors only used from the PeerConnectionFactory class
default_network_manager()80   rtc::BasicNetworkManager* default_network_manager() {
81     RTC_DCHECK_RUN_ON(signaling_thread_);
82     return default_network_manager_.get();
83   }
default_socket_factory()84   rtc::BasicPacketSocketFactory* default_socket_factory() {
85     RTC_DCHECK_RUN_ON(signaling_thread_);
86     return default_socket_factory_.get();
87   }
call_factory()88   CallFactoryInterface* call_factory() {
89     RTC_DCHECK_RUN_ON(worker_thread_);
90     return call_factory_.get();
91   }
92 
93  protected:
94   explicit ConnectionContext(PeerConnectionFactoryDependencies* dependencies);
95 
96   virtual ~ConnectionContext();
97 
98  private:
99   // The following three variables are used to communicate between the
100   // constructor and the destructor, and are never exposed externally.
101   bool wraps_current_thread_;
102   // Note: Since owned_network_thread_ and owned_worker_thread_ are used
103   // in the initialization of network_thread_ and worker_thread_, they
104   // must be declared before them, so that they are initialized first.
105   std::unique_ptr<rtc::Thread> owned_network_thread_
106       RTC_GUARDED_BY(signaling_thread_);
107   std::unique_ptr<rtc::Thread> owned_worker_thread_
108       RTC_GUARDED_BY(signaling_thread_);
109   rtc::Thread* const network_thread_;
110   rtc::Thread* const worker_thread_;
111   rtc::Thread* const signaling_thread_;
112   // channel_manager is accessed both on signaling thread and worker thread.
113   std::unique_ptr<cricket::ChannelManager> channel_manager_;
114   std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_
115       RTC_GUARDED_BY(signaling_thread_);
116   std::unique_ptr<rtc::BasicNetworkManager> default_network_manager_
117       RTC_GUARDED_BY(signaling_thread_);
118   std::unique_ptr<webrtc::CallFactoryInterface> const call_factory_
119       RTC_GUARDED_BY(worker_thread_);
120 
121   std::unique_ptr<rtc::BasicPacketSocketFactory> default_socket_factory_
122       RTC_GUARDED_BY(signaling_thread_);
123   std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_;
124   // Accessed both on signaling thread and worker thread.
125   std::unique_ptr<WebRtcKeyValueConfig> const trials_;
126 };
127 
128 }  // namespace webrtc
129 
130 #endif  // PC_CONNECTION_CONTEXT_H_
131