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" |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 18 | #include "api/field_trials_view.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 19 | #include "api/media_stream_interface.h" |
| 20 | #include "api/peer_connection_interface.h" |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 21 | #include "api/ref_counted_base.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 22 | #include "api/scoped_refptr.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 23 | #include "api/sequence_checker.h" |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 24 | #include "api/transport/sctp_transport_factory_interface.h" |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 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" |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
| 28 | #include "rtc_base/network.h" |
| 29 | #include "rtc_base/network_monitor_factory.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 30 | #include "rtc_base/rtc_certificate_generator.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 31 | #include "rtc_base/socket_factory.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 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 { |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 36 | class BasicPacketSocketFactory; |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 37 | class UniqueRandomIdGenerator; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 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 | |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 68 | cricket::MediaEngineInterface* media_engine() const { |
| 69 | return media_engine_.get(); |
| 70 | } |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 71 | |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 72 | rtc::Thread* signaling_thread() { return signaling_thread_; } |
| 73 | const rtc::Thread* signaling_thread() const { return signaling_thread_; } |
Harald Alvestrand | 00579e8 | 2022-05-03 11:37:34 | [diff] [blame] | 74 | rtc::Thread* worker_thread() { return worker_thread_.get(); } |
| 75 | const rtc::Thread* worker_thread() const { return worker_thread_.get(); } |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 76 | rtc::Thread* network_thread() { return network_thread_; } |
| 77 | const rtc::Thread* network_thread() const { return network_thread_; } |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 78 | |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 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). |
| 83 | const FieldTrialsView& field_trials() const { return *trials_.get(); } |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 84 | |
| 85 | // Accessors only used from the PeerConnectionFactory class |
Niels Möller | dcb5a58 | 2022-06-20 13:33:59 | [diff] [blame] | 86 | rtc::NetworkManager* default_network_manager() { |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 87 | RTC_DCHECK_RUN_ON(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 88 | return default_network_manager_.get(); |
| 89 | } |
Niels Möller | 573b145 | 2022-06-21 09:37:29 | [diff] [blame] | 90 | rtc::PacketSocketFactory* default_socket_factory() { |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 91 | RTC_DCHECK_RUN_ON(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 92 | return default_socket_factory_.get(); |
| 93 | } |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 94 | CallFactoryInterface* call_factory() { |
Harald Alvestrand | 00579e8 | 2022-05-03 11:37:34 | [diff] [blame] | 95 | RTC_DCHECK_RUN_ON(worker_thread()); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 96 | return call_factory_.get(); |
| 97 | } |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 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. |
Joachim Reiersen | 45a985c | 2023-08-24 17:00:01 | [diff] [blame] | 103 | bool use_rtx() { return use_rtx_; } |
| 104 | |
| 105 | // For use by tests. |
| 106 | void set_use_rtx(bool use_rtx) { use_rtx_ = use_rtx; } |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 107 | |
| 108 | protected: |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 | [diff] [blame] | 109 | explicit ConnectionContext(PeerConnectionFactoryDependencies* dependencies); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 110 | |
Tommi | 86ee89f | 2021-04-20 14:58:01 | [diff] [blame] | 111 | friend class rtc::RefCountedNonVirtual<ConnectionContext>; |
| 112 | ~ConnectionContext(); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 113 | |
| 114 | private: |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 115 | // The following three variables are used to communicate between the |
| 116 | // constructor and the destructor, and are never exposed externally. |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 117 | bool wraps_current_thread_; |
Niels Möller | b02e1ac | 2022-02-04 13:29:50 | [diff] [blame] | 118 | std::unique_ptr<rtc::SocketFactory> owned_socket_factory_; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 119 | std::unique_ptr<rtc::Thread> owned_network_thread_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 120 | RTC_GUARDED_BY(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 121 | rtc::Thread* const network_thread_; |
Harald Alvestrand | 00579e8 | 2022-05-03 11:37:34 | [diff] [blame] | 122 | AlwaysValidPointer<rtc::Thread> const worker_thread_; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 123 | rtc::Thread* const signaling_thread_; |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 124 | |
| 125 | // Accessed both on signaling thread and worker thread. |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 126 | std::unique_ptr<FieldTrialsView> const trials_; |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 127 | |
Peter Hanspers | 3e1e831 | 2023-09-05 15:55:34 | [diff] [blame] | 128 | // This object is const over the lifetime of the ConnectionContext, and is |
| 129 | // only altered in the destructor. |
| 130 | std::unique_ptr<cricket::MediaEngineInterface> media_engine_; |
Harald Alvestrand | 0ac50b9 | 2022-05-18 07:51:34 | [diff] [blame] | 131 | |
| 132 | // This object should be used to generate any SSRC that is not explicitly |
| 133 | // specified by the user (or by the remote party). |
| 134 | // TODO(bugs.webrtc.org/12666): This variable is used from both the signaling |
| 135 | // and worker threads. See if we can't restrict usage to a single thread. |
| 136 | rtc::UniqueRandomIdGenerator ssrc_generator_; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 137 | std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 138 | RTC_GUARDED_BY(signaling_thread_); |
Niels Möller | dcb5a58 | 2022-06-20 13:33:59 | [diff] [blame] | 139 | std::unique_ptr<rtc::NetworkManager> default_network_manager_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 140 | RTC_GUARDED_BY(signaling_thread_); |
Harald Alvestrand | a654437 | 2023-11-13 09:33:56 | [diff] [blame] | 141 | std::unique_ptr<CallFactoryInterface> const call_factory_ |
Harald Alvestrand | 00579e8 | 2022-05-03 11:37:34 | [diff] [blame] | 142 | RTC_GUARDED_BY(worker_thread()); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 143 | |
Niels Möller | 573b145 | 2022-06-21 09:37:29 | [diff] [blame] | 144 | std::unique_ptr<rtc::PacketSocketFactory> default_socket_factory_ |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 | [diff] [blame] | 145 | RTC_GUARDED_BY(signaling_thread_); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 146 | std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_; |
Joachim Reiersen | 45a985c | 2023-08-24 17:00:01 | [diff] [blame] | 147 | |
| 148 | // Controls whether to announce support for the the rfc4588 payload format |
| 149 | // for retransmitted video packets. |
| 150 | bool use_rtx_; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | } // namespace webrtc |
| 154 | |
| 155 | #endif // PC_CONNECTION_CONTEXT_H_ |