| 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/environment/environment.h" |
| 18 | #include "api/media_stream_interface.h" |
| 19 | #include "api/peer_connection_interface.h" |
| 20 | #include "api/ref_counted_base.h" |
| 21 | #include "api/scoped_refptr.h" |
| 22 | #include "api/sequence_checker.h" |
| 23 | #include "api/transport/sctp_transport_factory_interface.h" |
| 24 | #include "media/base/media_engine.h" |
| 25 | #include "p2p/base/basic_packet_socket_factory.h" |
| 26 | #include "rtc_base/checks.h" |
| 27 | #include "rtc_base/network.h" |
| 28 | #include "rtc_base/network_monitor_factory.h" |
| 29 | #include "rtc_base/rtc_certificate_generator.h" |
| 30 | #include "rtc_base/socket_factory.h" |
| 31 | #include "rtc_base/thread.h" |
| 32 | #include "rtc_base/thread_annotations.h" |
| 33 |
| 34 | namespace rtc { |
| 35 | class BasicPacketSocketFactory; |
| 36 | class UniqueRandomIdGenerator; |
| 37 | } // namespace rtc |
| 38 |
| 39 | namespace webrtc { |
| 40 |
| 41 | // This class contains resources needed by PeerConnection and associated |
| 42 | // objects. A reference to this object is passed to each PeerConnection. The |
| 43 | // methods on this object are assumed not to change the state in any way that |
| 44 | // interferes with the operation of other PeerConnections. |
| 45 | // |
| 46 | // This class must be created and destroyed on the signaling thread. |
| 47 | class ConnectionContext final |
| 48 | : public rtc::RefCountedNonVirtual<ConnectionContext> { |
| 49 | public: |
| 50 | // Creates a ConnectionContext. May return null if initialization fails. |
| 51 | // The Dependencies class allows simple management of all new dependencies |
| 52 | // being added to the ConnectionContext. |
| 53 | static rtc::scoped_refptr<ConnectionContext> Create( |
| 54 | const Environment& env, |
| 55 | PeerConnectionFactoryDependencies* dependencies); |
| 56 |
| 57 | // This class is not copyable or movable. |
| 58 | ConnectionContext(const ConnectionContext&) = delete; |
| 59 | ConnectionContext& operator=(const ConnectionContext&) = delete; |
| 60 |
| 61 | // Functions called from PeerConnection and friends |
| 62 | SctpTransportFactoryInterface* sctp_transport_factory() const { |
| 63 | return sctp_factory_.get(); |
| 64 | } |
| 65 |
| 66 | cricket::MediaEngineInterface* media_engine() const { |
| 67 | return media_engine_.get(); |
| 68 | } |
| 69 |
| 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_.get(); } |
| 73 | const rtc::Thread* worker_thread() const { return worker_thread_.get(); } |
| 74 | rtc::Thread* network_thread() { return network_thread_; } |
| 75 | const rtc::Thread* network_thread() const { return network_thread_; } |
| 76 |
| 77 | // Environment associated with the PeerConnectionFactory. |
| 78 | // Note: environments are different for different PeerConnections, |
| 79 | // but they are not supposed to change after creating the PeerConnection. |
| 80 | const Environment& env() const { return env_; } |
| 81 |
| 82 | // Accessors only used from the PeerConnectionFactory class |
| 83 | rtc::NetworkManager* default_network_manager() { |
| 84 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 85 | return default_network_manager_.get(); |
| 86 | } |
| 87 | rtc::PacketSocketFactory* default_socket_factory() { |
| 88 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 89 | return default_socket_factory_.get(); |
| 90 | } |
| 91 | MediaFactory* call_factory() { |
| 92 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 93 | return call_factory_.get(); |
| 94 | } |
| 95 | rtc::UniqueRandomIdGenerator* ssrc_generator() { return &ssrc_generator_; } |
| 96 | // Note: There is lots of code that wants to know whether or not we |
| 97 | // use RTX, but so far, no code has been found that sets it to false. |
| 98 | // Kept in the API in order to ease introduction if we want to resurrect |
| 99 | // the functionality. |
| 100 | bool use_rtx() { return use_rtx_; } |
| 101 |
| 102 | // For use by tests. |
| 103 | void set_use_rtx(bool use_rtx) { use_rtx_ = use_rtx; } |
| 104 |
| 105 | protected: |
| 106 | ConnectionContext(const Environment& env, |
| 107 | PeerConnectionFactoryDependencies* dependencies); |
| 108 |
| 109 | friend class rtc::RefCountedNonVirtual<ConnectionContext>; |
| 110 | ~ConnectionContext(); |
| 111 |
| 112 | private: |
| 113 | // The following three variables are used to communicate between the |
| 114 | // constructor and the destructor, and are never exposed externally. |
| 115 | bool wraps_current_thread_; |
| 116 | std::unique_ptr<rtc::SocketFactory> owned_socket_factory_; |
| 117 | std::unique_ptr<rtc::Thread> owned_network_thread_ |
| 118 | RTC_GUARDED_BY(signaling_thread_); |
| 119 | rtc::Thread* const network_thread_; |
| 120 | AlwaysValidPointer<rtc::Thread> const worker_thread_; |
| 121 | rtc::Thread* const signaling_thread_; |
| 122 |
| 123 | const Environment env_; |
| 124 |
| 125 | // This object is const over the lifetime of the ConnectionContext, and is |
| 126 | // only altered in the destructor. |
| 127 | std::unique_ptr<cricket::MediaEngineInterface> media_engine_; |
| 128 |
| 129 | // This object should be used to generate any SSRC that is not explicitly |
| 130 | // specified by the user (or by the remote party). |
| 131 | // TODO(bugs.webrtc.org/12666): This variable is used from both the signaling |
| 132 | // and worker threads. See if we can't restrict usage to a single thread. |
| 133 | rtc::UniqueRandomIdGenerator ssrc_generator_; |
| 134 | std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_ |
| 135 | RTC_GUARDED_BY(signaling_thread_); |
| 136 | std::unique_ptr<rtc::NetworkManager> default_network_manager_ |
| 137 | RTC_GUARDED_BY(signaling_thread_); |
| 138 | std::unique_ptr<MediaFactory> const call_factory_ |
| 139 | RTC_GUARDED_BY(worker_thread()); |
| 140 |
| 141 | std::unique_ptr<rtc::PacketSocketFactory> default_socket_factory_ |
| 142 | RTC_GUARDED_BY(signaling_thread_); |
| 143 | std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_; |
| 144 |
| 145 | // Controls whether to announce support for the the rfc4588 payload format |
| 146 | // for retransmitted video packets. |
| 147 | bool use_rtx_; |
| 148 | }; |
| 149 |
| 150 | } // namespace webrtc |
| 151 |
| 152 | #endif // PC_CONNECTION_CONTEXT_H_ |
| 153 |