blob: 43239ffae0ab3e5b0627232dbad945e20ae5907a [file] [log] [blame]
Harald Alvestranda39689c2020-10-15 08:34:31 +00001/*
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:31 +000017#include "api/call/call_factory_interface.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020018#include "api/field_trials_view.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000019#include "api/media_stream_interface.h"
20#include "api/peer_connection_interface.h"
Tommi86ee89f2021-04-20 16:58:01 +020021#include "api/ref_counted_base.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000022#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010023#include "api/sequence_checker.h"
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000024#include "api/transport/sctp_transport_factory_interface.h"
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000025#include "media/base/media_engine.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000026#include "p2p/base/basic_packet_socket_factory.h"
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000027#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:31 +000030#include "rtc_base/rtc_certificate_generator.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000031#include "rtc_base/socket_factory.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000032#include "rtc_base/thread.h"
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000033#include "rtc_base/thread_annotations.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000034
Harald Alvestrand9e334b72022-05-04 13:38:31 +000035namespace cricket {
36class ChannelManager;
37}
38
Harald Alvestranda39689c2020-10-15 08:34:31 +000039namespace rtc {
40class BasicNetworkManager;
41class BasicPacketSocketFactory;
42} // namespace rtc
43
44namespace webrtc {
45
46class RtcEventLog;
47
48// This class contains resources needed by PeerConnection and associated
49// objects. A reference to this object is passed to each PeerConnection. The
50// methods on this object are assumed not to change the state in any way that
51// interferes with the operation of other PeerConnections.
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000052//
53// This class must be created and destroyed on the signaling thread.
Tommi86ee89f2021-04-20 16:58:01 +020054class ConnectionContext final
55 : public rtc::RefCountedNonVirtual<ConnectionContext> {
Harald Alvestranda39689c2020-10-15 08:34:31 +000056 public:
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000057 // Creates a ConnectionContext. May return null if initialization fails.
58 // The Dependencies class allows simple management of all new dependencies
59 // being added to the ConnectionContext.
60 static rtc::scoped_refptr<ConnectionContext> Create(
61 PeerConnectionFactoryDependencies* dependencies);
62
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000063 // This class is not copyable or movable.
64 ConnectionContext(const ConnectionContext&) = delete;
65 ConnectionContext& operator=(const ConnectionContext&) = delete;
66
Harald Alvestranda39689c2020-10-15 08:34:31 +000067 // Functions called from PeerConnection and friends
68 SctpTransportFactoryInterface* sctp_transport_factory() const {
Harald Alvestranda39689c2020-10-15 08:34:31 +000069 return sctp_factory_.get();
70 }
71
72 cricket::ChannelManager* channel_manager() const;
73
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000074 rtc::Thread* signaling_thread() { return signaling_thread_; }
75 const rtc::Thread* signaling_thread() const { return signaling_thread_; }
Harald Alvestrand00579e82022-05-03 11:37:34 +000076 rtc::Thread* worker_thread() { return worker_thread_.get(); }
77 const rtc::Thread* worker_thread() const { return worker_thread_.get(); }
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000078 rtc::Thread* network_thread() { return network_thread_; }
79 const rtc::Thread* network_thread() const { return network_thread_; }
Harald Alvestranda39689c2020-10-15 08:34:31 +000080
Jonas Oreland6c7f9842022-04-19 17:24:10 +020081 // Field trials associated with the PeerConnectionFactory.
82 // Note: that there can be different field trials for different
83 // PeerConnections (but they are not supposed change after creating the
84 // PeerConnection).
85 const FieldTrialsView& field_trials() const { return *trials_.get(); }
Harald Alvestranda39689c2020-10-15 08:34:31 +000086
87 // Accessors only used from the PeerConnectionFactory class
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000088 rtc::BasicNetworkManager* default_network_manager() {
89 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31 +000090 return default_network_manager_.get();
91 }
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000092 rtc::BasicPacketSocketFactory* default_socket_factory() {
93 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31 +000094 return default_socket_factory_.get();
95 }
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000096 CallFactoryInterface* call_factory() {
Harald Alvestrand00579e82022-05-03 11:37:34 +000097 RTC_DCHECK_RUN_ON(worker_thread());
Harald Alvestranda39689c2020-10-15 08:34:31 +000098 return call_factory_.get();
99 }
100
101 protected:
Harald Alvestrandffd5dc72020-10-20 15:35:31 +0000102 explicit ConnectionContext(PeerConnectionFactoryDependencies* dependencies);
Harald Alvestranda39689c2020-10-15 08:34:31 +0000103
Tommi86ee89f2021-04-20 16:58:01 +0200104 friend class rtc::RefCountedNonVirtual<ConnectionContext>;
105 ~ConnectionContext();
Harald Alvestranda39689c2020-10-15 08:34:31 +0000106
107 private:
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000108 // The following three variables are used to communicate between the
109 // constructor and the destructor, and are never exposed externally.
Harald Alvestranda39689c2020-10-15 08:34:31 +0000110 bool wraps_current_thread_;
Niels Möllerb02e1ac2022-02-04 14:29:50 +0100111 std::unique_ptr<rtc::SocketFactory> owned_socket_factory_;
Harald Alvestranda39689c2020-10-15 08:34:31 +0000112 std::unique_ptr<rtc::Thread> owned_network_thread_
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000113 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31 +0000114 rtc::Thread* const network_thread_;
Harald Alvestrand00579e82022-05-03 11:37:34 +0000115 AlwaysValidPointer<rtc::Thread> const worker_thread_;
Harald Alvestranda39689c2020-10-15 08:34:31 +0000116 rtc::Thread* const signaling_thread_;
Jonas Orelanded99dae2022-03-09 09:28:10 +0100117
118 // Accessed both on signaling thread and worker thread.
Jonas Orelande62c2f22022-03-29 11:04:48 +0200119 std::unique_ptr<FieldTrialsView> const trials_;
Jonas Orelanded99dae2022-03-09 09:28:10 +0100120
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000121 // channel_manager is accessed both on signaling thread and worker thread.
Harald Alvestrand9e334b72022-05-04 13:38:31 +0000122 // Const after construction, explicitly cleared in destructor.
Harald Alvestranda39689c2020-10-15 08:34:31 +0000123 std::unique_ptr<cricket::ChannelManager> channel_manager_;
124 std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000125 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31 +0000126 std::unique_ptr<rtc::BasicNetworkManager> default_network_manager_
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000127 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31 +0000128 std::unique_ptr<webrtc::CallFactoryInterface> const call_factory_
Harald Alvestrand00579e82022-05-03 11:37:34 +0000129 RTC_GUARDED_BY(worker_thread());
Harald Alvestranda39689c2020-10-15 08:34:31 +0000130
131 std::unique_ptr<rtc::BasicPacketSocketFactory> default_socket_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000132 RTC_GUARDED_BY(signaling_thread_);
Tommic3257d02021-02-10 17:40:08 +0000133 std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_;
Harald Alvestranda39689c2020-10-15 08:34:31 +0000134};
135
136} // namespace webrtc
137
138#endif // PC_CONNECTION_CONTEXT_H_