| 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 | // This header file provides wrapper for common WebRTC utilities. |
| 12 | // Different application may need different implementations of these utilities, |
| 13 | // Moreover, single application may need to use WebRTC for multiple purposes, |
| 14 | // and thus would need to provide different utilities implementations for |
| 15 | // different peer connections. |
| 16 | // The main purpose of the `Environment` class below is to propagate references |
| 17 | // to those utilities to all WebRTC classes that need them. |
| 18 |
| 19 | #ifndef API_ENVIRONMENT_ENVIRONMENT_H_ |
| 20 | #define API_ENVIRONMENT_ENVIRONMENT_H_ |
| 21 |
| 22 | #include <utility> |
| 23 |
| 24 | #include "absl/base/nullability.h" |
| 25 | #include "api/ref_counted_base.h" |
| 26 | #include "api/scoped_refptr.h" |
| 27 | #include "rtc_base/system/rtc_export.h" |
| 28 |
| 29 | namespace webrtc { |
| 30 |
| 31 | // These classes are forward declared to keep Environment dependencies |
| 32 | // lightweight. Users who need any of the types below should include their |
| 33 | // header explicitely. |
| 34 | class Clock; |
| 35 | class TaskQueueFactory; |
| 36 | class FieldTrialsView; |
| 37 | class RtcEventLog; |
| 38 |
| 39 | // Contains references to WebRTC utilities. Object of this class should be |
| 40 | // passed as a construction parameter and saved by value in each class that |
| 41 | // needs it. Most classes shouldn't create a new instance of the `Environment`, |
| 42 | // but instead should use a propagated copy. |
| 43 | // Usually Environment should be the first parameter in a constructor or a |
| 44 | // factory, and the first member in the class. Keeping Environment as the first |
| 45 | // member in the class ensures utilities (e.g. clock) are still valid during |
| 46 | // destruction of other members. |
| 47 | // |
| 48 | // Example: |
| 49 | // class PeerConnection { |
| 50 | // public: |
| 51 | // PeerConnection(const Environment& env, ...) |
| 52 | // : env_(env), |
| 53 | // log_duration_on_destruction_(&env_.clock()), |
| 54 | // rtp_manager_(env_, ...), |
| 55 | // ... |
| 56 | // |
| 57 | // const FieldTrialsView& trials() const { return env_.field_trials(); } |
| 58 | // |
| 59 | // scoped_refptr<RtpTransceiverInterface> AddTransceiver(...) { |
| 60 | // return make_ref_counted<RtpTransceiverImpl>(env_, ...); |
| 61 | // } |
| 62 | // |
| 63 | // private: |
| 64 | // const Environment env_; |
| 65 | // Stats log_duration_on_destruction_; |
| 66 | // RtpTransmissionManager rtp_manager_; |
| 67 | // }; |
| 68 | // This class is thread safe. |
| 69 | class RTC_EXPORT Environment final { |
| 70 | public: |
| 71 | // Default constructor is deleted in favor of creating this object using |
| 72 | // `EnvironmentFactory`. To create the default environment use |
| 73 | // `EnvironmentFactory().Create()` or `CreateEnvironment()`. |
| 74 | Environment() = delete; |
| 75 |
| 76 | Environment(const Environment&) = default; |
| 77 | Environment(Environment&&) = default; |
| 78 | Environment& operator=(const Environment&) = default; |
| 79 | Environment& operator=(Environment&&) = default; |
| 80 |
| 81 | ~Environment() = default; |
| 82 |
| 83 | // Provides means to alter behavior, mostly for A/B testing new features. |
| 84 | // See ../../g3doc/field-trials.md |
| 85 | const FieldTrialsView& field_trials() const; |
| 86 |
| 87 | // Provides an interface to query current time. |
| 88 | // See ../../g3doc/implementation_basics.md#time |
| 89 | Clock& clock() const; |
| 90 |
| 91 | // Provides a factory for task queues, WebRTC threading primitives. |
| 92 | // See ../../g3doc/implementation_basics.md#threads |
| 93 | TaskQueueFactory& task_queue_factory() const; |
| 94 |
| 95 | // Provides an interface for collecting structured logs. |
| 96 | // See ../../logging/g3doc/rtc_event_log.md |
| 97 | RtcEventLog& event_log() const; |
| 98 |
| 99 | private: |
| 100 | friend class EnvironmentFactory; |
| 101 | Environment(scoped_refptr<const rtc::RefCountedBase> storage, |
| 102 | absl::Nonnull<const FieldTrialsView*> field_trials, |
| 103 | absl::Nonnull<Clock*> clock, |
| 104 | absl::Nonnull<TaskQueueFactory*> task_queue_factory, |
| 105 | absl::Nonnull<RtcEventLog*> event_log) |
| 106 | : storage_(std::move(storage)), |
| 107 | field_trials_(field_trials), |
| 108 | clock_(clock), |
| 109 | task_queue_factory_(task_queue_factory), |
| 110 | event_log_(event_log) {} |
| 111 |
| 112 | // Container that keeps ownership of the utilities below. |
| 113 | // Defining this as a RefCountedBase allows `Environment` to share this |
| 114 | // storage with another `Environment`, in particular allows `Environment` to |
| 115 | // be copyable. It is up to the `EnvironmentFactory` to provide an object that |
| 116 | // ensures references to utilties below are valid while object in the |
| 117 | // `storage_` is alive. |
| 118 | scoped_refptr<const rtc::RefCountedBase> storage_; |
| 119 |
| 120 | absl::Nonnull<const FieldTrialsView*> field_trials_; |
| 121 | absl::Nonnull<Clock*> clock_; |
| 122 | absl::Nonnull<TaskQueueFactory*> task_queue_factory_; |
| 123 | absl::Nonnull<RtcEventLog*> event_log_; |
| 124 | }; |
| 125 |
| 126 | //------------------------------------------------------------------------------ |
| 127 | // Implementation details follow |
| 128 | //------------------------------------------------------------------------------ |
| 129 |
| 130 | inline const FieldTrialsView& Environment::field_trials() const { |
| 131 | return *field_trials_; |
| 132 | } |
| 133 |
| 134 | inline Clock& Environment::clock() const { |
| 135 | return *clock_; |
| 136 | } |
| 137 |
| 138 | inline TaskQueueFactory& Environment::task_queue_factory() const { |
| 139 | return *task_queue_factory_; |
| 140 | } |
| 141 |
| 142 | inline RtcEventLog& Environment::event_log() const { |
| 143 | return *event_log_; |
| 144 | } |
| 145 |
| 146 | } // namespace webrtc |
| 147 |
| 148 | #endif // API_ENVIRONMENT_ENVIRONMENT_H_ |
| 149 |