Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 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 | |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 17 | #include "api/call/call_factory_interface.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 18 | #include "api/media_stream_interface.h" |
| 19 | #include "api/peer_connection_interface.h" |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 20 | #include "api/ref_counted_base.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 21 | #include "api/scoped_refptr.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 22 | #include "api/sequence_checker.h" |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 23 | #include "api/transport/sctp_transport_factory_interface.h" |
| 24 | #include "api/transport/webrtc_key_value_config.h" |
| 25 | #include "media/base/media_engine.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 26 | #include "p2p/base/basic_packet_socket_factory.h" |
| 27 | #include "pc/channel_manager.h" |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 28 | #include "rtc_base/checks.h" |
| 29 | #include "rtc_base/network.h" |
| 30 | #include "rtc_base/network_monitor_factory.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 31 | #include "rtc_base/rtc_certificate_generator.h" |
| 32 | #include "rtc_base/thread.h" |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 33 | #include "rtc_base/thread_annotations.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 34 | |
| 35 | namespace rtc { |
| 36 | class BasicNetworkManager; |
| 37 | class BasicPacketSocketFactory; |
| 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. |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 48 | // |
| 49 | // This class must be created and destroyed on the signaling thread. |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 50 | class ConnectionContext final |
| 51 | : public rtc::RefCountedNonVirtual<ConnectionContext> { |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 52 | public: |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 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 | |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 59 | // This class is not copyable or movable. |
| 60 | ConnectionContext(const ConnectionContext&) = delete; |
| 61 | ConnectionContext& operator=(const ConnectionContext&) = delete; |
| 62 | |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 63 | // Functions called from PeerConnection and friends |
| 64 | SctpTransportFactoryInterface* sctp_transport_factory() const { |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 65 | return sctp_factory_.get(); |
| 66 | } |
| 67 | |
| 68 | cricket::ChannelManager* channel_manager() const; |
| 69 | |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 70 | rtc::Thread* signaling_thread() { return signaling_thread_; } |
| 71 | const rtc::Thread* signaling_thread() const { return signaling_thread_; } |
| 72 | rtc::Thread* worker_thread() { return worker_thread_; } |
| 73 | const rtc::Thread* worker_thread() const { return worker_thread_; } |
| 74 | rtc::Thread* network_thread() { return network_thread_; } |
| 75 | const rtc::Thread* network_thread() const { return network_thread_; } |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 76 | |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 77 | const WebRtcKeyValueConfig& trials() const { return *trials_.get(); } |
| 78 | |
| 79 | // Accessors only used from the PeerConnectionFactory class |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 80 | rtc::BasicNetworkManager* default_network_manager() { |
| 81 | RTC_DCHECK_RUN_ON(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 82 | return default_network_manager_.get(); |
| 83 | } |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 84 | rtc::BasicPacketSocketFactory* default_socket_factory() { |
| 85 | RTC_DCHECK_RUN_ON(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 86 | return default_socket_factory_.get(); |
| 87 | } |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 88 | CallFactoryInterface* call_factory() { |
| 89 | RTC_DCHECK_RUN_ON(worker_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 90 | return call_factory_.get(); |
| 91 | } |
| 92 | |
| 93 | protected: |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 94 | explicit ConnectionContext(PeerConnectionFactoryDependencies* dependencies); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 95 | |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 96 | friend class rtc::RefCountedNonVirtual<ConnectionContext>; |
| 97 | ~ConnectionContext(); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 98 | |
| 99 | private: |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 100 | // The following three variables are used to communicate between the |
| 101 | // constructor and the destructor, and are never exposed externally. |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 102 | bool wraps_current_thread_; |
| 103 | // Note: Since owned_network_thread_ and owned_worker_thread_ are used |
| 104 | // in the initialization of network_thread_ and worker_thread_, they |
| 105 | // must be declared before them, so that they are initialized first. |
Niels Möller | b02e1ac | 2022-02-04 13:29:50 | [diff] [blame^] | 106 | std::unique_ptr<rtc::SocketFactory> owned_socket_factory_; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 107 | std::unique_ptr<rtc::Thread> owned_network_thread_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 108 | RTC_GUARDED_BY(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 109 | std::unique_ptr<rtc::Thread> owned_worker_thread_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 110 | RTC_GUARDED_BY(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 111 | rtc::Thread* const network_thread_; |
| 112 | rtc::Thread* const worker_thread_; |
| 113 | rtc::Thread* const signaling_thread_; |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 114 | // channel_manager is accessed both on signaling thread and worker thread. |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 115 | std::unique_ptr<cricket::ChannelManager> channel_manager_; |
| 116 | std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 117 | RTC_GUARDED_BY(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 118 | std::unique_ptr<rtc::BasicNetworkManager> default_network_manager_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 119 | RTC_GUARDED_BY(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 120 | std::unique_ptr<webrtc::CallFactoryInterface> const call_factory_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 121 | RTC_GUARDED_BY(worker_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 122 | |
| 123 | std::unique_ptr<rtc::BasicPacketSocketFactory> default_socket_factory_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 124 | RTC_GUARDED_BY(signaling_thread_); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 125 | std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 126 | // Accessed both on signaling thread and worker thread. |
| 127 | std::unique_ptr<WebRtcKeyValueConfig> const trials_; |
| 128 | }; |
| 129 | |
| 130 | } // namespace webrtc |
| 131 | |
| 132 | #endif // PC_CONNECTION_CONTEXT_H_ |