blob: 8fad13c10c6d9b2b8994b3251e36f5711ff8d8d6 [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"
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"
24#include "api/transport/webrtc_key_value_config.h"
25#include "media/base/media_engine.h"
Harald Alvestranda39689c2020-10-15 08:34:3126#include "p2p/base/basic_packet_socket_factory.h"
27#include "pc/channel_manager.h"
Harald Alvestrandffd5dc72020-10-20 15:35:3128#include "rtc_base/checks.h"
29#include "rtc_base/network.h"
30#include "rtc_base/network_monitor_factory.h"
Harald Alvestranda39689c2020-10-15 08:34:3131#include "rtc_base/rtc_certificate_generator.h"
32#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
35namespace rtc {
36class BasicNetworkManager;
37class BasicPacketSocketFactory;
38} // namespace rtc
39
40namespace webrtc {
41
42class 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 Alvestrandffd5dc72020-10-20 15:35:3148//
49// This class must be created and destroyed on the signaling thread.
Tommi86ee89f2021-04-20 14:58:0150class ConnectionContext final
51 : public rtc::RefCountedNonVirtual<ConnectionContext> {
Harald Alvestranda39689c2020-10-15 08:34:3152 public:
Harald Alvestrandffd5dc72020-10-20 15:35:3153 // 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 Alvestrand4244b5f2020-10-15 12:57:0559 // This class is not copyable or movable.
60 ConnectionContext(const ConnectionContext&) = delete;
61 ConnectionContext& operator=(const ConnectionContext&) = delete;
62
Harald Alvestranda39689c2020-10-15 08:34:3163 // Functions called from PeerConnection and friends
64 SctpTransportFactoryInterface* sctp_transport_factory() const {
Harald Alvestranda39689c2020-10-15 08:34:3165 return sctp_factory_.get();
66 }
67
68 cricket::ChannelManager* channel_manager() const;
69
Harald Alvestrand4244b5f2020-10-15 12:57:0570 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_; }
73 const rtc::Thread* worker_thread() const { return worker_thread_; }
74 rtc::Thread* network_thread() { return network_thread_; }
75 const rtc::Thread* network_thread() const { return network_thread_; }
Harald Alvestranda39689c2020-10-15 08:34:3176
Harald Alvestranda39689c2020-10-15 08:34:3177 const WebRtcKeyValueConfig& trials() const { return *trials_.get(); }
78
79 // Accessors only used from the PeerConnectionFactory class
Harald Alvestrand4244b5f2020-10-15 12:57:0580 rtc::BasicNetworkManager* default_network_manager() {
81 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:3182 return default_network_manager_.get();
83 }
Harald Alvestrand4244b5f2020-10-15 12:57:0584 rtc::BasicPacketSocketFactory* default_socket_factory() {
85 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:3186 return default_socket_factory_.get();
87 }
Harald Alvestrand4244b5f2020-10-15 12:57:0588 CallFactoryInterface* call_factory() {
89 RTC_DCHECK_RUN_ON(worker_thread_);
Harald Alvestranda39689c2020-10-15 08:34:3190 return call_factory_.get();
91 }
92
93 protected:
Harald Alvestrandffd5dc72020-10-20 15:35:3194 explicit ConnectionContext(PeerConnectionFactoryDependencies* dependencies);
Harald Alvestranda39689c2020-10-15 08:34:3195
Tommi86ee89f2021-04-20 14:58:0196 friend class rtc::RefCountedNonVirtual<ConnectionContext>;
97 ~ConnectionContext();
Harald Alvestranda39689c2020-10-15 08:34:3198
99 private:
Harald Alvestrand4244b5f2020-10-15 12:57:05100 // The following three variables are used to communicate between the
101 // constructor and the destructor, and are never exposed externally.
Harald Alvestranda39689c2020-10-15 08:34:31102 bool wraps_current_thread_;
103 // Note: Since owned_network_thread_ and owned_worker_thread_ are used
104 // in the initialization of network_thread_ and worker_thread_, they
105 // must be declared before them, so that they are initialized first.
106 std::unique_ptr<rtc::Thread> owned_network_thread_
Harald Alvestrand4244b5f2020-10-15 12:57:05107 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31108 std::unique_ptr<rtc::Thread> owned_worker_thread_
Harald Alvestrand4244b5f2020-10-15 12:57:05109 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31110 rtc::Thread* const network_thread_;
111 rtc::Thread* const worker_thread_;
112 rtc::Thread* const signaling_thread_;
Harald Alvestrand4244b5f2020-10-15 12:57:05113 // channel_manager is accessed both on signaling thread and worker thread.
Harald Alvestranda39689c2020-10-15 08:34:31114 std::unique_ptr<cricket::ChannelManager> channel_manager_;
115 std::unique_ptr<rtc::NetworkMonitorFactory> const network_monitor_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05116 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31117 std::unique_ptr<rtc::BasicNetworkManager> default_network_manager_
Harald Alvestrand4244b5f2020-10-15 12:57:05118 RTC_GUARDED_BY(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31119 std::unique_ptr<webrtc::CallFactoryInterface> const call_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05120 RTC_GUARDED_BY(worker_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31121
122 std::unique_ptr<rtc::BasicPacketSocketFactory> default_socket_factory_
Harald Alvestrand4244b5f2020-10-15 12:57:05123 RTC_GUARDED_BY(signaling_thread_);
Tommic3257d02021-02-10 17:40:08124 std::unique_ptr<SctpTransportFactoryInterface> const sctp_factory_;
Harald Alvestranda39689c2020-10-15 08:34:31125 // Accessed both on signaling thread and worker thread.
126 std::unique_ptr<WebRtcKeyValueConfig> const trials_;
127};
128
129} // namespace webrtc
130
131#endif // PC_CONNECTION_CONTEXT_H_