blob: 3869164aaaeffff92b800fc0c1de5b95c99b0a9a [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
Harald Alvestrandffd5dc72020-10-20 15:35:3117#include "api/call/call_factory_interface.h"
Jonas Orelande62c2f22022-03-29 09:04:4818#include "api/field_trials_view.h"
Harald Alvestranda39689c2020-10-15 08:34:3119#include "api/media_stream_interface.h"
20#include "api/peer_connection_interface.h"
Tommi86ee89f2021-04-20 14:58:0121#include "api/ref_counted_base.h"
Harald Alvestranda39689c2020-10-15 08:34:3122#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2423#include "api/sequence_checker.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3124#include "api/transport/sctp_transport_factory_interface.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3125#include "media/base/media_engine.h"
Harald Alvestranda39689c2020-10-15 08:34:3126#include "p2p/base/basic_packet_socket_factory.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3127#include "rtc_base/checks.h"
28#include "rtc_base/network.h"
29#include "rtc_base/network_monitor_factory.h"
Harald Alvestranda39689c2020-10-15 08:34:3130#include "rtc_base/rtc_certificate_generator.h"
Harald Alvestrandc24a2182022-02-23 13:44:5931#include "rtc_base/socket_factory.h"
Harald Alvestranda39689c2020-10-15 08:34:3132#include "rtc_base/thread.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3133#include "rtc_base/thread_annotations.h"
Harald Alvestranda39689c2020-10-15 08:34:3134
Harald Alvestrand9e334b72022-05-04 13:38:3135namespace cricket {
36class ChannelManager;
37}
38
Harald Alvestranda39689c2020-10-15 08:34:3139namespace rtc {
40class BasicNetworkManager;
41class BasicPacketSocketFactory;
Harald Alvestrandc3fa7c32022-05-22 10:57:0142class UniqueRandomIdGenerator;
Harald Alvestranda39689c2020-10-15 08:34:3143} // namespace rtc
44
45namespace webrtc {
46
47class RtcEventLog;
48
49// This class contains resources needed by PeerConnection and associated
50// objects. A reference to this object is passed to each PeerConnection. The
51// methods on this object are assumed not to change the state in any way that
52// interferes with the operation of other PeerConnections.
Harald Alvestrandffd5dc72020-10-20 15:35:3153//
54// This class must be created and destroyed on the signaling thread.
Tommi86ee89f2021-04-20 14:58:0155class ConnectionContext final
56 : public rtc::RefCountedNonVirtual<ConnectionContext> {
Harald Alvestranda39689c2020-10-15 08:34:3157 public:
Harald Alvestrandffd5dc72020-10-20 15:35:3158 // Creates a ConnectionContext. May return null if initialization fails.
59 // The Dependencies class allows simple management of all new dependencies
60 // being added to the ConnectionContext.
61 static rtc::scoped_refptr<ConnectionContext> Create(
62 PeerConnectionFactoryDependencies* dependencies);
63
Harald Alvestrand4244b5f2020-10-15 12:57:0564 // This class is not copyable or movable.
65 ConnectionContext(const ConnectionContext&) = delete;
66 ConnectionContext& operator=(const ConnectionContext&) = delete;
67
Harald Alvestranda39689c2020-10-15 08:34:3168 // Functions called from PeerConnection and friends
69 SctpTransportFactoryInterface* sctp_transport_factory() const {
Harald Alvestranda39689c2020-10-15 08:34:3170 return sctp_factory_.get();
71 }
72
Harald Alvestrandc3fa7c32022-05-22 10:57:0173 cricket::MediaEngineInterface* media_engine() const {
74 return media_engine_.get();
75 }
Harald Alvestranda39689c2020-10-15 08:34:3176
Harald Alvestrand4244b5f2020-10-15 12:57:0577 rtc::Thread* signaling_thread() { return signaling_thread_; }
78 const rtc::Thread* signaling_thread() const { return signaling_thread_; }
Harald Alvestrand00579e82022-05-03 11:37:3479 rtc::Thread* worker_thread() { return worker_thread_.get(); }
80 const rtc::Thread* worker_thread() const { return worker_thread_.get(); }
Harald Alvestrand4244b5f2020-10-15 12:57:0581 rtc::Thread* network_thread() { return network_thread_; }
82 const rtc::Thread* network_thread() const { return network_thread_; }
Harald Alvestranda39689c2020-10-15 08:34:3183
Jonas Oreland6c7f9842022-04-19 15:24:1084 // Field trials associated with the PeerConnectionFactory.
85 // Note: that there can be different field trials for different
86 // PeerConnections (but they are not supposed change after creating the
87 // PeerConnection).
88 const FieldTrialsView& field_trials() const { return *trials_.get(); }
Harald Alvestranda39689c2020-10-15 08:34:3189
90 // Accessors only used from the PeerConnectionFactory class
Harald Alvestrand4244b5f2020-10-15 12:57:0591 rtc::BasicNetworkManager* default_network_manager() {
92 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:3193 return default_network_manager_.get();
94 }
Harald Alvestrand4244b5f2020-10-15 12:57:0595 rtc::BasicPacketSocketFactory* default_socket_factory() {
96 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:3197 return default_socket_factory_.get();
98 }
Harald Alvestrand4244b5f2020-10-15 12:57:0599 CallFactoryInterface* call_factory() {
Harald Alvestrand00579e82022-05-03 11:37:34100 RTC_DCHECK_RUN_ON(worker_thread());
Harald Alvestranda39689c2020-10-15 08:34:31101 return call_factory_.get();
102 }
Harald Alvestrandc3fa7c32022-05-22 10:57:01103 rtc::UniqueRandomIdGenerator* ssrc_generator() { return &ssrc_generator_; }
104 // Note: There is lots of code that wants to know whether or not we
105 // use RTX, but so far, no code has been found that sets it to false.
106 // Kept in the API in order to ease introduction if we want to resurrect
107 // the functionality.
108 bool use_rtx() { return true; }
Harald Alvestranda39689c2020-10-15 08:34:31109
110 protected:
Harald Alvestrandffd5dc72020-10-20 15:35:31111 explicit ConnectionContext(PeerConnectionFactoryDependencies* dependencies);
Harald Alvestranda39689c2020-10-15 08:34:31112
Tommi86ee89f2021-04-20 14:58:01113 friend class rtc::RefCountedNonVirtual<ConnectionContext>;
114 ~ConnectionContext();
Harald Alvestranda39689c2020-10-15 08:34:31115
116 private:
Harald Alvestrand4244b5f2020-10-15 12:57:05117 // The following three variables are used to communicate between the
118 // constructor and the destructor, and are never exposed externally.
Harald Alvestranda39689c2020-10-15 08:34:31119 bool wraps_current_thread_;
Niels Möllerb02e1ac2022-02-04 13:29:50120 std::unique_ptr<rtc::SocketFactory> owned_socket_factory_;
Harald Alvestranda39689c2020-10-15 08:34:31121 std::unique_ptr<rtc::Thread> owned_network_thread_
Harald Alvestrand4244b5f2020-10-15 12:57:05122 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31123 rtc::Thread* const network_thread_;
Harald Alvestrand00579e82022-05-03 11:37:34124 AlwaysValidPointer<rtc::Thread> const worker_thread_;
Harald Alvestranda39689c2020-10-15 08:34:31125 rtc::Thread* const signaling_thread_;
Jonas Orelanded99dae2022-03-09 08:28:10126
127 // Accessed both on signaling thread and worker thread.
Jonas Orelande62c2f22022-03-29 09:04:48128 std::unique_ptr<FieldTrialsView> const trials_;
Jonas Orelanded99dae2022-03-09 08:28:10129
Harald Alvestrand0ac50b92022-05-18 07:51:34130 const std::unique_ptr<cricket::MediaEngineInterface> media_engine_;
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 Alvestranda39689c2020-10-15 08:34:31137 std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05138 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31139 std::unique_ptr<rtc::BasicNetworkManager> default_network_manager_
Harald Alvestrand4244b5f2020-10-15 12:57:05140 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31141 std::unique_ptr<webrtc::CallFactoryInterface> const call_factory_
Harald Alvestrand00579e82022-05-03 11:37:34142 RTC_GUARDED_BY(worker_thread());
Harald Alvestranda39689c2020-10-15 08:34:31143
144 std::unique_ptr<rtc::BasicPacketSocketFactory> default_socket_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05145 RTC_GUARDED_BY(signaling_thread_);
Tommic3257d02021-02-10 17:40:08146 std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_;
Harald Alvestranda39689c2020-10-15 08:34:31147};
148
149} // namespace webrtc
150
151#endif // PC_CONNECTION_CONTEXT_H_