blob: 28b2d1cdd5fc4e619252d2fa6c5ba3c50fab8dfb [file] [log] [blame]
Harald Alvestranda39689c2020-10-15 08:34:311/*
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
Danil Chapovalov7eaa9dc2023-11-27 14:00:2117#include "api/environment/environment.h"
Harald Alvestranda39689c2020-10-15 08:34:3118#include "api/media_stream_interface.h"
19#include "api/peer_connection_interface.h"
Tommi86ee89f2021-04-20 14:58:0120#include "api/ref_counted_base.h"
Harald Alvestranda39689c2020-10-15 08:34:3121#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2422#include "api/sequence_checker.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3123#include "api/transport/sctp_transport_factory_interface.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3124#include "media/base/media_engine.h"
Harald Alvestranda39689c2020-10-15 08:34:3125#include "p2p/base/basic_packet_socket_factory.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3126#include "rtc_base/checks.h"
27#include "rtc_base/network.h"
28#include "rtc_base/network_monitor_factory.h"
Harald Alvestranda39689c2020-10-15 08:34:3129#include "rtc_base/rtc_certificate_generator.h"
Harald Alvestrandc24a2182022-02-23 13:44:5930#include "rtc_base/socket_factory.h"
Harald Alvestranda39689c2020-10-15 08:34:3131#include "rtc_base/thread.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3132#include "rtc_base/thread_annotations.h"
Harald Alvestranda39689c2020-10-15 08:34:3133
34namespace rtc {
Harald Alvestranda39689c2020-10-15 08:34:3135class BasicPacketSocketFactory;
Harald Alvestrandc3fa7c32022-05-22 10:57:0136class UniqueRandomIdGenerator;
Harald Alvestranda39689c2020-10-15 08:34:3137} // namespace rtc
38
39namespace webrtc {
40
Harald Alvestranda39689c2020-10-15 08:34:3141// 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.
Harald Alvestrandffd5dc72020-10-20 15:35:3145//
46// This class must be created and destroyed on the signaling thread.
Tommi86ee89f2021-04-20 14:58:0147class ConnectionContext final
48 : public rtc::RefCountedNonVirtual<ConnectionContext> {
Harald Alvestranda39689c2020-10-15 08:34:3149 public:
Harald Alvestrandffd5dc72020-10-20 15:35:3150 // 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(
Danil Chapovalov7eaa9dc2023-11-27 14:00:2154 const Environment& env,
Harald Alvestrandffd5dc72020-10-20 15:35:3155 PeerConnectionFactoryDependencies* dependencies);
56
Harald Alvestrand4244b5f2020-10-15 12:57:0557 // This class is not copyable or movable.
58 ConnectionContext(const ConnectionContext&) = delete;
59 ConnectionContext& operator=(const ConnectionContext&) = delete;
60
Harald Alvestranda39689c2020-10-15 08:34:3161 // Functions called from PeerConnection and friends
62 SctpTransportFactoryInterface* sctp_transport_factory() const {
Harald Alvestranda39689c2020-10-15 08:34:3163 return sctp_factory_.get();
64 }
65
Harald Alvestrandc3fa7c32022-05-22 10:57:0166 cricket::MediaEngineInterface* media_engine() const {
67 return media_engine_.get();
68 }
Harald Alvestranda39689c2020-10-15 08:34:3169
Harald Alvestrand4244b5f2020-10-15 12:57:0570 rtc::Thread* signaling_thread() { return signaling_thread_; }
71 const rtc::Thread* signaling_thread() const { return signaling_thread_; }
Harald Alvestrand00579e82022-05-03 11:37:3472 rtc::Thread* worker_thread() { return worker_thread_.get(); }
73 const rtc::Thread* worker_thread() const { return worker_thread_.get(); }
Harald Alvestrand4244b5f2020-10-15 12:57:0574 rtc::Thread* network_thread() { return network_thread_; }
75 const rtc::Thread* network_thread() const { return network_thread_; }
Harald Alvestranda39689c2020-10-15 08:34:3176
Danil Chapovalov3bdb49b2023-11-30 07:59:3977 // 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
Harald Alvestranda39689c2020-10-15 08:34:3182 // Accessors only used from the PeerConnectionFactory class
Niels Möllerdcb5a582022-06-20 13:33:5983 rtc::NetworkManager* default_network_manager() {
Harald Alvestrand4244b5f2020-10-15 12:57:0584 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:3185 return default_network_manager_.get();
86 }
Niels Möller573b1452022-06-21 09:37:2987 rtc::PacketSocketFactory* default_socket_factory() {
Harald Alvestrand4244b5f2020-10-15 12:57:0588 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:3189 return default_socket_factory_.get();
90 }
Danil Chapovalovfe66dda2023-12-04 11:05:3891 MediaFactory* call_factory() {
Harald Alvestrand00579e82022-05-03 11:37:3492 RTC_DCHECK_RUN_ON(worker_thread());
Harald Alvestranda39689c2020-10-15 08:34:3193 return call_factory_.get();
94 }
Harald Alvestrandc3fa7c32022-05-22 10:57:0195 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.
Joachim Reiersen45a985c2023-08-24 17:00:01100 bool use_rtx() { return use_rtx_; }
101
102 // For use by tests.
103 void set_use_rtx(bool use_rtx) { use_rtx_ = use_rtx; }
Harald Alvestranda39689c2020-10-15 08:34:31104
105 protected:
Danil Chapovalov7eaa9dc2023-11-27 14:00:21106 ConnectionContext(const Environment& env,
107 PeerConnectionFactoryDependencies* dependencies);
Harald Alvestranda39689c2020-10-15 08:34:31108
Tommi86ee89f2021-04-20 14:58:01109 friend class rtc::RefCountedNonVirtual<ConnectionContext>;
110 ~ConnectionContext();
Harald Alvestranda39689c2020-10-15 08:34:31111
112 private:
Harald Alvestrand4244b5f2020-10-15 12:57:05113 // The following three variables are used to communicate between the
114 // constructor and the destructor, and are never exposed externally.
Harald Alvestranda39689c2020-10-15 08:34:31115 bool wraps_current_thread_;
Niels Möllerb02e1ac2022-02-04 13:29:50116 std::unique_ptr<rtc::SocketFactory> owned_socket_factory_;
Harald Alvestranda39689c2020-10-15 08:34:31117 std::unique_ptr<rtc::Thread> owned_network_thread_
Harald Alvestrand4244b5f2020-10-15 12:57:05118 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31119 rtc::Thread* const network_thread_;
Harald Alvestrand00579e82022-05-03 11:37:34120 AlwaysValidPointer<rtc::Thread> const worker_thread_;
Harald Alvestranda39689c2020-10-15 08:34:31121 rtc::Thread* const signaling_thread_;
Jonas Orelanded99dae2022-03-09 08:28:10122
Danil Chapovalov7eaa9dc2023-11-27 14:00:21123 const Environment env_;
Jonas Orelanded99dae2022-03-09 08:28:10124
Peter Hanspers3e1e8312023-09-05 15:55:34125 // 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_;
Harald Alvestrand0ac50b92022-05-18 07:51:34128
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_;
Harald Alvestranda39689c2020-10-15 08:34:31134 std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05135 RTC_GUARDED_BY(signaling_thread_);
Niels Möllerdcb5a582022-06-20 13:33:59136 std::unique_ptr<rtc::NetworkManager> default_network_manager_
Harald Alvestrand4244b5f2020-10-15 12:57:05137 RTC_GUARDED_BY(signaling_thread_);
Danil Chapovalovfe66dda2023-12-04 11:05:38138 std::unique_ptr<MediaFactory> const call_factory_
Harald Alvestrand00579e82022-05-03 11:37:34139 RTC_GUARDED_BY(worker_thread());
Harald Alvestranda39689c2020-10-15 08:34:31140
Niels Möller573b1452022-06-21 09:37:29141 std::unique_ptr<rtc::PacketSocketFactory> default_socket_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05142 RTC_GUARDED_BY(signaling_thread_);
Tommic3257d02021-02-10 17:40:08143 std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_;
Joachim Reiersen45a985c2023-08-24 17:00:01144
145 // Controls whether to announce support for the the rfc4588 payload format
146 // for retransmitted video packets.
147 bool use_rtx_;
Harald Alvestranda39689c2020-10-15 08:34:31148};
149
150} // namespace webrtc
151
152#endif // PC_CONNECTION_CONTEXT_H_